Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 379 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drupal node_save no longer returns $nid, so how do I get it?

#1
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?
Reply

#2
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.
Reply

#3
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:
<br>//use drupal_set_message() to inform the user that the node was successfully saved
<br>//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;

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through