hook_node_update

7 node.api.php hook_node_update($node)
8 node.api.php hook_node_update(Drupal node Node $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

function hook_node_update($node) {
  db_update('mytable')
    ->fields(array('extra' => $node->extra))
    ->condition('nid', $node->nid)
    ->execute();
}

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)

Seems like a bug

I ran into this, and I'm pretty sure it's a bug and not by design.

(Nodes already have $node->original when they're passed to this hook if you need access to the pre-saved version. But calling node_load() should really always return something that reflects the current state of the database, and it seems like a bug if it doesn't.)

This is being discussed in the issue queue at http://drupal.org/node/221081. It looks like it's on its way to getting fixed for Drupal 8 (as part of other conversions that are happening there). For backwards-compatibility reasons I'm not sure if it can be fixed in Drupal 7, but it's at least worth discussing in that issue.

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

Worst case scenerio you could

Worst case scenerio you could always use a static variable to track the number of times it has been called and only fire your event on the first node_update().

See Example #4 & #5: http://php.net/manual/en/language.variables.scope.php

Login or register to post comments