How to prevent node deletion (cancel delete)

dreipunktnull - June 30, 2009 - 13:31

Hi,
I have a module with several custom node types that can be referenced to each other via nid. How can I prevent deletion of a node that is currently being referenced by another? I tried hook_delete() and hook_nodapi() but was only able to detect the dependency and to warn the user about it. I could use hook_access() for this but would then not be able to change the error message, right?
Thanks in advance for your hints.
Best regards,
Björn

which error message do you

siodong - July 1, 2009 - 02:37

which error message do you mean? i think you can still use hook_access() and set a drupal_set_message in your hook and return FALSE. but the next problem is, you should find which hook_access() called and return a page / an error message.

I mean the default 'Access

dreipunktnull - July 1, 2009 - 09:26

I mean the default 'Access denied' message that is shown when hook_access() returns FALSE. Which one is called should be under my control as it's a custom node type. I'll give it a try. Thanks for your reply.

Edit: I tried hook_access() and it worked...kind of. The hook is obviously fired for each $op value even when editing the node so my error message would pop up before trying to delete a node. The latter is also not be possible because when hook_access() returns FALSE for $op == 'delete' the delete button would not be rendered.

Another solution

cdesautels - November 25, 2009 - 20:15

I accomplished this by using hook_form_alter to intercept the deletion confirmation form and replace it's message and submit button with a message saying that the node could not be deleted. Then I just wrapped this in the logic to limit it's application. In my case a specific set of nodes.

 
 

Drupal is a registered trademark of Dries Buytaert.