Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
There are a few bits of software in the Drupal world that I call super modules. These are the ones that open up a whole new world of possibilities for your site. The obvious example is views, the study of which makes up most of my lynda.com course, Drupal 7 Reporting and Visualizing Data. Views took functionality that required custom programming, and put it out there in the hands of nonprogrammers, like me. Rules is another of those super modules; it basically lets you configure your site to respond in custom ways when users perform actions.
It's some what like the actions and trigger modules that are in core Drupal, but it's much better. We start out as usual, by going to its project page at drupal.org/project/rules. Before we install it, we are going to have to take care of one of its requirements, which is Entity API for Drupal 7. So I'll open that up in a new tab, and grab that as well. Now we install, and now we enable them, and now the rules module.
The Entity API module is in the other group, and then rules are down here in their own group at the bottom of the page. I'll enable Rules, and Rules UI, which is necessary to actually create rules. I'll leave Rules Scheduler off, since we're not going to talk about this, but if you're interested, of course, there is very good documentation for it. And then we save configuration. If you've forgotten to enable any modules that are necessary, as usual, Drupal will tell you. Once that's installed and enabled, you control rules by going up to the Configuration menu, and then down to Rules.
Rules comprise three parts: events, conditions, and actions. Their names make them fairly self-explanatory. When an event occurs, rules causes an action to be performed, if certain conditions are met. The best way to demonstrate is to create a simple, but very useful rule. Let's say that you wanted to display a thank you message whenever users post a comment. We do that by going up to Add New Rule, I'll name it Thank you for your comment, and the event it's going to react to is, after saving a new comment, and then I continue.
You'll notice it's already put that event up here. You could change it by deleting it, and adding a different event, or add a series of events, or whatever it is you like, but let's just go down and add the action. There are quite a lot to choose from, and this list gets even longer as you add more modules that interact with the Rules module. The one that we want, however, is down toward the bottom; Show a message on the site, in the System group. We then have options of what exactly we want to show, I'll just say Thank you for your comment.
You can actually customize this by using the replacement patterns, if you have Token installed. Let's scroll through here, and take a look at some of the things we could add. One of them is comment:author. This way, we could go back up, I'll copy that, and come up here, and say Thank you for your comment, comma, name, and scroll down, and save. Now let's see how that works. Go back to our front page, find an article here, since we can comment on articles, and I'll add a comment.
Scroll down, and Save. Now, we didn't see it very well, because of the way that the commenting works, but up here at the top, there is our message: Thank you for your comment, Admin. So perhaps this isn't the best example, but you can see the power. You could also have it send an e-mail, or change any of a number of environment variables, and so on. Because there was that condition section in the middle as well, you could also set it up so it only shows that message to users with certain roles, for example. And the way you do that is, again, go back to Configuration, and Rules, there's our Rule; we'll edit it, and add a condition, and as I say, there are quite a few conditions to choose from.
We'll go deeper into rule creation in the later video. Let's go back and take a look at some of the other parts of the rules system. I clicked on Rules to get back here, where I see the active rules, and next to each one, you will see, not only is there edit, but translate, disable, clone, delete, and export. These should be fairly self-explanatory. Clone makes an exact copy of the one that's there, disable turns it off temporarily, and so forth. I do want to show you export and import though. So we'll click export for the one rule that we created, and it gives us this text. We'll copy that, and then go back to our Rules page.
Now we can import that rule by clicking up here, pasting in the code, and we have a choice of overwriting the one that's already there, or we could go up here and edit it, change its name; do all sorts of things. I am not actually going to go through it, but you can see how powerful this is if you want to move Rules from one site to another. The last part of this system that needs mentioning is something called components. You see that up here under this tab. The best way to describe components is as bits of rules, or collections of rules that are packaged for easy reuse.
You can assign values to variables in components, making them vary depending on context. Now, this is a fairly advanced subject, and so I won't be going into it in this course. Like the Views module, the Rules module has a big user base, and lots of documentation. As usual, you can get there from the documentation link on the Rules module page. I've gone back to that page, and as usual, it's in the right column, and just click Read documentation. Just as amazing is how many modules integrate with the Rules module.
If we go back to that project page, and scroll down to the bottom, we have this link: Modules supporting rules. Now, since making this video, it's actually been split into two links: list of modules related to rules, and manual list of modules supporting rules. The first list is automated, while the second was put together by hand. Let's look at the second one. As you can see, it's an enormous list. These other modules typically provide hooks in the form of events or actions. For example, the User Points module makes an action available to rules, so you can grant merit-based points to a user who creates a comment, or creates a node.
Before going into rule creation any deeper, I'd like to show you how the shopping cart system Drupal Commerce uses rules, because it's quite instructive. It's an example of a complex system that's essentially based on rules, views and custom code.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99249 Viewers
56 Video lessons · 112522 Viewers
71 Video lessons · 81330 Viewers
131 Video lessons · 39045 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.