0

In the hook_entity_update function

update_candidat_from_contrat_direct(Drupal\Core\Entity\EntityInterface $entity)

I call another function in which :

  • I create a new node

       $new_entity = \Drupal::entityTypeManager()->getStorage('node')->create([
        'type' => 'candidat',
        'field_nom' => $nom,
      ]);
      $new_entity->save();
    
  • I want to attach this $new_entity to the $entity

      $nid = $new_entity->id();
      $candidats_attaches[] = $nid;
      $entity->set('field_candidats', $candidats_attaches);
      $entity->save();
    

But because of the call to "save" function, this involve a new call to the "hook_entity_update", so another call to this sub-function, so a loop....

I understood that there is the "presave" function to avoid this behaviour, but I did not succeed to use it. Can I have some advices, please?

9
  • Welcome to Drupal Answers! Both hook_entity_presave() and hook_entity_update() would be called when $entity->save() is called. hook_entity_presave() doesn't avoid the issue you have with your current code. Is $entity a node, or a different entity? In the case it's a node, is its content type different?
    – apaderno
    Mar 19, 2021 at 20:38
  • Also, the question seems to say that update_candidat_from_contrat_direct(Drupal\Core\Entity\EntityInterface $entity) is an implementation of hook_entity_update() but that's not true.
    – apaderno
    Mar 19, 2021 at 20:40
  • entity is a node.
    – cocq
    Mar 22, 2021 at 13:02
  • "update_candidat_from_contrat_direct(Drupal\Core\Entity\EntityInterface $entity) " is called by "lamih_entity_update"
    – cocq
    Mar 22, 2021 at 13:04
  • When entity is updated by the user, the aim is, within update_candidat... function, to, : (1) create an instance ot a content type, (3) fill its fields with values isued from entity form, (3) register id of this instance in a field of the entity, (4) update the entity in the database.
    – cocq
    Mar 22, 2021 at 13:08

0

Your Answer

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

Browse other questions tagged or ask your own question.