Edit field in contentType B when creating or editing node of contentType A I need to edit a field in contentType B when I create or edit nodes of contentType A.

I know it's possible to use rules for this but I need to have more control so I want to do it in my own module.

Tried this code without success:

function MYMODULE_node_presave($node) {
    MYMODULE_change_value($node);
}

function MYMODULE_change_value($node) {
    $node = node_load($node-&gt;nid);

    $referenced_nid = $node-&gt;referenced_node[LANGUAGE_NOE][0]['target_id']

    $referenced_node = node_load(referenced_nid);

    $referenced_node-&gt;my_field[LANGUAGE_NONE][0]['value'] = 12345.0;
    node_save($referenced_node);
}


Am I choosing the wrong hook?
Try the following:

/**
 * Implements hook_node_presave().
 */
function yourModule_node_presave($node) {
  if ($node-&gt;type == 'your_typeA') {
    yourModule_change_value($node);
  }
}

function yourModule_change_value($node) {
  if (isset($node-&gt;field_node_referenced[LANGUAGE_NONE][0]['target_id'])) {
    //- Get referenced node id
    $referenced_node_id = $node-&gt;field_node_referenced[LANGUAGE_NONE][0]['target_id'];
    //- Load referenced node
    $wrapper = entity_metadata_wrapper('node', $referenced_node_id);
    $wrapper-&gt;field_name-&gt;set(array(
      'value' =&gt; 'test new field value'
    ));
    $wrapper-&gt;save();
  }
}