Update a Referenced Node with a value from the node referencing it In Drupal 7, it was easy to update a referenced node with a value from the node referencing it, using rules: https://www.drupal.org/node/1836240

But how could I do the same in Drupal 8? This code is for updating a node programmatically:

use Drupal\node\Entity\Node;
$node = Node::load($nid);
$node-&gt;setTitle('MY NEW TITLE'); // This is a special meta field
$node-&gt;set('FIELD_NAME', 'THIS IS DATA'); // This is a Field added in to the content type
$node-&gt;save();


What hook should I call and place this code inside? I need when the user creates a new node or updates an existing node, then a field from the referenced node been updated with the value of a field from the current node. Should I use a form alter function? Is it possible with hook form alter to update, not only the current node, but also a field from the referenced node, or is there another hook for that?
Thank you both @GiorgosK and @4k4, for pointing me to the right direction! Here is the solution:

use Drupal\node\Entity\Node;


function my_module_entity_insert(Drupal\Core\Entity\EntityInterface
$entity) {
 if ($entity-&gt;getEntityTypeId() == 'node' &amp;&amp; $entity-&gt;bundle() ==  'iamoriginal') {
   if (!$entity-&gt;field_my_reference-&gt;isEmpty() &amp;&amp;   !$entity-&gt;field_number-&gt;isEmpty() &amp;&amp;
 !$entity-&gt;field_hide_me-&gt;value) {
  _my_module_update_parent($entity, 'add');
 }
 }
}


function my_module_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
 if ($entity-&gt;getEntityTypeId() == 'node' &amp;&amp; $entity-&gt;bundle() == 'iamoriginal') {
$original = $entity-&gt;original;
if (!$entity-&gt;field_my_reference-&gt;isEmpty() &amp;&amp; !$entity-&gt;field_number-&gt;isEmpty() &amp;&amp;
  $entity-&gt;field_hide_me-&gt;value != $original-&gt;field_hide_me-&gt;value) {
  $operation = ($entity-&gt;field_hide_me-&gt;value) ? 'sub' : 'add';
  _my_module_update_parent($entity, $operation);
}
 }
}

function _my_module_update_parent($entity, $operation) {
 $ref = $entity-&gt;get('field_my_reference')
   -&gt;first()
   -&gt;get('entity')
   -&gt;getTarget()
  -&gt;getValue();
 if ($ref-&gt;hasField('field_number_of_originals') &amp;&amp;        $ref-&gt;hasField('field_total_value')) {
$total = $ref-&gt;field_total_value-&gt;value;
$count = $ref-&gt;field_number_of_originals-&gt;value;
$number = $entity-&gt;field_number-&gt;value;
switch ($operation) {
  case 'add':
    $total += $number;
    $count++;
    break;
  case 'sub':
    $total -= $number;
    $count--;
    break;
}
$ref-&gt;set('field_total_value', $total);
$ref-&gt;set('field_number_of_originals', $count);
$ref-&gt;save();
 }
 }


If a user adds a new node of the post type "iamoriginal" then the value of the field_number is added to the refrenced node's field_total_value and +1 to the field_number_of_originals. If he later wants to disable the original node ,he gives a value to the field_hide_me and the previously added values to the referenced node are abstracted. Thank you again for your help!!!