Drupal 7 Rules Scheduler Part 3

By shane
Wed, 2012-10-10 21:36
2 comments
Daily Dose of Drupal Episode #26

Share with Others

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.

Comments

Hi,

First of all, your website is awesome and has helped me a lot. However, I have a pickle that I need help with concerning rules and publishing of content.

I have created the content type with an embedded live PHP codded stream as the content. This stream plays just fine. Basically, I need to make the content do the following:

1) Display only during x and y times every day
2) On 1 specific page only

I have used the code below to limit the time period that the content shows:

$current_time = (Integer) date('Gi');
if ($current_time >= '600' && $current_time <= 1300) {
// stream is on the air, display the block
return true;
}
else {
// stream is off the air
return false;

I'm by no means an expert in PHP, so I hope that the code is correct.

Here are my issues:

1) The code doesn't seem to work on limiting the time period.

2) If I can get the time limits to work, how can I now limit the content to a specific page?

I have tried creating a view and setting that view to show only on 1 specific page then using the "views_php" module to limit the stream during specific times without success.

I have tried creating a block with the stream code and then placing the time limit code in the PHP filter without success either. This won't limit the content to a specific page.

Please help?!?!,

Raf

I am using a node page. I tied using views and panels but they give many problems with PHP visibility.

Anyway, I tried the code you gave me but it doesn't seem to work with Drupal 7. I found and altered the 2 sets of code below to suit my needs and they work perfectly:

a) To display during specific times (works with local time and not the server time):

$time = (int) date('Gi');
return (date('w') > 0 && $time >= 0001 && $time <= 0829);

b) To display block on a single page:

// Only show block from types array and nodes array
$match = FALSE;
// Which node types
$types = array('page' => 1);
// Select the nodes (by nid)
$nodes = array(23);
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load($nid);
  $type = $node->type;
  if (isset($types[$type]) && in_array($nid, $nodes)) {
        $match = TRUE;
  }
}
return $match;

I had the option of merging the 2 codes or using the "Nodes in a Block" module. I ended up using the module. I created a specific content type and then dumped that content type in a block. The good thing about the "Nodes in a Block" module is that it gives you 2 options for visibility (1 in the core options of the module and the other when you actually assign the block in the "Structure" menu.

So far this works flawlessly. I will mess around with merging the 2 codes and let you know how it turns out.

Thanks for your help though!!! And again: AWESOME SITE.

Chao.

Raf

Post new comment