Quickly Protecting a Few Nodes from Deletion in Drupal 8

I’m working on a Drupal 8 site with some critical pages for which we do not want to allow deletion. The homepage, for example, is a basic page; we do not want to allow anyone (even UID #1) to delete this page. If there comes a time where UID #1 decides they need to, they’ll have to update this simple code to support that. We need not make it any more complicated than that for this particular project.

Here’s a solution that:

  • shows nothing but an error on the “Delete” form for specific nodes (works for all users)
  • prevents deletion by any means (works for all users except UID #1)
    • why except UID #1? because hook_node_access doesn’t run for this user

Note that the array of node IDs has labels to help the developer know which node ID represents which page. There are only three nodes and this only happens on deletion or deletion form rendering, so performance is a non-issue.  array_search() will work just fine.



Leave a Reply

Your email address will not be published.