8

I have code like this:

... setup $issue object ...
$node = node_save($issue);

print_r($node);

The node is created successfully, and everything works fine...but nothing it returned from save_node(). Older docs indicate that it returns $nid. Several discussions and tickets indicate that in recent Drupal versions the node object is returned, but I get nothing back (and $node->nid is empty).

So, how do I find out the nid of the newly created node?

Isaac Waller
  • 32,709
  • 29
  • 96
  • 107
swelljoe
  • 808
  • 5
  • 17

2 Answers2

24

OK, finally figured this one out (and boy do I feel silly).

node_save now operates on the existing node object (already defined in $issue in my case), and simply adds the nid field (among others) to the existing object. Nothing is returned, but I can access the nid with $issue->nid after node_save has run.

swelljoe
  • 808
  • 5
  • 17
  • Always great to see someone following up to help save others some frustration. Thank you a year later! – wynz Jun 19 '10 at 08:50
0

Thank You! Very good to know. Thank You for answering your own question and sharing so others (like myself) can learn! Good solution! Thanks for contributing

2 tips:
//use drupal_set_message() to inform the user that the node was successfully saved
//try using node_submit() before saving to catch error that might exist

if ($_newnode = node_submit($_newnode)) {
    node_save($_newnode);
    drupal_set_message(t("Node ".$_newnode->title." added correctly"));
    $return = $_newnode->uid;
} else {
    $return = 0;
    drupal_set_message(t("Node ".$_newnode->title." added incorrectly"), "error");
}

return $return;
jem
  • 9
  • 1