7 node.api.php | hook_node_presave($node) |
8 node.api.php | hook_node_presave(\Drupal\Core\Entity\EntityInterface $node) |
Act on a node being inserted or updated.
This hook is invoked from node_save() before the node is saved to the database.
Parameters
$node: The node that is being inserted or updated.
Related topics
5 functions implement hook_node_presave()
3 invocations of hook_node_presave()
File
modules/
node/
node.api.php, line 684
Hooks provided by the Node module.
Code
function hook_node_presave($node) {
if ($node->nid && $node->moderate) {
// Reset votes when node is updated:
$node->score = 0;
$node->users = '';
$node->votes = 0;
}
}
Comments
original node
PermalinkYou can access the original contents of the node at:
<?php
$node->original
?>
Log in or register to post comments
$node->original is undefined on node/add
Permalinkmaybe it's obvious, but $node->original will not exist for inserts.
Log in or register to post comments
Node object has to be returned
PermalinkThe example is wrong.
$node has to be returned in order to have an effect.
Log in or register to post comments
---
PermalinkWith PHP 5, objects passed as arguments for a function are passed by reference. The example is correct, since Drupal 7 requires PHP 5.
Log in or register to post comments
how to prevent saving?
Permalinkis there a way to prevent saving the node? (can't do form validate as this is not necessarily done on form submit)
Log in or register to post comments
A drupal_goto would probably
PermalinkA drupal_goto would probably short-circuit the save?
来自 https://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_presave/7