How many times drupal calls hook_node_presave for a node? 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...

  $counter++;
  watchdog('mymodule', "hook has been called $counter");
}
Once.

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