0

In my site Drupal 8, I have custom entities (say components) and bundles. I load these entities in a node using the Inline entity module.
On the front-end, the contextual links provide two edit links: an edit link to edit the node with all inline entities and a link for a specific entity. When users use the second link, they get the edit page for that entity and its field values. Using the second link, the node last update value won't change.

I tried updating the node value in hook_entity_presave(), but it creates infinite redirection. How do I avoid it?

1
  • Make sure you don't call $node->save() in your presave, just update the time, the save will happen automatically.
    – sonfd
    Commented Mar 24, 2019 at 15:35

1 Answer 1

2

hook_entity_presave() cannot save any entity, or saving an entity will cause hook_entity_presave() to be invoked again. If your hook_entity_presave() implementation isn't able to understand it is invoked for the second time, and saves again an entity, it will cause an infinite loop.
Even if you implement hook_ENTITY_TYPE_presave() and save a different entity, you will cause hook_entity_presave() to be invoked more than once, and that could cause issues.

In your case, if components is the entity type being edited, you should implement hook_components_presave(), get the node for which you need to alter the Last changed value, and save that node. As I said, I cannot guaranty there will not be issues, but at least your hook_components_presave() implementation won't be called twice, as it is invoked for the components entities, and it saves a node. The issues would come, for example, from a module that assumes that hook_entity_presave() is invoked only when a user edits or creates a node, and saves it.

1
  • this works perfectly. :)
    – DDD
    Commented Mar 25, 2019 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.