Rule to stop a node being deleted..

Events happening in the community are now at Drupal community events on www.drupal.org.
wipeout_dude's picture

Hi,

I need a rule to stop certain nodes being deleted based on the value of a field..

So as an example..

Event : Before node is deleted
Conditition : field1 = 1234
Action : Don't delete the node

Obviously (as far as I am aware) these events and actions don't exist..

Anyone have any ideas how to achieve this functionality?

Thanks..

Comments

I guess you could replace the

mitchell's picture

I guess you could replace the UX of "deleting" with another action that flags it as "deleted" or checks a "deleted" field.

This module comes to mind for that type of route: https://drupal.org/project/publish_button

For reference, here's the event issue for a delete hook: https://drupal.org/node/366790

Thanks mitchell.. I guess

wipeout_dude's picture

Thanks mitchell..

I guess creating a "delete" field might be the most simple work around..

Something like..
Event : After updating existing content
Condition : "deleted" field = yes
Action : Delete entity

Might give that a try..

Before node is deleted

zhangtaihao's picture

A "Before node is deleted" event is not achievable until D8:

D7 Node API Hooks:
http://api.drupal.org/api/drupal/modules%21node%21node.api.php/group/nod...

D8 Node API Hooks:
http://api.drupal.org/api/drupal/core%21modules%21node%21node.api.php/gr...

However, you could try using Rules Forms Support to hide the "Delete" button conditionally.

Simple rule only for UI

theodorosploumis's picture

If you want to prevent that only on UI (mean not through a batch proccess) you can redirect users when path is node/[nid]/delete and optionally set a message or disallow access for this path using php hook.

In any case the Event will be "Drupal is initializing" and Condition will be "Data: Text Comparison -> Current page path" or a php code that checks "node/[nid]/delete" path.

Attaching an example rule:

{ "rules_prevent_node_delete" : {
    "LABEL" : "Prevent node delete",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "delete", "node", "permissions" ],
    "REQUIRES" : [ "php", "rules" ],
    "ON" : [ "init" ],
    "IF" : [
      { "php_eval" : { "code" : "
          $arg0 = arg(0); // node
          $arg1 = arg(1); // nid
          $arg2 = arg(2); // delete
          return ($arg0 == 'node' && $arg2 == 'delete');" }
      }
    ],
    "DO" : [
      { "redirect" : { "url" : "admin\/dashboard" } },
      { "drupal_message" : { "message" : "This content cannot be deleted.", "type" : "warning" } }
    ]
  }
}

Similar discussion on stackoverflow.

Drupal 7, Drupal 6, Drupal anyway...

Prevent deleting on the drupal way

reinaud's picture

This could be helpful! if you do a redirect like this:

  if($dontdelete) {
      drupal_set_message('Your Message');
      drupal_goto('/admin/content');
   }

https://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_delete/7

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: