function node_object_prepare

You are here

7 node.module node_object_prepare($node)
4.7 node.module node_object_prepare(&$node)
5 node.module node_object_prepare(&$node)
6 node.pages.inc 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.

Parameters

$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.

File

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

Code

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);
}

Comments

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

<?php
$node
= new stdClass();
$node->type = 'thing';
node_object_prepare($node);

$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:

<?php
$node
= new stdClass();
$node->type = 'thing';
node_object_prepare($node);

$node->title = 'Test thing';

// ...more node properties...

node_save($node);

$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.

Warning

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

<?php
$node
= node_load($nid);
node_prepare($node);
node_save($node);
?>

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]

Work-around

If you want to use node_prepare to pull in additional node data that's not always loaded, and then save later,
Either
- clone the node object first (like token.module does
or
- 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:

<?php
    node_object_prepare
($node);
    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');