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?

2

Make sure you don't call $node->save() in your presave, just update the time, the save will happen automatically.

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.

  • this works perfectly. :) – DDD Mar 25 '19 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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