1 node_hooks_example.module | node_hooks_example_node_update($node) |
Implements hook_node_update().
As an existing node is being updated in the database, we need to do our own database updates.
This hook is called when an existing node has been changed. We can't simply update, since the node may not have a rating saved, thus no database field. We first check the database for a rating. If there is one, we update it; otherwise, we call node_hooks_example_node_insert() to create one.
Related topics
File
- modules/
examples/ node_hooks_example/ node_hooks_example.module, line 187 - Hook implementations for the Node Hooks Example module.
Code
function node_hooks_example_node_update($node) {
if (config_get('node_hooks_example.settings', 'node_hooks_example_node_type_' . $node->type)) {
// Check first if this node has a saved rating.
$rating = db_select('node_hooks_example', 'e')
->fields('e', array('rating'))
->where('e.vid = (:vid)', array(':vid' => $node->vid))
->execute()->fetchField();
if ($rating) {
// Node has been rated before.
db_update('node_hooks_example')
->fields(array('rating' => $node->node_hooks_example_rating))
->condition('vid', $node->vid)
->execute();
}
else {
// Node was not previously rated, so insert a new rating in database.
node_hooks_example_node_insert($node);
}
}
}