Using Rules to publish/unpublish based on a Date field

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

Can anyone point me to a tutorial or "how-to" for publishing/unpublishing content with Rules using a Date field -- in Drupal 7? I've done it before in Drupal 6 -- but I'm stumped about how to do it in Drupal 7.

Here's the scenario: A content type has a Date field. When you create a node of this content type, the node is "unpublished." I'd like to set up a Rule to publish the content on the date contained in the Date field.

Conversely, I need to be able to set up the opposite scenario as well: A node starts off as published. I need to be able to "unpublish" it on the date contained in the Date field.

Any assistance would be greatly appreciated.

Comments

joemaine's picture

This worked for me to unpublish a node when it hit a date field.

Create the Rule set first...then the Rule.

Not the best of instructions...but it might work as an example for you - Good Luck

at /admin/config/workflow/rules/components/add
Create a ruleset (name it "Unpublish Content")
In the Settings/Variables:
Data type = "Node"
Label = "node"
Machine name = "node"
Save
...next screen...Add rule
Name = "Unpublish Content Rule"
Save

Conditions:
Content is published
Data selector = node
Save
Content is of type = node
Value = your-content type
Save

Actions:
Unpublish content
Data selector = node
Save

at /admin/config/workflow/rules
Create a rule (name it "Unpublish Rule"
React on event = After saving new content
Save

Conditions:
Content is of type
Data selector = node
Value = your-content-type
Save

Actions:
Schedule component evaluation
Component Value = Unpublish Content
Data selector = node:field-your-node-date-field
Identifier value = unpublish [node:nid]
node Data selector = node
Save

Thank you for this

drupal27923's picture

Thank you for this joemaine.

I've followed your instructions and created the rule set and rule. When testing I can see that a component is scheduled on the Schedule tab within rules after I create a new piece of content. However when the date passes, the component is run but the content is not unpublished.

Where exactly do I add the action to unpublish the content?

Thanks,

Adam

Thanks joemaine

jim22's picture

This worked and was very helpful. Thanks for taking the time to document it.

Scheduler Module

drshearer's picture

Any reason not to use the Scheduler module for this? http://drupal.org/project/scheduler Seems to do the trick, pretty easily.

Scheduler Module

joemaine's picture

It is an easy solution. But rules offers more scheduling options (not just publish/unpublish). I needed a solution that could schedule on the update of a node as well as the creation of a node. ...and just didn't want to add yet another module.

Does not see date field

gettysburger's picture

I have a content typwe calle Event. It has two date fields. One is the date of the event. The second is field_event_expiration, which is the date I want the node to expire (unpublish.) When I get to the last step in your example:

Actions:
Schedule component evaluation
Component Value = Unpublish Content
Data selector = node:field-your-node-date-field
Identifier value = unpublish [node:nid]
node Data selector = node
Save

It does not see my second date field or allow me to set it by hand in the Data Selector. It just sees the first date field. I even set another condition "Entity has Field" and it let me set the second date field in that. Any thoughts? Thanks.

I just did something similar

nflowers1228's picture

I just did something similar where I wanted to unpublish something 30 days after it was created. Rather than using a 2nd date field, I used the Offset. This field is in the Scheduled evaluation date section. I added 10 mins first to test. when that worked, I changed it to 30 days. This tutorial was helpful as well: http://nodeone.se/sv/node/770

Nancy Flowers-Mangs

Don't forget to run cron...

hrodrig's picture

joemaine's solution worked very well- just remember to run cron when testing it out!

Thanks this is a great

nflowers1228's picture

Thanks this is a great tutorial!

Nancy Flowers-Mangs

at

pikadroo's picture

at /admin/config/workflow/rules
Create a rule (name it "Unpublish Rule"
React on event = After saving new content
Save

Conditions:
Content is of type
Data selector = node <------
Value = your-content-type
Save

I get to that point and I am unable to enter anything in the data selector other then site. If I try to put in node it is rejected when I try to save it and I can't seem to locate a reason why.

Drew

A tremendous help - Thank

hyperglide's picture

A tremendous help - Thank you!

Wow... really?

pikadroo's picture

Wow, really? Wasn't any help to me. I guess that is how it works around here just post "help" threads and sit back and enjoy the praise.

Drew

Hey Drew

joemaine's picture

I thought I was pretty clear in my opening statement
"Not the best of instructions...but it might work as an example for you - Good Luck"

I'm not a coder and am probably the last person you would want to call on for 'support'. I posted what worked for me, wasn't looking for praise...just trying to give back to the community.

I wish I could help you...but I had a hard enough time walking through the solution I posted.

Completely Clear

pikadroo's picture

Typical Drupal response. Nobody knows anything right we just click hack around till something either breaks or works.

Cheers,
Drew

Sounds like a field inclusion problem in Rule's scope

dydave's picture

Hi pikadroo,

I think the reason why you probably can't find/select the date field you would be looking for is because you would probably need to add your variable in the Rule's "scope".
In other words, it's like declaring to rules that this object/variable/field/property is available when it runs.

Concretelly speaking, this would result in adding all the necessary conditions to check on the node whether the field is there or not. In terms of the conditions to be used:
Conditions on Data come in very handy: Data comparison, Data value is empty, List contains item, Text comparison.
For example, whatever entity you would be using, you could always compare the entity:type, with a particular type (for example node:type = article).

In this particular case you would have to use some of the Entity Conditions: "Entity has field" and select the field that you would like to use in your actions or other conditions.
By adding this condition to the Rule, it ensures that for any other actions or conditions further down in the execution scope, this field would be available on the objects accessed (Preventing from failure or crash: undefined variable or field).

You might as well want to add a Data condition to test whether the "Data value is empty" for the field (added in the scrope)

Lastly, I don't think rejecting the blame on others would necessarily be helpful. we understand your frustration but everyone's issues are always very urgent and most of us are already kept busy answering questions or assessing problems in other trackers or forums.

For faster response, you might want to try the IRC channels #drupal, #drupal-support (since this is a rather common question) or reading the rules documentation at Rules: Actions Automations, or Why can't I access my custom fields in Rules? (This is a very common question encountered with Drupal Commerce Payment Rules customizations).

I hope this will help resolving the issue you mentioned and earn back some of your trust in the ability of our community to collaborate with a helpful and friendly spirit.
Thanks again for your interest in Rules and perseverance.
Cheers!

Unpublished content showing up in schedule

bsetzer's picture

Hi there:

I have implemented the above rule from joemaine and overall it works great. I changed the action event to "after updating existing content" and I have found that editing unpublished content is causing it to appear on the schedule. Any idea why this might be occuring? I have the first condition of the rule set as "content is published".

Thanks much!
B.

A simpler way

gaspoker's picture

Hi all!
I think you can simplify the rule creation as follows:

At /admin/config/workflow/rules/components/add

Create a "Action set":
Name = Unpublish Content
Variables:
Data type = Node
Label = Node
Machine name = node
-Continue-

Actions:
  Node > Unpublish content
  Data selector = node
  -Save-

At /admin/config/workflow/rules
Create a "rule":
Name = Unpublish Rule
React on event = Node > After saving new content
Restrict by type = your content-type
-Save-

Conditions:
  Content is published
  Data selector = node
  -Save-

Actions:
  Schedule component evaluation
  Component Value = Unpublish Content
  Scheduled evaluation date data selector = node:field-your-node-date-field
  Identifier value = Unpublish content ID [node:nid] ([node:title]).
  Node Data selector = node
  -Save-

That is, it is not necessary to filter the content when create the component nor add a content-type condition in the rule.

Hope I was helpful.
Gaspo

Wow, worked for me. I looked

dandolino's picture

Wow, worked for me. I looked for plugins that could do this, but this is a sure and simple way. Thank you

Yup, I know it worked for you

pikadroo's picture

Yup, I know it worked for you because that is how it works around here. Someone can't figure out the obscure our outdated terminology used in a 5 year old post. They ask for help, hear NOTHING for a year, and have long since moved on with their live without. Then people like you come in here and stroke the Drupal ego, pat it on the back, and tell it what a good boy it is how good it is because it's such a good thing. how do I turn off notifications I am tired of this drupal ego spam.

It worked!

icf-vpathak's picture

Thanks joemaine!! I followed the procedure you mentioned and it worked.

Also for people who are not able to find their date field. Inside SCHEDULED EVALUATION DATE switch to data selection. Doing this allows user to enter their date field.

Rules

Group organizers

Group categories

Categories

Group notifications

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