Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The pattern in core is to invoke hook_entity_delete() before entities are deleted.
See for example http://api.drupal.org/api/drupal/modules!node!node.module/function/node_....
Marking as major as this can surely cause DX problems.
Comment | File | Size | Author |
---|---|---|---|
#12 | entity-1789494-12.patch | 1.92 KB | kristiaanvandeneynde |
Comments
Comment #1
awm CreditAttribution: awm commentedNot sure if this is what you meant, but I took a stab at it.
Comment #2
Mile23I'd say leave the invocation outside the try block, so that everyone knows who threw the exception.
Also, #1007830: Nested transactions throw exceptions when they got out of scope (from the comments in that function) is fixed, but another one started: #1803886: PDOException: Syntax error or access violation: 1305 SAVEPOINT savepoint_1 does not exist I'd say transactions are unneeded here, though they might be cool and all.
Comment #3
kristiaanvandeneyndeJust ran into this as well.
When trying to use entity metadata wrappers that reference the old entity in a hook_entity_delete(), you can get exceptions because the EMW tries to load an entity that no longer exists.
New patch attached, marking as critical because this is unexpected and potentially site breaking behavior.
Comment #4
kristiaanvandeneyndeBump
Comment #5
kristiaanvandeneyndeCan we get this in please? It's breaking part of the Group module.
In this case, there is a parent-child relationship. When the parent is deleted, the children are retrieved from the database by a conditional entity load (by 'parent' property). When Group tries to decide whether the children need to be deleted along with the parent, it tries to access the parent through an EMW.
Result: WSOD because the parent has already been deleted and the EMW can no longer access it.
That's just one use case, I can see plenty situations where this behavior causes WSODs or otherwise unexpected results.
Comment #6
Mile23Patch in #3 still applies. Let's see what the testbot says.
Comment #8
Mile23Testbot is happy green.
What else needs to happen here for a fix?
Comment #9
kristiaanvandeneyndeA commit.
Comment #10
lias CreditAttribution: lias commentedBump... will this be committed?
Comment #11
fagoIf I remember it correctly, this wasn't unified in core and entity.module does it the same way with d8 now which has another pre-delete hook. Moreover, I do not see any reasony why we should do an API break like this now?
Thus marking as won't fix. If you want, you can re-open it to introduce the pre-delete hook in d7 also?
Comment #12
kristiaanvandeneyndeYes please! This has been causing WSODs in the Group module for quite a long time now.
I didn't put anything in entity.api.php because the other hook_ENTITY_TYPE_action hooks aren't in there either and the hook_entity_ACTION hooks are generally documented in system.api.php.
If this gets in, would you mind releasing a new version soon? Then I can state that version as the minimum dependency for my module.
Comment #13
sic CreditAttribution: sic commentedthanks for the patch. I am trying to get this to work with commerce_order. Apparently the commerce orders get deleted like so:
entity_get_controller('commerce_order')->delete($order_ids);
Wouldnt the predelete hooks get invoked using the supplied patch from above?
Comment #14
kristiaanvandeneyndeIt would get invoked, yes. However, you'd need to move your code from:
* hook_entity_delete() to hook_entity_predelete() or
* hook_ENTITY_TYPE_delete() to hook_ENTITY_TYPE_predelete()
Comment #15
awm CreditAttribution: awm commentedSo I am trying to understand what's the difference between 'delete' and 'predelete'. cannot we not do the same operation in hook_delete?
Comment #16
kristiaanvandeneyndeNo, the difference is huge.
Predelete is ran when the entities still exist, much like 'delete' for some core entities. However, as fago mentioned in #11: Not all core entities behave this way; some run their 'delete' after the entity has been deleted.
In Drupal 8, we have 'predelete' and 'delete' properly implemented for all entities. Therefore, the patch in #12 adds a 'predelete' hook to mimic D8 and to make sure no existing hook_entity_delete() implementations break all of the sudden.
A very important consequence of adding hook_entity_predelete is that people may now clean up using an EntityMetadataWrapper (EMW) without getting a white screen of death (WSOD). Previously, the EMW would try to load the entity again, which would fail because it had already been deleted.
Comment #17
kristiaanvandeneynde@fago: If I build a branch of Group that uses this new hook and it works, would that be enough to set this issue to RTBC myself?
Comment #18
AjitSThis has already been fixed:
public static function ConfigEntityBase::preDelete
.Comment #19
AjitSFacepalm!
Comment #20
alexmoreno CreditAttribution: alexmoreno commentedshould this be closed, as it's been already done somewhere else?
Comment #21
alexmoreno CreditAttribution: alexmoreno commentedfeature already done