| 7 node.api.php | hook_node_update($node) |
| 8 node.api.php | hook_node_update($node) |
Respond to updates to a node.
This hook is invoked from node_save() after the node is updated in the node table in the database, after the type-specific hook_update() is invoked, and after field_attach_update() is called.
Parameters
$node: The node that is being updated.
Related topics
12 functions implement hook_node_update()
File
- modules/
node/ node.api.php, line 696 - Hooks provided by the Node module.
Code
<?php
function hook_node_update($node) {
db_update('mytable')
->fields(array('extra' => $node->extra))
->condition('nid', $node->nid)
->execute();
}
?> Login or register to post comments
Comments
Don't be fooled
Into reading the above and thinking that your node has already been saved. I ran into a situation where I was doing a node_load on the nid of the passed node, which would provide me with the OLD values - the ones that should have been replaced before this hook was invoked.
function hook_node_update($node) {
// Provides the "updated" values
dpm($node);
$loaded_node = node_load($node->nid);
// Provides the old values, which shouldn't be there according
// to the documentation above
dpm($loaded_node);
}
node_load cache
node_save() doesn't clear the cached node until later, so you're still getting the cached one. This is by design.
What if another module clears
What if another module clears the cache before your hook is executed? Then you get the new version instead of the old one. Is there a method that guarantees you get the previous version of the node?
I got fooled
I just spent hours getting tripped up by this one, it should really be mentioned in the description.
FIX FOR FUTURE READERS: Call
entity_get_controller('node')->resetCache(array($node->nid));tnx
tnx, it does really work)
Use $node->original
On hook_node_update the old values are saved in $node->original.
More Than Once
What if you want to create an entity on node update. Or write a log record. Some modules (like Rules or CNR) can update node many times after actual update.
For example a user saves his article and he can see several records in the log (provided by custom module). But he have updated the node only once.
And the question is... How can I differentiate programmatically updated node from user updated node?
Ideally, you can use
Ideally, you can use subversion to install and update your Gallery 2 installation directly on the server.But this option is only available if you have command line access to the server (ssh/telnet).