After node update I need to modify a field value in several connected nodes.
As I know, I cannot use hook_entity_update() since $node->save() forms infinite recursion.

How can I modify node fields after a node has been updated? (besides hook_cron)

  • 2
    You get an infinite recursion only if you do the node saves in the update hook unconditionally.
    – 4uk4
    Commented Aug 23, 2020 at 7:30
  • I need to modify a field value in another node in hook_entity_update(). Could you provide a tip what condition can help to stop infinite recursion?
    – Dimetry
    Commented Aug 23, 2020 at 8:20
  • 2
    hook_node_upate already implies the check for the entity type. If you reference a different content type add a check for the content type. If you reference within the same content type then the calculation of the field values could already imply a check, otherwise the recursion is somehow built into your logic. Then it could make sense to refactor the updates to a service which would keep track of the updated nodes.
    – 4uk4
    Commented Aug 23, 2020 at 8:53

1 Answer 1


Use hook_form_alter or hook_form_FORM_ID_alter

Add a custom validation that runs after all validation has passed

$form['actions']['submit']['#submit'][] = 'mymodule_update_referenced_nodes';

Then in that function

function mymodule_update_referenced_nodes(&$form, \Drupal\Core\Form\FormStateInterface $form_state){

  $field_other = $form_state->getValue('field_other')[0]['value'];

  $references = $node->get('field_referenced')->referencedEntities();
  foreach ($references as $referenced_node) {
    $referenced_node->field_other->value = $field_other;

  • Thanks. I could have guessed myself. Not universal solution (only for form update). But acceptable for the case.
    – Dimetry
    Commented Aug 23, 2020 at 7:05
  • 1
    @nosssweat, I am wondering why you use validation instead of another submit handler. Don't you have a risk of inconsistencies if saving of the node fails? Commented Aug 23, 2020 at 8:33
  • @Neograph734 actually you're correct, should be put in a submit instead, thanks.
    – No Sssweat
    Commented Aug 23, 2020 at 10:04
  • @Dimetry updated answer.
    – No Sssweat
    Commented Aug 23, 2020 at 10:07
  • 1
    @NoSssweat Oke. I was just wondering if doing it in validation had some advantage or something, as I always used submit. Apparently it does not ;) Commented Aug 23, 2020 at 10:11

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.