| 5 node.module | node_submit($node) |
| 6 node.module | node_submit($node) |
| 7 node.module | node_submit($node) |
| 8 node.module | node_submit($node) |
Prepares a node for saving by populating teaser, author, and creation date.
Parameters
object|array $node: A node object or array.
Return value
A validated node object with a populated teaser, author, and creation date.
File
- modules/
node/ node.module, line 833 - 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
<?php
function node_submit($node) {
global $user;
// Convert the node to an object, if necessary.
$node = (object) $node;
// Generate the teaser, but only if it hasn't been set (e.g. by a
// module-provided 'teaser' form item).
if (!isset($node->teaser)) {
if (isset($node->body)) {
$node->teaser = node_teaser($node->body, isset($node->format) ? $node->format : NULL);
// Chop off the teaser from the body if needed. The teaser_include
// property might not be set (eg. in Blog API postings), so only act on
// it, if it was set with a given value.
if (isset($node->teaser_include) && !$node->teaser_include && $node->teaser == substr($node->body, 0, strlen($node->teaser))) {
$node->body = substr($node->body, strlen($node->teaser));
}
}
else {
$node->teaser = '';
$node->format = 0;
}
}
if (user_access('administer nodes')) {
// Populate the "authored by" field.
if ($account = user_load(array('name' => $node->name))) {
$node->uid = $account->uid;
}
else {
$node->uid = 0;
}
}
$node->created = !empty($node->date) ? strtotime($node->date) : time();
$node->validated = TRUE;
return $node;
}
?> Login or register to post comments
Comments
If you're programmatically creating nodes, populate $node->name
If you're programmatically creating nodes, be sure to populate $node->name, since the block starting at "if (user_access('administer nodes'))" in node_submit() will change your $node->uid to 0 (anonymous) for all users who have 'administer nodes' permissions, including your admin user.
If you have problems when you
If you have problems when you update nodes with node_submit and node save because the created dates is change please read this post from drupal.org
http://drupal.org/node/286069
You must make this:
<?php$node = node_load($this_product_name_id);
// do some stuff here
$node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
$node = node_submit($node);
if ($node->validated) {
node_save($node);
}
?>
Thanks Berto.
Oskar