Documentation states:

> This hook is invoked from node_delete_multiple() after the node has been removed from the node table in the database, after the type-specific hook_delete() has been invoked, and before field_attach_delete() is called.

However, as #921944: error messages on tracker page - tracker_node_delete() does not correctly act on node deletion shows, the above is not true. The following code in tracker module still retrieves a node:

$node = db_query('SELECT nid, status, uid, changed FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();

The code in node.module confirms this:

        // Call the node-specific callback (if any):
        node_invoke($node, 'delete');
        module_invoke_all('node_delete', $node);
        module_invoke_all('entity_delete', $node, 'node');
        field_attach_delete('node', $node);

        // Remove this node from the search index if needed.
        // This code is implemented in node module rather than in search module,
        // because node module is implementing search module's API, not the other
        // way around.
        if (module_exists('search')) {
          search_reindex($nid, 'node');
        }
      }

      // Delete after calling hooks so that they can query node tables as needed.

Marking as major as this has implications for all implementing modules, and I've no idea which of the code or docs is correct...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

marcingy’s picture

The code is correct the change was introduced here http://drupal.org/node/890790

mr.baileys’s picture

Component: node system » documentation
Status: Active » Needs review
FileSize
763 bytes

Doc update.

marcingy’s picture

Status: Needs review » Reviewed & tested by the community

I'm happy to mark this rtbc.

jhodgdon’s picture

Status: Reviewed & tested by the community » Needs work

This patch is OK but incomplete.
http://api.drupal.org/api/drupal/modules--node--node.api.php/group/node_...
needs to be updated as well with this information.

jhodgdon’s picture

Title: does hook_node_delete() have a node or doesn't it? » Document correct sequence of hooks in node deletion
Priority: Major » Normal

Also, the node API page needs to mention that hook_entity_delete() is also called, apparently.
http://api.drupal.org/api/drupal/modules--node--node.module/function/nod...

Are there other entity hooks that have been added to other operations on that page too?

Anonymous’s picture

Assigned: Unassigned » linclark

I'll have a new patch for this shortly.

Anonymous’s picture

Version: 7.x-dev » 8.x-dev
Status: Needs work » Needs review
FileSize
2.59 KB

This patch builds upon the previous patch, which changed the order of the functions listed in the comment for hook_node_delete.

It adds hook_entity_delete to the comment for hook_node_delete and moves the reference to node deletion in the larger list of function calls for hook_node_delete, as suggested by jhodgdon.

It also adds to the large list the following hook invokations, which were missed before.

  • hook_entity_presave in node_save
  • hook_entity_view in node_build_content
  • hook_entity_view_alter in node_view

For some reason, "Viewing multiple nodes" had hook_node_view_alter listed and "View single node" did not. Since hook_node_view_alter is called from node_view, I assume that both should have the alter hooks listed.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Excellent work! Let's get this into d8 and d7 please. Thanks linclark and mr.baileys.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 7.x and 8.x.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.