Learning the Rules Framework

This page is archived

We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.

Alternate resources

The Rules module allows site administrators to define a series of conditionally executed actions on their website through a user interface. Rules allows you to configure different events that will be executed whenever certain conditions are met. This series on Learning the Rules framework was produced by Johan Falk of nodeone.se, and it will present some basic and advanced usage of the Rules framework. Here's an overview of what will be covered by this series:

  • Basic Rules concepts, such as reaction rules, data types, data input methods, using lists and creating loops, and Rules components.
  • Basic examples of Rules configuration, presenting some practical use-cases for the Rules module (using only the basic concepts).
  • Developing with Rules covers things like exporting Rules configuration and debugging Rules.
  • Rules Scheduler shows how to schedule evaluation of Rules actions (and other components), and also how to create repeated events.
  • Rules and Views Bulk Operations presents some of the improved integration between two awesome modules.
  • Rules Bonus Pack shows some experimental functionality that can be used with Rules, expanding your way of thinking about and using Rules – not least by utilizing Rules components from other modules.
Tutorials in this course
Categories
Drupal 7
More information

This series on Learning the Rules framework was produced by Johan Falk of nodeone.se, and it will present some basic and advanced usage of the Rules framework.

The Rules module helps decrease the need for custom coding. Johan starts with an empty sandbox site. He enables the 3 related Rules modules, and the modules they depend on. Now, we see what it can do for our site by creating some reaction rules. This isn’t just an introduction – you actually get into the mechanics of using Rules right away.

Additional resources

More information

This tutorial presents data types and the data selection method for input in Rules. This is the most important difference between Rules 1 (D6) and Rules 2 (D7).

  • How to use data selection to explore available data and drill down to selected properties
  • Using the data comparison condition
  • Using the set a data value action
  • That Rules recognizes different types of data, and verifies when necessary
  • That Rules knows that not all data is writable, and verifies when necessary
  • How to create composite tokens, extending the tokens listed in the replacement patterns
  • Making field values accessible to Rules
  • Using reference fields to access new data, such as tags on an article or nodes in a node reference field

Additional resources

Rules guide (Drupal.org)

Categories
Drupal 7
More information

This tutorial shows how to manage lists in Rules – data types corresponding to arrays of other data types. It covers:

  • How to loop through multiple-value data in Rules, to act on each individual data entry
  • That lists are their own data types in Rules
  • That each primary data type automatically gets a list type as well
  • How to add data to lists
  • How to only add data if it is not already present
  • How to select whether to add new data to the start or end of the list
  • How to load new entities into Rules (by id)
  • A few words about other ways of fetching entities, such as by property or with Views (through VBO or Rules Bonus Pack)
  • That Rules configuration are actually entities
  • How to disable reaction rules

Additional resources

Rules guide (Drupal.org)

More information

This tutorial guides you on a quick safari through the actions, conditions and events provided by Rules core. It covers:

  • A quick look at some actions provided by Rules
  • A quick look at conditions provided by Rules
  • A quick look at events provided by Rules
  • Some words about data objects being provided to Rules by the triggering events
  • Some words about multiple events in one rule limiting the available data (rather than expanding)

Additional resources

Rules guide (Drupal.org)

Categories
Drupal 7
More information

This screencast explains how to use AND and OR groups in Rules to configure more complex conditions. This is demonstrated by creating a rule that notifies administrators when content is promoted to front page using an OR group. (The same procedure can be used to create an AND group.)

The screencast covers:

  • How to combine conditions
  • How to create an OR group
  • Some advice about the order of conditions
  • How to delete reaction rules
  • Why you shouldn’t use the “send e-mail to all users in a role” action
  • Why it might be useful to have messages printed out when configuring complex rules

Additional resources

Rules guide (Drupal.org)

Categories
Drupal 7
More information

Why creating Rules components? Here is why!

  • You can reuse and simplify configuration
  • You can export each component individually
  • You can use rules components to perform conditions from within actions
  • You can execute components manually for easier debugging (or just for the sake of executing their actions)

An important aspect of components is that they have variables – parameters that must be sent to the component when executing it.

Additional resources

Rules guide (Drupal.org)

Categories
Drupal 7
More information

This screencast shows how to set up Rules to mimick parts of the Login Destination module. It covers:

  • How to redirect administrators to the content admin page on login
  • How to redirect non-adminsitrators to the front page on login
  • That you need the “force redirect” option when redirecting on login (yes, really!)
  • Some words about utilizing user permissions instead of user roles for conditions

Additional resources

Rules guide (Drupal.org)

Categories
Drupal 7
More information

This screencast shows how to set up Rules to allow node authors to – optionally – receive e-mail notifications when comments are posted to his or her content. It covers:

  • Adding a checkbox to user accounts, turning on or off comment notification
  • Sending e-mails to a node author when new comments are posted
  • Not sending e-mails to the author if he/she was the one writing the comment

Additional resources

Rules guide (Drupal.org)

Categories
Drupal 7
More information

This screencast shows how to set up Rules to allow comment writers to be notified when replies are posted to their comments. It covers:

  • Adding a checkbox to the comment form, turning on or off reply notifications
  • Sending e-mails to the comment author when new comments are posted
  • Not sending e-mails when replying to own comments
  • Some words about checking “entity has field” on the correct entity

Additional resources

Rules guide (Drupal.org)

More information

This screencast presents a way to help manage article promotions, separate content types used for promoting articles, providing more flexibility than standard teasers. The screencast shows how to have promotion nodes being published, unpublished and deleted in sync with the articles they belong to. Topics covered are:

  • Reacting on the node deletion event.
  • Using fetch entity by property to load a list of all relevant promotion nodes.
  • How to have the loaded list unlimited in length.
  • Using the delete entity action.
  • How to call rule components from a reaction rule.
  • Why it might be a good idea to have actions in separate components when acting on node updates.
  • Cloning rule components.
  • Publishing and unpublishing nodes.

Additional resources

Rules guide (Drupal.org)

More information

This screencast presents a way to automatically create the article promotions used in the previous screencast. Topics covered are:

  • Using the after new content has been created event, which does provide content NID
  • Working with rule set components.
  • Creating new entities with Rules.
  • Setting field values in new entities – even complex fields like images.
  • Force-saving entities, for example to get node IDs.

Additional resources

Rules guide (Drupal.org)

More information

This screencast shows how to work with export and import of Rules configuration, in particular using the Features module. It covers:

  • Why you should use the Features and Strongarm modules.
  • How to export Rules configuration with Features.
  • How to import Feature-exported Rules configuration to a site.
  • What reverting rules configuration means, and how to do it from Rules or Features.
  • How to (eventually) export Rules configuration manually, and some words about the high readability in the Rules export code.
  • How to import manually exported Rules configuration, including some available options/tweaks.
  • Why configuration management is an important issue for Drupal.

For more information and tutorials on the Features module, check out the Drupal Deployment with Features & Drush Series.

Additional resources

Rules guide (Drupal.org)

More information

This screencast shows how to go about debugging Rules configuration. It covers:

  • Enabling the debug information in Rules, showing rules evaluation.
  • How to drill down into Rules’ debug messages.
  • That most (or all) times, it is your configuration and not the Rules engine that has the bug.
  • That the most common bug is condition that evaluates in an unexpected way.
  • How to see how long time rules execution takes – and why/when this may be important.
  • How to use the debugging action in Rules, to inspect the data in Rules.

Additional resources

Rules guide (Drupal.org)

More information

This screencast shows how to start using Rules Scheduler. It does this by mimicking the Comment Closer module – closing the comments on articles two weeks after they are created. The screencast covers:

  • Rules Scheduler needs components – you can only schedule prepared components
  • Components can be scheduled as actions, for example from reaction rules
  • The evaluation time for scheduled tasks is set with strtotime(), which means that you can use highly flexible expressions
  • You can also use data selection, combined with offsets if you want to
  • Every scheduled task should have a unique task ID, or it will replace existing tasks
  • Scheduled tasks are displayed under the Schedule tab on the Rules admin pages
  • You can delete tasks manually if you want to
  • You can schedule tasks manually, without the need of reaction rules
  • It is sometimes useful to execute rule components manually, to see that the scheduled tasks will work as expected

Additional resources

Rules guide (Drupal.org)

More information

This screencast shows how to use fields when scheduling Rules components. In the example we are also using the Flag module, to allow people to sign up for reminder e-mails 24 hours before an event starts. The screencast covers:

  • Using flags to load user lists in Rules
  • Scheduling a rules component from a triggered rule
  • Some words about rescheduling the evaluation if the event date should change
  • Using conditions to make fields available in Rules configuration
  • Using a date field to set evaluation time
  • Adding an offset to the evaluation (-1 day)
  • Verifying the configuration by executing the action set manually

Note: It would have been smarter to use the “Before saving content” event for this rule – to make it act on both new nodes and updates of existing ones. If combined with a check to see that the event is in the future, you would actually have pretty good notification system!

Additional resources

Rules guide (Drupal.org)

Categories
Drupal 7
More information

This screencast shows how to repeat scheduled events – in this case sending repeated reminders to users who have not logged in for a month. The screencast covers:

  • How to set up a component for scheduling (as seen before)
  • The point of re-using an ID for scheduled tasks, to avoid duplicate reminders
  • The trick of finding a good triggering event for scheduling (which is not always easy)
  • The trick of having a component schedule itself upon completion, thereby repeating the scheduling
  • How to delete scheduled tasks, and why that may be a good idea

Additional resources

Rules guide (Drupal.org)

More information

This screencast introduces the awesome Views Bulk Operations (VBO) module, that can be used to perform actions on objects listed with Views. The screencast covers:

  • How to add VBO fields to your views, to allow actions
  • That these fields are displayed as check boxes (or possibly radio buttons) in your view
  • That each VBO field is tied to one type of data in the view (such as nodes, node revisions or users)
  • That some VBO actions have configuration pages

Additional resources

Views Bulk Operations (VBO) guide (Drupal.org)

More information

This screencasts presents some of the settings available when configuring a bulk operations view. It covers:

  • Controlling access to your VBO, and why that is important.
  • Showing actions as buttons rather than a select list.
  • The new cool option for selecting all items on all view result pages.
  • Turning check boxes into radio buttons.
  • Showing or hiding the result of the bulk operations.
  • Showing action configuration on the view page.
  • How to skip the confirmation page.
  • How to give your actions customized labels.

Additional resources

Views Bulk Operations (VBO) guide (Drupal.org)

More information

This screencast shows how you can use Rules components in VBO, allowing much more complex and flexible actions than comes out of the box with VBO. The episode covers:

  • How to create a simple Rules component.
  • How component parameters are used by VBO – in particular that it is useful to have a single node as parameter if you want to use a component with a node view.
  • Some words about what the provided variable option for Rules components means.
  • An example showing how to use VBO to close (and hide) comments using VBO + Rules in combination.
  • Some comments about creating more complex Rules components.

Additional resources

List operations with Views (Drupal.org)

More information

This screencast shows “how you can use multiple-parameter Rules components in VBO and get the additional parameters as action configuration,” which translates to:

  • If you have more than one parameter for a Rules component, you can still use it with VBO.
  • Any additional parameters will be displayed as action configuration, meaning that the person executing the VBO gets to choose parameter value.
  • Sadly, you can’t access data from the view item being actioned – you’ll have to stick with fixed input values. (If you want to use data from the processed item, you could actually do this inside the Rules component!) This is an issue being worked on.
  • If you have complex parameters – such as taxonomy terms instead of just an integer – you can (mostly) use entity ID in the direct input mode.
  • You can, by coding, change the form elements used for input. It is probably better to target the Rules data widget than doing a standard form_alter, but both will work.
  • You could, for example, use this for changing comment settings on nodes, or adding selected tags to nodes. (Both examples shown in this screencast.) You could also use it for, say, sending customized messages or something. Or cloning existing nodes with changes specified in parameters.
  • Bonus: If you add an entry to a multiple-value field in an entity, Rules won’t pick up that the entity has changed – make sure to add a “save entity” action in the rule.

Not mentioned in this screencast but still good to know:

  • You can have Rules components without any parameters as well. No sweat.
  • You can use Rules components without any parameters matching the View type too. In that case, you’ll have to specify all the parameters manually.
  • If you have a component with several parameters matching the view type, the first one will be auto-populated by VBO – the rest you have to set manually.

Additional resources

Rules guide (Drupal.org)

More information

This screencast shows the awesome functionality of loading entity lists into Rules with help of VBO. As an example, a rule is set up that, once a day, removes any stick front page content that is older than one week. To do similar cools stuff yourself, the following steps may be useful:

  • You need a view that lists the entities you want to work on in Rules. Note that you don’t need a display of the view – and it might even be better not to have one if you don’t want the view to be displayed somewhere.
  • However, you need one bulk operations field. The type of bulk operations field determines what entity type will be sent to Rules. Note that you don’t need any actions enabled for the field – it is enough that it is present.
  • You need a rule that, as an action, loads entities from a VBO. (This is available under the “Views bulk operations” group.) All view displayes with at least one VBO field will be selectable.
  • The action provides a list of entities, that can be used just like other lists in Rules. Combine with loops, actions and Rules Scheduler to make awesomeness happen.

Additional resources

Rules guide (Drupal.org)

More information

This screencast quickly shows the events, conditions and actions that Flag exposes to the Rules module. It covers:

  • Event: Flagging and unflagging entities
  • Condition: Flag is set on an entity (by a particular user)
  • Condition: An entity has at least a number of flaggings
  • Action: Load the list of users flagging an entity
  • Action: Set or remove a flag (for a particular user)
  • Action: Remove all flaggings (for a particular user) – or save a particular number of flaggings

Additional resources

Categories
Drupal 7
More information

This screencast shows the Rules Link module, and some basics in how to use it. It covers:

  • That Rules Link adds links to entities (and that you set up each link separately).
  • That these links triggers rule sets.
  • That the links may be displayed as links on (for example) comments and nodes.
  • That Rules Link is useful when you previously used Flag, but didn't really need the 1/0 data that Flag provides (but rather just the Rules trigger effect).
  • That access to each configured link can be controlled using the standard permission system.

All of this is demonstrated by adding a "block spammer" link to comments, allowing administrators to block the comment author and delete all comments written by him/her.

Additional resources

Rules guide (Drupal.org)

Categories
Drupal 7
More information

This screencast shows some more settings in the Rules Link module:

  • How to use the visibility conditions in Rules Link.
  • That the entity the link is attached to is always available as a parameter.
  • That you can add more variables to the Rules Link by passing them in the URL – and that you need to set up the parameters settings to tell the link how to interpret the data.
  • That each link is available as a field in Views.
  • That you can add parameters to links displayed in Views.
  • That there is a bug preventing links for non-node entities to be displayed in Views.

Additional resources

Rules guide (Drupal.org)

More information

This screencast shows some functionality included in Rules Bonus Pack, as an example of how to make Rules integrate with other modules on your site. In particular it shows some Views and CTools/Page manager integration.

  • Condition: Check the number of results from a view.
  • Action: Load the sum of a Views column as a number. (Currently only D6!)
  • Action: Load the first result in a view into Rules.
  • Action: Clone a full node object.
  • Action: Convert a number into a date
  • Event: Trigger a rule when a custom page is rendered.
  • Special: Allow Rules condition components to be used as CTools access plugins.

Additional resources

Rules guide (Drupal.org)

More information

This screencast shows how you can use Rules to alter the presentation of your Drupal site, such as:

  • Enabling, disabling and moving blocks around
  • Setting HTML title element and also on-page titles
  • Setting body classes
  • Setting the active menu item (using Menu Position)

Some bigger questions about using Rules for altering presentation is also discussed:

  • Using Rules for presentation altering is relatively heavy. (It consumes about 2.5 MB more PHP memory than Context, and has about the same loading times – judging from a first, rough comparison.)
  • Using Rules for presentation altering gives a more complex UI than the interface use for modules made for more particular use cases.
  • It is possible for modules to provide alternative user interfaces to Rules.
  • Rules allows reusing actions in many different situations, between different modules.
  • Rules (and Entity API) provides generalized data handling.

Additional resources

Rules guide (Drupal.org)

This course appears in the following guides:
Site Building
A guide to site building in Drupal 7.

Legacy Site Building