Updated: Comment #N

This might be a duplicate of an existing issue. Unfortunately the issue search seems to be broken atm and I can't check that.

Problem/Motivation

Currently hook_entity_delete() is sometimes called after the data already were deleted. This leads to situations in which it's impossible to do the necessary clean-up tasks because those tasks would rely on the already deleted data.
So far I know only of taxonomy and comments that invoke those deletion hooks after deleting the data. The node, user and file module already invoke the hook before deleting.

Proposed resolution

Change the order of hook invocation / data deletion in the modules taxonomy and comments.

Remaining tasks

Reviews needed.

User interface changes

none

API changes

Not really an API change, but it could change the available data in the affected hooks.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work

The last submitted patch, drupal-invoke-deletion-hooks-before-deleting-data.patch, failed testing.

das-peter’s picture

Status: Needs work » Needs review
FileSize
2.88 KB

Changed the code in the comments module to call _comment_update_node_statistics() after the data are removed. Locally the previously failing tests pass now.

das-peter’s picture

xjm’s picture

Component: other » entity system
Assigned: Unassigned » David_Rothstein
Issue tags: +Needs tests

So, hm. This is a bit of an API change, and it makes me a little nervous to consider it for D7. Let's check with David to get an idea whether this would be acceptable for D7 or not.

Also, if it can be considered for D7, it will need automated test coverage (see the D8 issue for how test coverage was added there) but let's get David's feedback first.

David_Rothstein’s picture

Assigned: David_Rothstein » Unassigned

Hm, yeah, that inconsistency is unfortunate but also looks a little dangerous.

Given that the entity is passed to the hook, it seems like most code wouldn't care whether it's in the database or not anymore (although I guess some could). Are there good examples of where this is needed?

Maybe we could consider introducing a separate "pre-delete" hook instead?

das-peter’s picture

@David_Rothstein Well I've stumbled over this issue while fixing the issue I've set as related: #2176317: Delete term related physical folder
Maybe we could consider introducing a separate "pre-delete" hook instead?
Absolutely, I think that would be a viable solution. All I need is a "delete" hook that is invoked while the data is still fully available.

nickonom’s picture

I am hitting the same issue as described on https://drupal.stackexchange.com/questions/254629

nickonom’s picture

Status: Needs review » Closed (duplicate)
Parent issue: » #1789494: Introduce a pre-delete hook like in D8

I've found another issue, which discusses introducing pre-delete hook, so I am closing this one and marking it as duplicate.

David_Rothstein’s picture

Title: Invoke hook_entity_delete() before actually deleting the data. » Introduce an entity pre-delete hook that consistently runs before the entity starts being deleted
Status: Closed (duplicate) » Needs work
Issue tags: +Needs issue summary update
Parent issue: #1789494: Introduce a pre-delete hook like in D8 »
Related issues: +#1789494: Introduce a pre-delete hook like in D8

That issue is for the Entity module, though, so it won't help with the core entities (such as taxonomy terms and comments) discussed in this issue, right? Drupal core would still need to introduce and use that hook.

nickonom’s picture

I see the difference now, David. Good point!