function hook_node_presave

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()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

book_node_presave in modules/book/book.module
Implements hook_node_presave().
entity_crud_hook_test_node_presave in modules/simpletest/tests/entity_crud_hook_test.module
Implements hook_node_presave().
forum_node_presave in modules/forum/forum.module
Implements hook_node_presave().
node_test_node_presave in modules/node/tests/node_test.module
Implements hook_node_presave().
trigger_node_presave in modules/trigger/trigger.module
Implements hook_node_presave().
1 invocation of hook_node_presave()
node_save in modules/node/node.module
Saves changes to a node or adds a new node.

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

You can access the original contents of the node at:

<?php
$node
->original
?>

The example is wrong.
$node has to be returned in order to have an effect.

With PHP 5, objects passed as arguments for a function are passed by reference. The example is correct, since Drupal 7 requires PHP 5.