Drupal 7 Rules Module Basics Part 1

By shane
Mon, 2012-10-08 18:40
Daily Dose of Drupal Episode #24

Share with Others

Learn about the basics of the Drupal 7 Rules module and how the Drupal Rules module can be used to react to specific events that happen on your Drupal 7 website.

In this episode you will learn:

  • What the Drupal 7 Rules module is and how you can use it to build more advanced Drupal websites
  • How to build Drupal 7 Rules to react to specific events that occur on your Drupal website
  • How to configure conditions to limit when your Drupal Rules should be triggered
  • How to set up actions to fire when your Drupal 7 rules are triggered

Welcome to another Daily Dose of Drupal this is Episode Number 24. Today we’re going to be talking about the Drupal 7 Rules module and as always I’m Shane, you can follow me on Twitter @smthomas3, you can visit my website at codekarate.com and see all the Daily Dose of Drupal videos as well as a whole bunch of other blog post and you can also sign up for the codekarate.com newsletter where I will eventually be posting some more in depth case study videos but it might be in a few weeks or month until I actually get around to finishing those up.

But the Rules module; we’re going to take a look at what exactly the Rules Module does. Today we’re only going to be touching the surface on the Rules Module, it’s pretty in depth and so I’m going to break it out over a few different days here and if you have any suggestions or any recipes that I guess you have built using rules, you can go ahead and send those to me and if you want me to show others some cool things you can do with rules. I’m going to first explain exactly what rules can do and show you just a few very simple examples.
In order to use Rules in Drupal 7 you’re going to need to download the Entity API Module, I already have that set up on my test site but you’ll need to make sure you have that downloaded; it says that right in here you need version 1.0 beta 9 or later. So Rules; what exactly does rules do? Well you can think of it as kind of like … I guess like a conditional actions kind of thing, if this happens or if this specific event occurs and it meets this criteria then you want to perform some type of action on your site and if you’ve used Drupal in the past you probably know about the Core Actions Module, this is similar to that but a lot more flexible, it’s really more of a framework than anything and it contains a lot of things that can make your life as Drupal website builder and even a Drupal developer much easier.

So it’s a little difficult to explain all that it can do so I’m just going to show you a couple of very quick examples. So the first thing that I’m going to do is download the Rules Module and you can see it contains a Rules Admin, Rule Scheduler, Rules I18 n which is for translation and then Rules. I’m going to come to my test site here and it’s just a basic Drupal 7 test site, I have some content and I’ve been using it for the past videos, pretty much bare, I’m going to the modules page and look for the Drupal 7 Rules Module and it’s going to be down here towards the bottom and you can see there’s the three modules here plus there’s the rules translation module.

In this example let’s go ahead and turn on the Rules Module and the Rules UI. You’ll need the Rules UI in order for actually doing anything on the website with rules, it’s similar to how views has a views module and a views UI module so it just separates out the actual interface from the code that runs all the rules, so I’m going to save that, it says I need to turn on the Entity Tokens Module, I’ll go ahead and hit continue and I’m going to go through very simple rules to kind of give you an idea of exactly what it’s capable of.

So if I come down to back to the Drupal 7 Rules Module, I’m going to click on Configure and it brings me to the rules administration page, you can see it says here my active rules and here are my inactive rules. So I can of course add a new rule using this link here or here and we will go ahead and click on Add New Rule and if you watched the past episode we built a custom module that just sent an e-mail every time Cron ran on your Drupal 7 website. We’re going to do the same thing with rules and I will show you why or it can be so powerful.

We spent I think two episodes before writing that module where this is something we can do in just a few minutes, so we’re going to say send e-mail on Cron, it builds a machine name for you, we can give it a tag here and this is just for your own filtering purposes, I’m just going to put it as an admin tag so I know that all my admin rules could be groups together and now you have all these different events and these are the different things you can react on.

So if you want to do something specific when a comment is viewed or after a comment is deleted you could select that event. In this case; I’m going to go ahead and say when a Cron maintenance tasks are perform. I would go ahead and look through this so you can see what’s available to you and some modules will define new events in they integrate it with rules. So keep in mind that that … depending on what other modules you may have, your list could be different. So I’ll hit save and now we’re on the Rules or this specific rules configuration page, you can see the event that occurs, Cron Maintenance Task are performed, we can have specific conditions.

Those are additional conditions that have to be met in order for this event or this action to fire and now we can add an action, I’m going to leave conditions blank for now, we’ll come back to that. So if I want to click Action or add an Action and we’re going to go down and take a look at this list there’s a whole bunch of things you can do here, we’re going to look at Send Mail, we’re going to try to send an e-mail when this Action actually occur, when this event actually occurs, so when Cron is run, send an e-mail and you can specify who do you want to send the e-mail to, you could use replacement pattern here and use the site e-mail address but I’m going to go ahead and just say I want to send an e-mail to Shane at codekarate.com and what the subject of the e-mail is going to be, I’m going to say Cron has run on and then I’m going to select this token down here and if you’re not familiar with tokens, they’re just little piece of text that will be replaced based on the settings on your Drupal site.

In this case; I’m using site name so it’s going to be replaced with the name of the site and this is all configurable in the admin section of your site. So if I put this on one site it might say test.codekarate.com, if i put it on another site it could say codekarate.com 2 or whatever the name of the site is and then the e-mail message; we should keep it simple, I’m going to use two replacement patterns here, Cron has run on the site name@ then I’m going to say the Current Date so that you’ll be able to tell exactly what time.

So pretty simple, we’ll leave the From blank so we’ll use a site wired configured e-mail address and we’re going to go ahead and hit Save. So that’s pretty basic rule, if I click back here on the Bread Crumb It’s going to bring me back to the Rules page, you can see that this is now an active rule, if I come back here and run Cron you want to make sure that if you’re doing this on the development site you may not get the e-mail if you do not have your development server setup to deliver e-mails if you’re doing on assigning own local systems, I do have a blog post on codekarate.com for easily setting up sending e-mails and abontoo or other Linux related environments but there’s whole bunch of ways that you can or whole bunch of things that you can find online for getting your development environment set up for sending e-mails.

So now that this e-mail should have been sent, so I’ll go ahead and check here; it doesn’t seem like it came through yet … okay so I finally got that e-mail that trigger for some reason it didn’t send right away but you can go ahead and you can see that it says Cron has run on codekarate and then it tells you the date and the time and all that good stuff just like we pulled what to do so if we come back here you can see that we have the active rule still, we’re going to go ahead and disable this.

So this allows you to easily enable and disable rules and then now it shows up in the inactive. We’re going to go ahead and quickly go through two more quick rules here that we can built; the first one we’re just going to have it “Show message on log-in” we’re just going to have it display a quick welcome message when a user logs in, so I’m going to put a tag on this as user, you’ll notice when I start typing in Admin it shows up as the Admin tag so we’ll auto fill for you, I’ll just put this as a User tag and we want this after a user has logged in we’ll go ahead and hit Save and we’re going to skip conditions again and add an Action and in this action we’re going to go ahead and go to system and show a message on the site, we’re going to use a replacement pattern here for the users name; so account name and then say thanks for logging in, it’s very simple, very basic and we want this to be status message and we’re not going to repeat this message, you’re going to make sure it doesn’t get repeated, so we go ahead and hit Save, save the changes.

Now I’m going to go ahead and log out of the site and log back in and it says my user name, thanks for logging in, very simple. If I come back to the rules page you can see I still have that show message on log in, the other thing we’re going to do is we are just going to go through one more quick one and I’ll try to make this one up a little bit as I go.

We’re going to do another show message here just because that’s simple and easy to demonstrate and we’re going to say when content is viewed, so that means when you’re actually going to view content and what I want to do here is I wanted to anytime or an anonymous user comes to a specific content or a piece of content on my site or a specific type of content I want to display a message that says maybe for instance please log in to join in the discussion in the comments section or something similar to that. So we’re going to add a condition here and we want that the use has a role of an anonymous user, so this data selector and it might seem a little confusing at first but there are specific types of data that you need to map to the specific user.

So we need to select the data of the type user; so we want the site information, the current logged in user and that’s going to give us a user data type and we want the role to be an anonymous user. So as we look at the current user, see if there is an anonymous user and if it matches then it’s going to pass. So you can see as the content is viewed and the user has a role then we’re going to do something; we’re going to just go ahead and just do the same thing we did before, just set a message and we could add links here and do things like that but we’re just going to say please log in to leave comments, I’m not going to repeat it again and hit Save.
We’re going to go ahead and add another condition; we’re going to say Content is of Type and we want to only show it on … we’ll go ahead and say Media Content. I have a bunch of different content types on my site but we’ll go ahead and we’ll say Article Content just to keep things simple because you’ll probably have that option if you have other content types so it’ll obviously show up here and we’re going to keep this and it should be already on notes so that should work fine, we’ll go ahead and hit save.

So now if user has roles and the user is of a specific type then it should show a message on the site. So we’re going to go ahead and make sure I add content of type Article and I’m just going to go ahead and save that so now I have this test content, now if I log out and I come over here to test it says please log in to leave content and comments but if I go to this YouTube videos it does not say that.
You’ll notice that when I’m on the main home page it also says that because there’s those types of or that type of content that’s showing up on this stream but if I go to any other actual node page, that isn’t of that type of the article type that will not show that message. So as you can see we just went through three very simple rules, nothing too extreme but we’re going to go ahead and get into a little more depths in the next few days.

If you have any examples of rules that you built or ideas for rules that might be helpful for new users who are just learning rules, again just send me an e-mail or send me a message on Twitter, find a way to get hold of me and let me know in the next day or two and I’ll see if I can get it worked in to the next couple of screencast. As always here’s my Twitter account information and thanks for watching the Daily Dose of Drupal, we’ll be back tomorrow.