| 5 node.module | node_object_prepare(&$node) |
| 6 node.pages.inc | node_object_prepare(&$node) |
| 7 node.module | node_object_prepare($node) |
| 8 node.module | node_object_prepare(Node $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.
1 call to node_object_prepare()
File
- modules/
node/ node.module, line 937 - 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);
}
Login or register to post comments
Comments
node_save() does not return
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...
Beware menus getting deleted
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:
<?phpnode_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'];
}
?>