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;


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:

    [0]['nid'] = 150,
    [1]['nid'] = 150;

The value of this field is filled two times.


Remove node_save($progetto); from alterator_node_presave and try. Because hook_node_presave is called just before the node_save. It can be reason of saving the field twice.


hook_node_presave() is invoked once for each node. As you implementation calls node_save(), which then invokes hook_node_presave() again, your hook is invoked twice, once for each node.

Possibly, you could have an infinite loop, if $node->field_progetto_rif['und'][0]['nid'] is the node ID of a node whose content type is "partner_progetto"; be sure to avoid that.

Your code is the adding a new node reference all the times a node is saved, as you use $progetto->field_soggetto_ref['und'][]['nid'], and not $progetto->field_soggetto_ref['und'][0]['nid'].

  • But in my hook there is an if based on node type. If it enter in the hook when is saving $progetto, the system should simply go away because it doesn't enter in the if. Or not? – arrubiu Sep 30 '12 at 9:21
  • hook_node_presave() is called for every node, independently from its content type. It is invoked, but with your actual code it doesn't do anything; still, the hook is invoked twice. – kiamlaluno Sep 30 '12 at 9:22
  • I know it, but the question is: why it enter two times in the if? – arrubiu Sep 30 '12 at 9:24
  • Actually, you are adding a new node reference every time the node is saved. – kiamlaluno Sep 30 '12 at 9:46
  • Yes, but only when the $node->type is "partner_progetto". Why enter in the "if" if the $node->type isn't "partner_progetto"? – arrubiu Sep 30 '12 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.