Using Drupal Rules Scheduler to Unpublish a Node

I use the Rules module together with Rules Scheduler (a Rules sub-module) to automatically unpublish content at a specified time, as explained in the steps below. These steps use content type = article, which has an expiry date field which is set by the user. When cron runs, the expiry date field is checked and the content is unpublished.

Step 1: Create a Rules “Component”

Here is the Rules Component to be created (in Rules export format):

{ "rules_unpublish_a_node" : {
    "LABEL" : "Unpublish an Article",
    "PLUGIN" : "action set",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "node" : { "label" : "node", "type" : "node" } },
    "ACTION SET" : [ { "node_unpublish" : { "node" : [ "node" ] } } ]
  }
}

 

Step 2: Create a “Rule” using the Rules Component

Here is the Rule to be created, in Rules export format:

{ "rules_unpublish_article" : {
    "LABEL" : "Unpublish Article",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_scheduler" ],
    "ON" : {
      "node_insert" : [],
      "node_update--article" : { "bundle" : "article" }
    },
    "IF" : [
      { "node_is_of_type" : {
          "node" : [ "node" ],
          "type" : { "value" : { "article" : "article" } }
        }
      }
    ],
    "DO" : [
      { "schedule" : {
          "component" : "rules_unpublish_a_node",
          "date" : [ "node:field-expiry-date" ],
          "identifier" : "Unpublish Article with node ID [node:nid]",
          "param_node" : [ "node" ]
        }
      }
    ]
  }
}

Note that the above rule refers to the Rules Component from the previous step. It’s in Rules export format, just import it in your own environment via copy-paste (after you adapted article in it to fit the machine name of your content type).

Published
Categorized as Drupal