Currently it's quite hard to write a module that deletes nodes of a user when the user gets deleted. This is, because node_delete() depends on node_load(), which depends on an existing entry in the user table - but the user module deletes this entry *before* it invokes the user hook.
Attached is a simple patch, which does nothing more, but invoking the hook *before* deleting the entries in the user tables.
I ran over this while working on content profile. I have already working code without this patch - ported from d5, but it's a complicated, ugly workaround, that basically re-implements node_load() and node_delete().
This patch would allow me to replace this (long) hack by some simple code. Actually it would save about 90 loc! (related content profile patch
Comment | File | Size | Author |
---|---|---|---|
#9 | user-delete-hook-218189-9.patch | 989 bytes | cburschka |
drupal_6_user_delete.patch | 946 bytes | fago | |
Comments
Comment #1
fagoComment #2
fagoComment #3
catchThis is a bug.
Comment #4
moshe weitzman CreditAttribution: moshe weitzman commentedquite reasonable.
Comment #5
Gábor HojtsyI checked Drupal all the way back to 4.7's user_confirm_delete_submit() including 5.x's user_delete() and this hook call was at the end all the time. I am not going to change a long time established API in the last minute, sorry. I can see good use for this in 7.x though.
Comment #6
fagoThat's bad. So we have to keep the ugly workarounds... :((
I know that this is the behaviour for quite a long time - but that doesn't make it better :(
I also know it's late for 6.x - sry.
Comment #7
fagoso, what is about 7.x?
Comment #8
cburschkaRe-roll.
Note that this patch will collide with the PDO monster. I suggest revisiting it after that is committed, because this one is far easier to reroll.
Comment #9
cburschkad.o. is still randomly swallowing attachments.
Comment #10
KarenS CreditAttribution: KarenS commentedWhy not add a new 'pre delete' hook instead? Then you don't break long-standing code and this becomes something that could be back-ported. Plus there are times when you might *want* a hook that is triggered after the user has been deleted, since that is the way it has always worked.
Comment #11
pushyamitra CreditAttribution: pushyamitra commentedYes, I agree. It would be nice to have a 'pre delete' hook.
Also, it would be great if acts like 'validate' operation, and user doesn't have to be deleted if this hook registers an error.
This way a module could do some validation, and _not_ delete user if some validation fails.
Comment #12
fago@pushyamitra: hm, this won't cover my use case as then I don't know if the user is really deleted. So only a real 'pre delete' would help here.
Comment #13
moshe weitzman CreditAttribution: moshe weitzman commentedI think the most recent patch is useful and consistent with core. Validation is a different matter and doesn't cover fago's original use case.
Comment #14
leanazulyoro CreditAttribution: leanazulyoro commentedI'm just learning the more advanced aspects of drupal, i'm creating a module and i came up with this, i have to delete nodes that a user submmited when the user is deleted. fago, when you created this issue you said that "currently it's quite hard to write a module that deletes nodes of a user when the user gets deleted" but you didn't say it's imposible... is really a way to do this? i mean without patches any patches. the module nodeprofile does it and i tried with no success to copy the exact same method that is used there. again: is there a way to do this?
by the way, my drupal version is 5.9
Comment #15
Dries CreditAttribution: Dries commentedI didn't break any tests so I've committed this to CVS HEAD. Thanks all.
If this patch breaks existing modules, I'd like to better understand their use case.
Comment #16
Anonymous (not verified) CreditAttribution: Anonymous commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #17
mistergroove CreditAttribution: mistergroove commentedIn my humble opinion wouldn't it be better if there was a pre and post hook for all major user operations (delete, load, save)? There are many situations you might want to run your delete or save etc code before and after the operation. This would save a lot of people juggling with module weights also. I'm a relative noob to the drupal so if theres reasons for not having these please excuse my ignorance.
Comment #18
NancyDruI'm sorry, but I have to re-open this and mark it critical against 6.x. My site and module is completely broken because of failed deletes. This needs to go back to D6 at least.
Comment #19
Dublin Drupaller CreditAttribution: Dublin Drupaller commented+1 for implementing this. I too have a module that needs a pre-delete user hook.
if it's possible, can the developer (or any other developers who have become stuck by this) who marked this as ignored indicate a workaround?
cheers
dub
Comment #20
sphism CreditAttribution: sphism commentedYeah i've just come up against this problem:
I can't patch core (it's a big project). So i'm not entirely sure what to do here... any suggestions?
Comment #21
KarenS CreditAttribution: KarenS commentedGabor doesn't want to alter existing behavior in D6, which I understand since that could unexpected break other things. My suggestion in #10 still stands, we could add a second hook that will be invoked before the user is deleted and any modules that need that can use it.
I'll take another stab at getting Gabor to look at this idea even though it is an API change because it is a critical issue and there really is no way to do it other than hacking core.
Comment #22
NancyDru@Karen: That would work for me. And I do understand Gabor's point of view, and agree with him. For me at least, this is no longer critical because I lost that account, partially because of this issue. However, we could use this in the Web Links module, which does have an option to take over a deleted user's content.
Comment #23
catchDowngrading to 'major'.
Comment #24
hargobindSince neither the patch above or the "pre delete" operation was ever implemented in 6.x, I'm writing this note to link people to a possible workaround to handle "pre delete" actions for anyone else who finds this thread.
I've made a comment about this behavior in the API page for
hook_user()
. That comment also includes a workaround usinghook_form_alter()
.