4.7.x node.module node_object_prepare(&$node)
5.x node.module node_object_prepare(&$node)
6.x node.pages.inc node_object_prepare(&$node)
7.x node.module node_object_prepare($node)

Prepares a node object for editing.

Fills in a few default values, and then invokes hook_prepare() on the node type module, and hook_node_prepare() on all modules.


$node: A node object.

1 call to node_object_prepare()
node_form in modules/node/node.pages.inc
Form constructor for the node add/edit form.


modules/node/node.module, line 979
The core that allows content to be submitted to the site. Modules and scripts may programmatically submit nodes using the usual form API pattern.


function node_object_prepare($node) {
  // Set up default values, if required.
  $node_options = variable_get('node_options_' . $node->type, array('status', 'promote'));
  // If this is a new node, fill in the default values.
  if (!isset($node->nid) || isset($node->is_new)) {
    foreach (array('status', 'promote', 'sticky') as $key) {
      // Multistep node forms might have filled in something already.
      if (!isset($node->$key)) {
        $node->$key = (int) in_array($key, $node_options);
    global $user;
    $node->uid = $user->uid;
    $node->created = REQUEST_TIME;
  else {
    $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
    // Remove the log message from the original node object.
    $node->log = NULL;
  // Always use the default revision setting.
  $node->revision = in_array('revision', $node_options);

  node_invoke($node, 'prepare');
  module_invoke_all('node_prepare', $node);


node_save() does not return anything anymore. The following will create a node, but returns nothing:

$node = new stdClass();
$node->type = 'thing';

$node->title = 'Test thing';

// ...more node properties...

$new = node_save($node);

var_dump($new); // => NULL

If you would like the nid of the node right after you create it, you will want to do:

$node = new stdClass();
$node->type = 'thing';

$node->title = 'Test thing';

// ...more node properties...


$nid = $node->nid;

print 'nid of the node I just created: ' . $nid;

Here's an article explaining the creation of nodes programmatically in d7 a bit more: http://www.group42.ca/creating_and_updating_nodes_programmatically_in_dr...

node_save() has never returned any value since Drupal 5, as the node parameter is passed by reference.


It appears this function is only intended to prepare a node for editing when using the node form - not though code.
Calling something like

$node = node_load($nid);

Will cause data loss - menu items (at least) will be deleted silently. menu_node_prepare() fails to set its own 'enabled' flag.

This has been marked as "by design" [#1534356]


If you want to use node_prepare to pull in additional node data that's not always loaded, and then save later,
- clone the node object first (like token.module does
- set the 'enabled' flag manually on menu items after loading so that node_menu_save() does not delete them unintentionally.

Suggested core patch.
Suggested work-around:

    if (!empty($node->menu)) {
      // Calling menu_node_prepare(), menu_node_save() 
      // will cause data loss unless we do this.
      $node->menu['link']['enabled'] = (int) (bool) $node->menu['link']['mlid'];

The documentation also says ...

Modules and scripts may programmatically submit nodes using the usual form API pattern.

If you are using in your module, dont forget to include
module_load_include('inc', 'node', 'node.pages');