1

I need to respond to an update event and regenerate a file, but when the node is deleted, the file should also be deleted. However, the problem is, everytime a node is deleted, the hook_node_update is ALSO called, which regenerates the file.

How can I differentiate between a node being updated (NOT deleted) and a node being deleted?

1
  • I just added a basic hook_node_update and hook_node_delete to a fairly clean install and I am only seeing the update print when I update a node and the delete firing on delete (with no update). Do you have any custom validate or submit functions that might be causing the hook_node_update to fire? Can you recreate this issue on a clean install? Feb 4 '15 at 21:48
4

Thanks for your answers, much appreciated!

I finally managed to find the cause. The "problem" is caused by the module https://www.drupal.org/project/field_collection. My node has a field that is a field_collection, and apparently, when deleting a node, the field_collection items are first deleted from the node, which is then saved (which causes the update hook to fire), and only then the node itself is actually deleted.

It's pretty easy to workaround this issue once you actually KNOW what is causing it, in my case I modified the update hook to check for field_collection items in the node, if there were none it means the node is about to be deleted...

1

Following are the set of hooks which are invoked from node_delete function, which shows update hook will not be invoked.

    node_invoke($node, 'delete');
    module_invoke_all('node_delete', $node);
    module_invoke_all('entity_delete', $node, 'node');
    field_attach_delete('node', $node);

Make sure you are not updating any other node when action is delete of node, something like

   function my_module_node_delete($node) {
     node_save('any_node_id');
   }
0

No more workarounds needed. This issue has been fixed in the latest dev version of Field collection: https://www.drupal.org/node/2385985

0

Good question and good point!

But this is not just apply when you have field_collection but also any attached entity may have the same effect (hook node update may be called when field_attach_delete is called in node_delete_multiple function), this can include the attached entity files (if you enabled file entity for example) and referenced nodes when you have chosen the option for deleting a parent node delete a child node!

Drupal automatically first delete attached entities and save the node each time (and hook node update will be called) and then continue the process (for example if you three field_collection fields, you will have three calls of hook node update (each time it deletes a field_collection from your node)!

In my case, my hook node update must react to deletion of a field_collection and I can't find any simple solution to determine the difference between an update of the node (when it is deleting a field_collection) and deleting the node (when again first it is deleting a field_collection and call node update)!

Anyway great question and mentioning an important point!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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