During debugging my code I noticed that hook_node_presave() is called 3 times when I edit a node. Why?

I use the following code to count how many times the function is called:

function mymodule_node_presave($node) {
  static $counter = 0;

  // Some code...

  watchdog('mymodule', "hook has been called $counter");

1 Answer 1



If it's called multiple times then you your node is saved multiple times.

This might be because you have some rules or other code that e.g. resaves the node in a hook_node_insert()/update().

To find out what and where, enable devel.module if you haven't already and add a ddebug_backtrace() call to your hook. That will give you the backtrace on how your function was called and you can identify who is calling node_save().

  • I added ddebug_backtrace() to mymodule_node_presave() . It shows that only this hook(mymodule_node_presave) is called 3 times.
    – hknik
    Commented Mar 13, 2012 at 7:03
  • Yes, and each of the output elements is a expandable krumo thingy that shows the full backtrace of each call. Click on it and post it here, if necessary.
    – Berdir
    Commented Mar 13, 2012 at 7:31
  • @Berdir OT : It happens for init hook as well when debugging but if i print any text it prints only once , how can this be explained
    – GoodSp33d
    Commented Mar 13, 2012 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.