Score:0

Check referenced child entity update in Inline Entity Form and change author in parent entity

hu flag

Can anybody help me how I can check upon entity update (with hook_entity_update or with something else) whether one of the fields of child referenced entity is update and make changes on parent entity? The child is an Inline Entity Form inside parent edit form.

There is a Parent node type which has a reference field (field_referenced_child, cardinality is 1) to Child node type which has a field_i_need_to_check textfield.

  function hook_node_update(EntityInterface $parent) 
  {
    if ($parent->bundle() != 'parent') {
      return;
    }

    $original_parent = $parent->original;

    if (
      $parent->get('field_referenced_child')->entity->get('field_i_need_to_check')->isEmpty() &&
      !$original_parent->get('field_referenced_child')->entity->get('field_i_need_to_check')->isEmpty()
    )
    {
      $parent->setOwnerId(0);
      $parent->save();
 
      // But this if never true, because the field_i_need_to_check 
      // in referenced node in new and in original is always same at this time.
    }
  }

When I try from the other side, catch the child entity update, I cannot change the parent because it will be overwritten by original parent entity save function.

  function hook_node_update(EntityInterface $child) 
  {
    if ($child->bundle() != 'child') {
      return;
    }

    $original_child = $child->original;

    if (
      $child->get('field_i_need_to_check')->isEmpty() &&
      !$original_child->get('field_i_need_to_check')->isEmpty()
    )
    {
      // the get_child_parent will return the parent
      $parent = get_child_parent($child);
      $parent->setOwnerId(0);
      $parent->save();
 
      // But this will never change the author to anonymus because 
      // after this will occur the original save and the later save is overwritten my changes. -,-
    }
  }

  function get_child_parent(Node $child) : Node
  {
    $parents = \Drupal::entityTypeManager()
      ->getStorage('node')
      ->loadByProperties([
        'type' => 'parent',
        'field_referenced_child' => $child->id()
      ]);

    // only 1 child can be as referenced entity so it will give the parent what I search
    return array_shift($parents);
  }

Does anybody have suggestions? :-)

King regards,

Gábor Riskó

4uk4 avatar
cn flag
This makes sense, the first try only works if the reference field is revision-based, which is a contrib module and is mainly used by Paragraphs. The second try has the problem, that the form values of the parent are still to be submitted by the inline form and overwrite any changes you make. So you probably need to change those values in a submit handler of the form.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.