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

si_odong’s picture

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.

dreipunktnull’s picture

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.

cdesautels’s picture

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.

himynameisseb’s picture

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']".

//stop deletion of nodes and comments
function MODULE_form_alter (&$form, &$form_state) {
  //stop any node/comment deletions
  if ((isset($form['operation']['#value']) && $form['operation']['#value'] == 'delete') || $form['#id'] == 'node-delete-confirm' || $form['#id'] == 'comment-confirm-delete') {
  //set a message...
  drupal_set_message('your message');
  //stop people from being able to submit the delete form (and in turn stop the delete)
  unset($form['actions']['submit']);
  //set the "cancel" link text to something else
  $form['actions']['cancel']['#title'] = t('Back to previous page');
}

//Stop the deletion of user accounts
if ($form['#id'] == 'user-multiple-cancel-confirm' || $form['#id'] == 'user-cancel-confirm-form') {
    drupal_set_message(t('You are attempting to delete a user account.  Users on this site cannnot be deleted, to disable this users account, please block the account.  Users with blocked accounts will not be able to login.'), 'error');
    unset($form['user_cancel_method']);
    unset($form['user_cancel_confirm']);
    unset($form['user_cancel_notify']);
    unset($form['actions']['submit']);
    $form['actions']['cancel']['#title'] = t('Back to previous page');
}
karthikeyan-manivasagam’s picture

https://www.drupal.org/project/prevent_node_delete

Is a module to prevent the node from being deletion 

Prwbt