欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal7 d7 function hook_node_presave 保存之前执行动作 action 有大用 有大大用

shiping1 的头像

function hook_node_presave

7 node.api.phphook_node_presave($node)
8 node.api.phphook_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

You can access the original contents of the node at:

<?php
$node
->original
?>

maybe it's obvious, but $node->original will not exist for inserts.

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.

is there a way to prevent saving the node? (can't do form validate as this is not necessarily done on form submit)

A drupal_goto would probably short-circuit the save?

 

来自 https://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_presave/7

 

普通分类: