3

I want to prevent a specific node from being deleted by anyone except admin. For this specific bundle there are two roles that have full access to the node: admin and manager. A manager can delete any nodes of this content type, but I want one specific node to be locked.

I tried hook_node_delete and it does not work as the delete hook for the specific content type (node) has already run by then.

I tried the Content Access module and setting of permissions per node, but then when I go to the specific node's edit page the 'delete this content' for the role manager is enabled and greyed out.

I also do not want to just remove the action from the edit form as the node can be deleted in a view bulk operation. Hence I need a catch all solution.

2 Answers 2

1

I don't think there's a catch all solution.


On second though, you maybe can create a new content type, say node2, and set the permissions for that content type to admin only.

and if you will ever need more than one admin-only node, you'll have no problems at all.

however i have no experience whatsoever there :p


in hook_menu_alter change the 'access callback' for the "node/%nid/delete", since you need it only for a particular node ( say nid=120)

$items['node/120/delete'] = $items['node/%nid/delete'];
$items['node/120/delete']['access callback'] = 'custom_function';
unset($items['node/120/delete']['access arguments']);

this will prevent that node to be deleted directly.

then you need to remove the delete button from that node form edit (however i don't know the form name).

this should be enough and prevent the deletion "nicely", but you can use hook_delete like pointed out in the other answer to catch everything and everything else that can't be prevented like that.

3
  • Thanks for this answer. It definitely looks like my best option. Commented Jul 17, 2014 at 18:48
  • I added a maybe-faster solution which may work way better
    – Astrayus
    Commented Jul 17, 2014 at 18:59
  • I made a custom module and put the above code in it – but this error is thrown out: ParseError: syntax error, unexpected '{', expecting '(' in drupal_load() (line 52 of /sites/all/modules/custom/custom_admin/custom_admin.module). Exception: theme() may not be called until all modules are loaded. in theme() (line 1017 of /includes/theme.inc). What's wrong there?
    – Madam Adam
    Commented Aug 1, 2017 at 10:20
0

I haven't tested this out, so take it for what it's worth, but I would try hook_delete.

The code for that one's really simple

function hook_delete($node) {
   db_delete('mytable')->condition('nid', $node->nid)->execute();
}

I would think wrapping the delete statement in a a permissions check should get you where you need to go.

1
  • 2
    That is not how this hook works. It is just there for giving you the option of responding to a node deletion, not preventing it.
    – Chance G
    Commented Oct 6, 2014 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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