Drupal 7 Rules Module Basics Part 1 - Daily Dose of Drupal Episode 24

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 at SM Thomas 3 you can visit my website at code karate comm and see all the daily dosage rupal videos as well as a whole bunch of other blog posts and you can also sign up for the code karate comm newsletter where I will eventually be posting some more in-depth case study videos but it might be you know 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 there it's pretty in-depth and so I'm going to you know 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 you know 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 and 9 or later so rules what exactly does rules do well you can think of it as kind of like a I guess like a conditional actions kind of thing if this happens or this specific event occurs and it meets this criteria then you want to perform some type of action on your site and I don't 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 a Drupal website builder and even a Drupal developer much easier so it's it's a little difficult to explain all that it can do so I'm just going to show you a couple very quick examples so the first thing I'm going to do is download the rules module and you can see it contains the rules admin rule scheduler rules I 89 which is for translations 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 past videos but it's pretty much bare I am going to go into the modules page and look for the Drupal 7 rules module and it's going to be down here towards the bottom 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 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 says I need to turn on the entity tokens module I'll go ahead and hit continue and I'm going to go through three very simple rules to kind of give you an idea of exactly what's what what it's capable of so if I come down to back to the rules Drupal seven 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 watch the past episode we built a custom module that just sent an email 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 how it can be so powerful we spent in 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 email 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 cron maintenance tasks are performed I would go ahead and look through these so you can see what's available to you and some modules will define new events if they integrate in 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 tasks 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 email when this action actually occur when this event actually occurs so when cron is run send an email and you can specify who do you want to send the email to you could use a replacement pattern here and use the site email address but I'm going to go ahead and just say I want to send an email to Shane at code karate comm and what the subject of the email 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 pieces 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 if I put this on one site it might say test code karate comm if I put it on another site it could say code karate com2 or whatever the name of the site is and then the email message I'm just going to keep it simple I'm going to use two replacement patterns here cron has run on the site name at and then I'm going to say the current date so you can you'll be able to tell exactly what time so pretty simple will leave the from blank so it will use a site-wide configured email address and we're going to go ahead and hit save so that's pretty basic rule if I click back here on the breadcrumb 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 a development site you may not get the email if you do not have your development server set up to deliver emails if you're doing this on your own local system I do have a blog post on code karate comm for easily setting up sending emails in Ubuntu or other linux related environments but there's there's a whole bunch of ways you can or whole bunch of things you can find online for getting your development environment set up for sending emails so now that now this email 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 email to trigger for some reason it didn't send right away but you can go ahead and you can see that says cron has run on code karate and then it tells you the date and the time and all that good stuff just like we told it to do so if I 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 build the first one we're just going to have it show message on login 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 in this as user you'll notice if I start typing in admin it shows up as the admin tag so we'll autofill 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 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 user's name so account name and then say thanks for logging in it's very simple very basic and we'll want this to be a status message and we're not going to repeat this message and 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 username thanks for logging in very simple I come back to the rules page you can see I still have that show message on login the other thing we're going to do is we are just going to go through one more back on and I'll probably just 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 not what I want to do here is I want it to any time a are an anonymous user comes to a specific content or a piece of content on my site or specific type of content I want to display a message that says you know maybe for instance please log in to joining 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 user has a role of an anonymous user so the 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 a data of the type user so we want to cite information the current logged end user and that's going to give us a user data type and we want the role to be an anonymous user so it's going to look at the current user see if they're an anonymous user and if it matches then it's going to pass so you can see if the cow as the content is viewed and the user has a role then we're going to do something we're going to go ahead and just do the same thing we did before just set a message and we could add links here you know 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 we're going to 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 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 they'll obviously show up here and we're going to keep this and it should be already on node 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 are going to go ahead and make sure I add contents of type article and I'm just going to go ahead and save that let's now have this test content now if I log out and I come over here to test it says please log in to leave content comments but if I go to this YouTube video 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 type of or that type of content that's showing up in the stream but if I go to any other actual node page that isn't of that type of the article type it will not show that message so as you can see that it we just went through and built three very simple rules nothing nothing too extreme but we're going to go ahead and get into a little more depth in the next few days if you have any examples of rules that you've built there ideas for rules that might be helpful for new users who are just learning rules again just send me an email or file or send me a message on Twitter you know find a way to get ahold of me and let me know in the next day or two and I'll see if I can get it worked into the next couple screencasts as always here's my Twitter account information and thanks for watching the daily dose of Drupal we'll be back tomorrow
Info
Channel: Code Karate
Views: 30,493
Rating: 4.8620691 out of 5
Keywords: Drupal, Rules, Conditional Actions, Triggers, Daily Dose of Drupal, DDoD
Id: j9SD2S69Fjw
Channel Id: undefined
Length: 15min 19sec (919 seconds)
Published: Mon Oct 08 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.