I've write a simple module for my sites, where I populate a node reference by code.
I use hook_node_presave, but I notice that drupal enters TWO times in this hook, so if my node reference field is multiple, it populates it two times.
Is there something wrong on my code or is normal? In the presave hook I've to load another node and then save it
I have something like this:
function alterator_node_presave($node) {
if ($node->type == 'partner_progetto') {
// here I load a node "progetto"
// that is referenced on my partner_progetto node
$progetto = node_load($node->field_progetto_rif['und'][0]['nid']);
// here I load another node "soggetto"
// that is referenced on my partner_progetto node
$soggetto = node_load($node->field_soggetto['und'][0]['nid']);
// then I've to fill a node_reference in the node $progetto,
// using the nid of $soggetto
$progetto->field_soggetto_ref['und'][]['nid'] = $soggetto->nid;
node_save($progetto);
}
}
In this hook the system enter 2 times, so when I save a "partner progetto" node that has a reference to the node soggetto with nid '150' I've that the "progetto" node has this structure:
$progetto->field_soggetto_ref
['und']
[0]['nid'] = 150,
[1]['nid'] = 150;
The value of this field is filled two times.