Drupal 7 Rules Scheduler Part 3
We continue learning more about the Drupal 7 Rules module in this episode of the Daily Dose of Drupal. We examine how the Rules Scheduler module works and how it can be used to schedule the triggering of Rules actions.
In this episode you will learn:
- How the Drupal 7 Rules Module can be used to schedule actions to run in the future
- How to publish a newly created Drupal node on a future date
Welcome to another Daily Dose of Drupal, today we’re on Episode 26. We are continuing on with our discussion on the Rules Module from the past two episodes so if you haven’t watched those and you want to get an introduction on the basics of Rules and Rules Components you should watch those two episodes, as always I’m Shane from codekarate.com, you could follow me on Twitter @smthomas3, also sign up for the newsletter. Alright Rules; what are we going to talk about today?
We’re going to talk about scheduling different Rules after specific event has occurred. In this Example we’re going to walk through the Rules Scheduler Module a little bit and we’re going to give one example of how you can set it up to automatically publish content after a specific period of time.
So let’s say you create a post and you want it to automatically publish in an hour later for instance or a day later, you can go ahead and follow this example and you’ll be able to do that. One thing I want to stress is on the project/rulespage.org in for Rules Module there’s a re-documentation link down here, if you’re interested in learning more you should definitely go there. It’s a very well documented module with a lot of examples and this … what I’m going to walk through here is just based off one of those examples.
So we’re going to go ahead and get started; the first thing is you need to download Rules and all of the necessary modules to run those, I think C tools is needed maybe, we’ll go ahead and scroll down to Rules or … excuse me, Entity API is needed. So if you go ahead and you enable the Rules Scheduler Module and you hit Save Configuration, the Rules Scheduler Module will be enabled, I already had the Rules and the Rules UI Modules enabled so you need to make sure you had those on as well and we’re going to go ahead and click on Configure on Rule Scheduler.
This brings you to a page …you can ignore that error, it’s a different module, this brings you to a page that’s going to list all the different rules that you have to be scheduled that are going to be run in the future. The first step of what we’re going to do here is we’re going to build a component and in this case we’re going to build an Action Set and what we want this Action to do is we want this Action to actually publish a specific piece on content when it’s run.
So I’m going to call this Publish Content Action and I’m going to select Node as the data type because we’re going to publish a specific Node. The label; I’m going to say Node To Publish, give it a machine name, leave this as it is and go ahead and hit Continue. Now we want to add an Action and in this case we go ahead and we scroll down under Node and hit Publish Content.
On this Data selector we’re going to click on it and select our Node to Publish and hit Save and this is going to go ahead and publish the content when this is run. So we’ll save that and we’ll come back to this Rules page. If you come over to Components you can see that we have this Publish Content Action, that’s the Action Set. Now we’re going to build a Rule.
So on the Rules page we click Add Rule, so we’re going to call this Publish Content After One Hour, you could name that whatever you want so you know what it is and we’re going to execute this Rule or trigger this Rule after saving new content, we’re going to hit Save.
The event is after saving new content, a condition, well go ahead and add one and we’ll click Content is Published and it’s going to automatically bring in the Node and we want to negate this. So this is basically saying Content is not Published. So if you’re saving a Content and it’s not published then we’re going to perform an action and in this case we’re going to scroll down to Rules Scheduler and we’re going to select Schedule Component Evaluation and we’re going to select our Component which is Publish Content Action, we’re going to hit Continue and now we have to select the Schedule Evaluation Date.
If you read this it will tell you you could set a specific time or you can use any other values like +1 day, +1 hour or things like that and you read that and find out some more so we’ll go ahead and do +1 Hour here just as example, at the Identifier we want to show what Node is going to be published, so we copy this replacement pattern in here and I’m going to say Node ID and we could even go as far as adding the Node title in there as well and I’ll show you where this is going to show up here in a second and the Node to Publish is the created content, go ahead and hit Save.
Now what this says is after you save a new content, if it is not published it’s going to schedule this Action that we created to run and this Action that we created was of course to Publish Content. It’s going to take this piece of content, it’s going to queue it up and then when Cron is executed after that one hour has passed then it will get run and it will go ahead and be published.
The thing to make sure you have set up is a running Cron so it runs periodically and depending on how you set it up, you may want to run it every 15 minutes or maybe a couple of times day depending on your site, it can vary and certainly it doesn’t hurt to have it run more often besides the fact that it adds a little bit of extra load on your website.
So let’s go ahead and let’s test this out; so I’m going to go to Add New Content, I’m going to add an article, call it Test on Publish Article, add a little bit of a body and in the Publishing Options I’m going to make sure it’s not published, I’ll hit Save, I went ahead and it’s been created so I’ll come over here to my Rules, click on Schedule and you’ll notice that now there’s an entry here; it says Rules Publish Content Action, that’s the Component that’s going to be run, the schedule date of when it’s going to be run, which is about an hour from now … or let me see here … no it looks like it might be the current time … okay that should be the time it should be run I believe but I don’t know, I think the time on my server maybe off so I’m not exactly sure on that but it does Node 18 which is the Node ID and the title of the article and also an operation to delete it.
And that’s really all there is to it, as soon as Cron runs, pass the date that this was scheduled to run, it will go ahead and execute this or trigger this action which is going to publish this content. And this is just one example of how you can use the scheduling component, you can also come in here and for instance under Publish Content Action you could schedule something to be run and you could select a specific Node ID. So let’s say you created it, you created a Node or a new blog post today and you want it to be automatically scheduled on Friday, you could come in here; select Friday date and time you want, put in the value here and put in the Node ID and schedule it and then you can schedule it for a specific time as well. So you can manually schedule one off events or one off components to run as well.
It was nice and short today and that’s all, tomorrow we’ll come back with a couple of other or a new topic all together and we’ll go from there. As always; you can follow me on Twitter as I mentioned before, thanks for watching the Daily Dose of Drupal.