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ó