Drupal Association members fund grants that make connections all over the world.
This tutorial will give you an example how to use the Rules Scheduler module.
Additionally to Tutorial 2: download and install CCK and Date and setup a content type with a date field, that will serve as publishing date (set the Label to "Publishing Date"). See the documentation of those modules if you are not quite sure how to use them.
Creating a rule set as scheduling action
We can re-use the rule set from Tutorial 2, so refer to that example if you haven't created it yet.
Creating a triggered rule
- Go to "admin/rules/trigger" and add a new rule.
- The process will be very similar to the example in Tutorial 2.
- The rule label should be something like "Schedule publishing by date field" and the event must be "After saving new content".
- Add a condition "Content has type" to verify that we only act on our previously added content type.
- Now add an action, select "Schedule Publish content" and click "Next".
- Fill out the "Identifier" field with something like "node [node:nid]".
- Here comes the tricky part: fill out "Scheduled evaluation date" with something like "[node:field_publish_date-datetime]" where "field_publish_date" is the identifier of your date CCK field.
- You can lookup the available replacement tokens from the fieldset "Replacement patterns for saved content" above. Click "Save".
- Create new content from your previously defined content type with the date field.
- When you save it, the rule will be executed.
- Go to the scheduling page ("admin/rules/rule_sets/scheduling") and look at the list of scheduled tasks, your node should be listed with the scheduling date you entered during node creation.
Cloning the rule to also affect updated content
The rule from above only works for newly created content, but we want it on updated content, too. If the date value is updated, then also a previously scheduled task with the same user provided identifier (the node id in our case) should get updated to the specified date. We will add a condition to only affect publishing dates in the future (to avoid endless rescheduling loops). You need to enable the PHP filter module from Drupal core.
- Go to triggered rules page ("admin/rules/trigger") and click on "clone" near your rule from above.
- Change the event from "After saving new content" to "After updating existent content".
- Change the label of the rule by adding something like "on updating"
- "Save changes".
- Add a condition "Numeric comparison", set Number 1 to "[node:field_publish_date-timestamp]" (the token corresponding to your date field), the Operation to "Greater than" and Number 2 to "<?php echo time(); ?>". Click "Save".
- Now go back to your already existing content and edit the date field.
- Visit the scheduling page ("admin/rules/rule_sets/scheduling") and observe the updated scheduling date of your node.
Note: Rules Scheduler saves dates in GMT/UTC internally, but displays them in your timezone setting in the scheduling list. So be aware that the content of the date field is interpreted as UTC/GMT.
Deleting scheduled tasks
If you want to remove scheduled tasks, you either can do it manually on the scheduling page, but you can also use a Rules action to do it automatically. Let's say we want to cancel/delete scheduled tasks for a node, if the node gets deleted.
- Add a new triggered rule with the event "After deleting content"
- Add an action to to this rule "Delete scheduled rule sets" from the Rules Scheduler section
- Fill out the identifier field with "node [node:nid]" (Note: only scheduled tasks with exactly the same identifier match, so be careful in assigning them)
- Leave the Rule set box empty (we want delete tasks related to any rule set) and "Save"
- Do some testing: watch a scheduled task on the scheduling page, then delete the related node, then visit the page again. The task(s) have been deleted.
Another note: Deleting scheduled tasks for nodes that do not exist anymore is not mandatory, if a task is executed on a disappeared node, it gets simply canceled by the Rules system and does not break your site.