Taming the Butler: Learning Rules
With this blog post I’d like to summarize the content of the Learn the Rules framework screencasts, and give curious Drupalistas some easy entrances to one of Drupal’s most powerful and complex modules.
In early september I started publishing “Learn the Rules Framework” – a screencast series introducing the Rules module, and how to use it with some of its related modules. It is now nearly finished, and must be called a smash hit. The more than thirty screencasts have been viewed 5000+ times, and the first episode has more than a thousand plays by itself. Wow! I’m also overwhelmed by the positive reactions I’ve got from both people learning from the screencasts, and from maintainers of the modules covered in the screencasts. Thank you all!
What is Rules?
Rules is a framework for managing events, conditions and not least actions. It allows site builders to configure actions that should happen on certain events, and optionally only execute these actions if selected conditions are met. This can be both simple and quite complex things:
- When a user allowed to manage comments logs in, redirect her to the list of recent comments.
- When someone posts a comment on the site, notify people subscribing to comment notifications.
- When deleting an article, check for and delete any orphaned tags (tags not being used by other articles).
- Manage non-linear workflows, to require multiple approval before publishing content (sneak preview here!)
A very important part of Rules is its data and configuration management. It allows different parts of Rules (and other modules) to communicate about what type of parameters a plugin expects or provides. It also allows site builders to access this data in a quite slick way. The data management largely builds on Entity API, but extends it to allow non-entity variables to be managed in the same framework.
I am new to Rules – where should I start?
If you haven’t used Rules before, you should be aware that it is a module as complex as Views. Prepare for banging your head against the wall a few times – put on your helmet, and don’t expect everything to be crystal clear from the start.
I recommend the following path to start with Rules:
- View episodes 1–6 in the screencast series, introducing concepts like data types, lists and loops and creating non-trivial conditions.
- Watch episodes 7–11, showing some basic examples of Rules configuration – configuration that doesn’t require any additional modules or particularly complex configuration.
- Play around with Rules and create your own reaction rules for a while. Make sure you check out the conditions data comparison and entity has field, plus the action set a data value. These are important.
- If your configuration doesn’t work as you expect, watch episode 13 about debugging. It will save you time.
- Watch episode 6 – components – again, and then watch the introduction to using Views Bulk Operations in episodes 17–19.
- Play around with Rules components and VBO, and create yourself a nifty administration page or two. (This is a good and non-complex way of getting familiar with components.)
I’ve used Rules in Drupal 6 – what’s new in Drupal 7?
The changes between Rules 1 and Rules 2 (D6/D7) are big. The most important change is the data management, which takes some time to get used to. (I personally felt that Rules 2 was awkward and not really a success the first month or so, but I now feel that Rules has gotten some really important and really well-made improvements.)
If you want to get familiar with Rules for D7, I recommend the following:
- Watch episode 2, data types and data selection.
- Especially check out the conditions data comparison and entity has field, plus the action set a data value. These are key parts in using the new data framework.
- Watch episode 3, lists and loops. Loops are new in Rules 2, and really useful when it comes to using multiple-value fields (and some other cases).
- Play around with Rules 2 for a while. Check out episodes 9–11 for some inspiration, if you need it.
- Depending on how you have used Rules before, you might want to learn more about Rules Scheduler or combining Rules and Views Bulk Operations. You will definately want to check out how to use VBO to load lists of entities into Rules.
I’ve seen Rules before – where can I watch the cool stuff?
Ah – the cool stuff! You should check out the following:
- If you haven’t seen the new data handling, check out episode 2. Not extremely fancy, but you really must see it.
- Check out how to use Views to load entities into Rules. This is a wow.
- Check out the Views and CTools integration in Rules Bonus Pack. This is how components should work all over Drupal.
- You might also want to check out how to use Rules for doing things that Context normally does. I think this really could be something.
I’m a coder – what should I know about Rules?
- Check out how configuration parameters are managed in Rules. If you expose your data to Entity API, Rules will know about it.
- If you’re building modules with configurable conditions or actions, you should check out the components Rules provides. They are easy to implement in your module, and could save you a lot of time.
- You could also consider exposing your own actions/conditions as Rules plugins. There is quite good online documentation, and I incidentally also made a screencast series about writing Rules plugins.
- Similarly, if you’re building modules where you introduce new events that might be interesting for other modules to act on – tell Rules about them. It is a little bit more complex than writing actions and conditions, but not much. (Check out the documentation links above.)
- If you ever get stuck, the rules.api.php file is an invaluable resource when coding for Rules. Also, you can stop by at #drupal-rules on IRC.