By dreipunktnull on
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
Comments
which error message do you
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
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$opvalue 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 whenhook_access()returns FALSE for$op == 'delete'the delete button would not be rendered.Another solution
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.
Done via hook_form_alter
I have achieved the ability to stop people deleting a node and provide information via a message in hook_form_alter. My solution is for Drupal 7, however it could be applied to Drupal 6. (Just remember to check form item naming.)
In the example below, for any delete confirmation page for nodes or comments (bulk or single), I am stopping users being able to submit the confirm delete form.
In the case of the initial question, you could get the node id from the form, then run a node_load($nid). Check the results and unset the "confirm" button dependent on the results.
REMEMBER THOUGH: nodes can also be deleted as a call back from a user delete. In my example I am stopping the ability to delete users. If you want to allow user deletes, you would have to remove the option for "cancel user account and delete content" by unsetting the relevant array item in "$form['user_cancel_method']".
Prevent Node delete module
https://www.drupal.org/project/prevent_node_delete
Is a module to prevent the node from being deletion
Prwbt