In my web application, I would like to prevent accidental or unauthorized deletions or modifications (e.g. a student steals teacher's credentials and deletes or changes bad marks). But, at the same time, I'm trying to keep the interface as easy and straightforward as possible.

I've already enabled node revisions, so if a mark is changed we can restore the previous state, but I also hid the revision message, because it confuses non-techie teachers.

The next step would be transparently prevent mark deletion. The user should see and use the "Delete" button and the deletion confirmation page, but when confirming, Drupal should not delete the node and just unpublish it. Is this possible to do?

(I would then create a view to see/administer the revised/unpublished nodes, to check unauthorized modifications)


Use hook_node_delete . This hook is being invoked right before node deletion.

function YOURMODULE_node_delete($node) {
  if ($node->type == 'NODETYPE') {
    $node->status = 0;
  • So, unset($node);, by unsetting the node variable, prevents the node being removed from the database? – user1527576 May 4 '17 at 10:30
  • yes, this way the argument (object $node) is not being passed to hook_entity_delete() which makes actual deletion, so nothing actually happends – laniol May 4 '17 at 11:18
  • I speaked too early. This method doesn't work, the node will still be deleted, so I'm reopening the question! – user1527576 Jun 6 '17 at 20:40

I fixed the unset() problem using a redirect

$node->status = 0; node_save($node); drupal_set_message(t('The node has been deleted.')); drupal_goto('node/'); exit();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.