This tutorial will give you an example how to use the Rules Scheduler module.

Preparations

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".

Testing

  • 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.

Comments

fp’s picture

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.

If you want to avoid timezones surprises you might find it useful to use as the Scheduled evaluation date token something like [node:field_publish_date-value] rather than [node:field_publish_date-datetime]. The latter might be transformed by a timezone setting and rules will store it as if it were GMT/UTC time thus throwing off the scheduling by whatever difference there is between the timezone and GMT.

letapjar’s picture

contrary to the above post, changing between date-value and date-timestamp does not seem to have any effect on when the rule is scheduled (Rules 6.x-1.3).

In my application I set the date input format for publishing to be 12/07/2010 - 8:00am -05:00
(i.e. dates stored as utc by compensating for the site's timezone)

I left the timezone handling for the date as site's time zone.

One tricky thing - If you are going to make a rule to both new content and updated content - the updated content trigger could fire when a new node is saved if the conditions are not set correctly.

Try adding the following condition for the updated content rule:

if "updated content's field 'field_your_cck_date_field_name' has changed

This guarantees that the rule only fires if an update actually changes the values of the date field in question.

Before adding this in I was having a problem where, on newly created nodes - rules scheduler was dropping the time portion of the scheduled date because both the new content saved and the content updated rules were firing .

Apfel007’s picture

Hi there,
this scheduling is not working with OG node-types. The node is loosing it's group during publishing!!!!
Did some one solve scheduling with OG node-types?

Apfel007’s picture

no one did this before?

davidek’s picture

How can I use this kind of expressions: [node:field_publish_date-datetime]

In the tutorial it says you can look that up under "Replacement patterns for saved content", but I cant find it. (Using the german translation of Drupal 6)
Where can I get documentation for that?

Or is this, what you use the Token module for?

Edit: It isnt displayed because of a bug: http://drupal.org/node/390482#comment-2819634

jdln’s picture

Under ‘Creating a triggered rule’ ‘datetime’ is used but under ‘Cloning the rule to also affect updated content’ ‘timestamp’ is used.

Is this a mistake? Ive tried both variations for ‘Cloning the rule to also affect updated content’ but it doesn't work for me while the first part of the tutorial works fine.
Thanks

EDIT-
Just realise. Updating the date of new content does work, i was updating the date events that id made before these rules.

I used ‘timestamp’ like in the tutorial.

Rilla Creative’s picture

I know the custom action works and I've called it through Rules manually and it works fine. But when I schedule it, it doesn't fire off even though the ruleset gets called.

Here is my previous post about it. http://drupal.org/node/833178

Anyone have any idea why this isn't working for me?

Tom

jdln’s picture

Newbie suggestion but have you run the cron?

clashar’s picture

Very good tutorial, thanks for publishing it.

I would like to see an option for condition, so that while updating content the rule get fired only if a CCK date field changed. Because currently it seems that even if I update a node changing any other information than CCK date, anyway rule is getting fired.
And it is unnecessary.

Any hint for implementing this condition?

OneTwoTait’s picture

[node:field_publish_date-datetime] uses the from date. What if we want to use the to date instead?

letapjar’s picture

If you look in the token replacements, it shows that for the to dates you just insert a "-to-"
between your field identifier and the format you want.

e.g. [node:field_publish_date-to-datetime]
^^- notice inserted "-to-"....^^^

salientknight’s picture

Using -to-datetime rules are scheduling using from value. Is this a known bug?

I am actually getting returns that are datetime -1 day

I solved this by passing

+1 day, [node:field_application_date-to-datetime] Schedules an event for the day in the field at 00:00:00

fehin’s picture

Subscribing

Jo_’s picture

I really want to do something similar with Rules 2, just unpublish content based on a CCK field - but the docs for Rules 2 are minimal and I can't work out how to do it. The component types and configuration of scheduling is confusing. Has anyone done this sort of thing in Rules 2 who could provide a similar tutorial?

Hardrocker’s picture

Would I be able to use schedule loops with this scenario as outlined by "Tutorial 4: Publish and unpublish content regularly on some week days (scheduling loops)?" My problem is getting nodes to publish at a specific time rather than 24 hours and explained "Tutorial 2: Publish content exactly 24 hours after it was created". Considering the method outlined above will let you publish based on date and time would it be able to repeat the following week?. Please advise.

skobe’s picture

hi,

i tried to extend this tutorial with the scheduled unpublishing of a node, but it doesn't work. I simply followed the steps above twice, with some modifications in the second one which is for unpublishing.
The only differences in the setup for unpublishing are the identifiers of course and the scheduled evaluation date in the triggered rule which says: [node:field_publish_date-to-datetime] for the unpublishing procedure.

So both triggered rules are triggered by "After saving new content". By the condition of having a certain content-type.

Any ideas why this couldn't work?

Thanks in advance,
skobe

alb’s picture

I tested that only after activate cron the node is published; is so?
rules depending by cron?

jami3z’s picture

Please someone provide a guide on how to do this in Drupal 7 with Rules 2!

chintan4u’s picture

In D7 I am not getting "Schedule Publish content" and "Scheduled evaluation date" actions....?

-
Chintan Umarani
Drupal Developer
www.umarani.com

ace11’s picture

Schedule content publish using date field in Drupal 7

Creating Scheduled content type

  • Create new content type and go to Publishing options and uncheck Published. This automaticly sets all created content to unpublished!
  • Add fields you want and also add date field for schedule time. New field type Date and I used Widget Text field.
  • Save your new content type.

Creating Rule Component

  • Go to admin/config/workflow/rules/components and add new component
  • Set Component plugin: Action set - and hit continue
  • Name: Publish action (or something you want)
  • Set Data Type as Node - Enter Label and machine name. Example for Label: Node to publish and for Machine Name: node_to_publish. And then hit continue
  • Add action and choose under Node Publish Content. And hit continue
  • Data selector - click on it and choose node-to-publish - which is machine name you created earlier. And hit save

Creating Rule

  • Go to admin/config/workflow/rules and add new rule
  • Give name: Publish unpublished content
  • Choose from React on event list under Node After saving new content. And hit save
  • Add condition: Content is of type and choose your content type which includes date field for schedule time/date. And hit save
  • Add condition: Content is published
  • Data selector should be node - leave it as it is. Check Negate. This sets condition to "Content is not published". And hit save
  • Add action. And choose from under Rules scheduler Schedule component evaluation
  • Set value as Publis action. Which is the action (rule component) you made at the begining
  • From Scheduled evaluation date hit button Switch to data selection and find your date field which includes schedule date from Data Selectors list. Example: node:field-scheduledate
  • Write to Identifier value: Node [node:nid] - [node-title]
  • Node to Publish. Set Data selector to node (click on field and choose node (created content)). And hit save

That should do it. You can test it by creating new content. You can also check scheduled component from: admin/config/workflow/rules/schedule

MHz’s picture

I worked out that I simply hadn't enabled the the Scheduler Rules modules. oops.

at the step "Add action. And choose from under Rules scheduler Schedule component evaluation" I don't have this action in my list, any idea why it's not showing up?