Drupal 7 Rules Module Components Part 2

By shane
Tue, 2012-10-09 17:56
Daily Dose of Drupal Episode #25

Share with Others

In this episode we continue learning more about the Drupal 7 Rules module and explore building Rules components. We go through what Rules components are, and how they can be useful when building a Drupal 7 website.

In this episode you will learn:

  • What Rules components are and how they can make building Drupal Rules easier and more powerful
  • What the various types of Rules components are and what each type of component can be used for

Welcome to another Daily Dose of Drupal, this is Shane and we’re on Episode 25. Yesterday we’ve talked about the Rules Module and today we’re going to continue on with the Rules Module and talk about components a little bit and these few episodes I’m going over the Rules Module, they’re just the very basics of what the Rules framework is able to do.

I’m not trying to go into the My New details because frankly that would take probably 20 to 30 episodes to really dig in and learn everything there is to know. So what I’m doing is I’m just covering the basic showing you a few different things and showing you where you can go to hopefully learn more.

And so if you haven’t done one of the Rules Module already or watched the previous episodes you might want to start there. The most important thing you’re going to want to learn about or know about is the documentation page which has a whole bunch of different documentation that also has links to other videos on Rules. So if you want to expand your knowledge you can really dig in and learn more there. I have the components documentation page opened up; this is going to be another important piece of information you’re going to want to look at if you’re interested in learning more about what Rules Components are.

As always I’m Shane, you can follow me on Twitter @smthomas3, you can go to the website at codekarate.com, sign up for the newsletter, check out some other videos on post and go along such of stuff there. So I have my test site here and we already have the Rules Module installed but the important thing to note is we’re going to continue a couple of rules or one of the rules that we built the last time so you’re going to want to maybe take a look at that.

We’re just focusing on Components today and what a Component is it’s really a reusable … I guess kind of like a plug-in for Rules. If you have a specific set of conditions that you want to potentially use multiple times or a specific set of actions you want to be able to reuse or even if you have specific rule that you want to … it doesn’t necessarily have an event but you want to be able to manually trigger this rule at a specific time, this is where Components can come in.

So if you come down into Workflow and go to Rules you’ll see that I have some rules that I had built in the previous episode but we’re going to look at this Component section and we’ll just take a look at this first; I don’t have any components, I can go ahead and click Add a New Component and there’s 5 different options here; the first is an Or Condition Set; this means that you’re going to have a listing or a set of conditions and if any one of these conditions matches it’s going to go ahead and pass as True and basically same thing except for all of the conditions have to pass in order for it to evaluate as True.

Now you can think of this is you’re familiar with programming just as and IF Statement in a way, you know, if there’s an Or or an If statement, if one thing matches it will go ahead and pass, if it’s an And it both have to match or all have to match in order for it to pass. And Action Set is just a set of various actions … multiple actions that you can trigger and plug into a different rule and then there’s Rule and Rule Set, we’ll go over a couple of this here.

The first thing is we have a rule that shows a message to an anonymous user, when they view … I’m going to look at this Rule, when they view an Article Content Type. So when content is being viewed it looks for users that have a role of anonymous user, meaning they’re not logged in and they’re looking at an Article Content Type and then what it does is it shows a message on the site.
So let’s assume that maybe there’s other things that we’re going to have or other events in which we want to use these same conditions, we want to check is it’s an anonymous user and if the node is an article and in order to actually do that, we’re going to go ahead and jump into the components and we’re going to go ahead and build that reusable condition set.

So we go to Condition Set AND because we want both to be True and we’re going to hit Continue and we’re going to type in Anonymous User in Article Page, we can give it a tag whatever we want to go ahead and filter this more and now in this section we’re going to select the various parameters. You can think of this if you’re familiar with programming as to building out the parameters of the function in a way. This is going to be in similar to a way of Function is going to run and so we’re going to build the various parameters that are required in order to run or check these conditions.

One of the parameters is of course the User; so we’re going to go into Entity and select User and we’re going to select or type in for a label Current User and for the machine name we’re going to go ahead and just go Current User and the other Data Type we need is a Node Data Type because we need to check if it’s a natural article page. So the User is going to be used to check the role, the Node is going to be used to check if it’s an article page.

So we’re going to go ahead and just say Node Page here and this label isn’t … this could be anything you want really just so it helps you understand what is or what the parameter is being passed in here. So we’re going to hit continue and now we build out our conditions. So we’re going to add a Condition and we’re going to go ahead and say User has Roles, now in our data selector we need to find this current user parameter that we passed and select that, select that the user has anonymous role and go ahead and hit Save.

The next thing we’re going to do is add another condition for Content is up type, it’s under the Node section and you’re going to select the Node page parameter that you created and select Article.

Once you save that you now have your working component, so the next step is to go back to your Rules and you can go back to the Show Message to Anonymous User Role, click Edit, we’re going to get rid of these two conditions and we are going to add a new condition and this condition we’re going to go down to the Component Section all the way at the bottom and it says Anonymous User on Article Page.

Now we need to basically match up the different data types; so we know that our condition that we created accepts a user so we need to find the current user and that’s going to be under Site Information, the current logged in user. The next we have to match up the Node page which is going to be the Node page or the Node Content that’s being viewed, we go ahead and hit Save and now we have our Condition Set being used, everything else will work as before and it’s just a way to simplify how you build out your Conditions.

You can think of it as you’re building pieces of a puzzle and then you can plug those pieces in various areas. So if you have a bunch of different rules or if you need to trigger things problematically this can help out and it just makes us so you don’t have to individually add both those conditions every time if you’re going to build out a whole bunch of rules on your site.

You could do the same with Actions; you can build an Action Set but I’m not going to go ahead and do that because it’s pretty similar, the same concept just applied to Actions but one other thing we’re going to look at really quickly here is under Components, if you add a new Component you can add a Rule or a Rule Set. Rule Set is just a list of rules that you want to trigger all at once. So you can think of a Rule Set is just a wrap around a bunch of rules.

So we’re going to go ahead and just select a single rule here and it’s going to be very similar to how we had built other rules. So you could go ahead and … we’re just going to say Send Test e-mail …it’s going to be a not really practical one but it’s going to show you what it can do and you can select User, I’ll just put User in there as a label and we will go ahead and say User to Send E-mail to, just to make it a little more specific.

Okay we want to leave everything else the way it is and we’ll go ahead and hit Save Changes and I’m just going to run through this really quick here; what we want to do is we want to add an Action to send an e-mail and we’re going to take the replacement pattern from the User to E-mail mail field which is just a token, the subject we’re going to say Come Back and Visit the Website as the body and you can of course add replacement patterns or specify it from address but we’re going to make that very simple and hit Save.

Now we could go ahead and we could specify a condition, for instance a data comparison and we could say we want the user to send an e-mail to last log-in and we could leave that as the last logged-in date of the user and we could say Is Lower than and then select Direct Input Mode and you could go minus 5 days or something like that, save it and now when you go to Execute you’re going to … it’s going to allow you to select the user to send the e-mail to, you could then select Direct Input Mode and you could specify a User ID and if it meets those conditions, in this case if that User has logged in … or it’s been more than 5 days since the user has last logged in, it will send that e-mail to the user.

Note that if you’re in a development environment you need to have e-mail set up for this obviously the work and you’ll have to have a user that’s actually spend more than 5 days since they logged in but you get the idea; it can be very flexible, obviously this isn’t necessarily the most practical case but you could use this condition or this component and you can trigger this programmatically or reuse it in multiple different ways.

So just a whole bunch of flexible options that Rules Module provides for building out and I guess in various different workflows and actions that can happen on specific events or even when you, like I showed you manually executing action throughout your Drupal website and there’s a whole bunch more we can go over, we might cover one more day on just some basic Rules Module things.

If you have any ideas that you want me to throw into the next video please let me know on Twitter or just contact me on codekarate.com. Thanks for watching the Daily Dose of Drupal and we’ll be back again tomorrow.

Post new comment