Drupal node_save action firing in a hook (gotcha)

Recently while developing a website for a client I ran into this issue which is so simple, yet the effects so devastating. First a bit of background. I was trying to use a hook that is fired by an existing action to update some fields on the same node after the action has occurred.

This hook was firing after the node had been saved, so it should work. Yet after the initial save the node in question become uneditable, you could no longer update any fields on it or publish it. The issue is that when I loaded the saved node so I could update it, I did not reset the node’s cache so the existing node’s value always overrode any values in the future.

The effects of this problem are that after you initially submit an item you can no longer update it in any way. The same fields are written to the system no matter what. The solution is simple.

When you load the node use the $reset variable on it.

So instead of:

node_load($nid);

node_load($nid, null, true);

That’s it, not really much more to it then that. You should consult the API in Drupal for more information.

Published on: 5 October 2012
Posted by: Sami K.