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?
hook_entity_presave()
andhook_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?update_candidat_from_contrat_direct(Drupal\Core\Entity\EntityInterface $entity)
is an implementation ofhook_entity_update()
but that's not true.