In the Loop! How to process your views results with Rules


The Rules module is one of the most powerful site building tools in Drupal contrib space besides Views. You may already be familiar with creating reaction rules to perform custom actions after certain events. In this blog post, I will show you different ways of how you can leverage the Rules module to process lists of data.

The Rules UI is designed to provide only a limited choice of options for simplicity. Still, it allows you to create complex scenarios, and computer scientists may notice that its instruction set is actually turing complete. Within the actions configuration, there are no If/Then/Else constructs, but there is a module for that. The only thing that the Rules UI provides allows you to do besides defining a sequential list of actions, is the ability to loop over lists of things.

Intro: Using loops

Configuring a loop over a list of items is a pretty straightforward task for a site builder. At the bottom of your actions configuration area, use the "Add loop" link.

Rules Black Add Loop

Next, you need to configure the required variables to loop perform the loop on:

  1. Step one, use the data selector to pick a list of items. In the given example screenshot, we are looping over a list of nodes that the component takes as a parameter.
  2. Step two, specify the variable label and name that should be used to identify items within the loop. When the rule is executed, the inner actions will be executed for each of the list items and the current item will then be referenced using the defined variable name.
Rules Loop How to Add

Simple as that! You can now use the "Add action" and "Add loop" operations on the right hand side to configure what should happen for each of the list items.

Rules Loop Simple

Ok, I have to admit that this wasn't rocket science so far. Let's get to the gist of this article, where do we get the list data from? Often, your parameter variables will already either represent a list of items as in the example component before. In other cases, you might access a multiple-valued property of a single-valued variable, such as the Tags field of a node. In the following, I'd like to discuss two additional methods that will allow you to actually query for listed data and process it within Rules.

Get lists of items from entity property queries

Rules ships with the "Fetch entity by property" action that allows for simple entity queries.

  1. First, specify the entity type of the items that you want to retrieve as a list.
  2. Next, you can specify an arbitrary property of the entity type and compare it against a value of your choice.
    Be aware that the provided formular is a bit buggy: it doesn't automatically refresh when selecting a property. This can easily lead to confusion, as the value selection states "Data types: Select data of the type Unknown.". Fortunately there's a trick: just use the "Switch to data selection" button within the below area to force a reload of formular a voila.
  3. Now you should be able to specify the property value for the query either by using the "data selection" to select a variable or use "direct input mode" for providing a fixed value.
  4. Finally specify the maximum number of results to be fetched, as well as label and name for the result items variable.

Note, that we have recently fixed the support for all entity properties in the entity query action. You'll need a recent Rules dev version or apply this patch for using bundle-specific properties.

The following example checks a custom, boolean "Needs review" field on the node type to be checked (TRUE). The configured query will return a maximum number of 10 items of nodes that currently need review.

Rules Entity Query Example

We can now use the provided list of the "Fetch entity by property" action and pass it over to our previously created "Loop test" component. As we created the loop as a component, it is easily reusable within different reaction rules or other components.

Rules Entity Query Example

Fetching entities by property is a pretty straight-forward way for getting a list of data into Rules. Unfortunately, you only have very limited control over the data that gets returned by the query. In many cases, specifying a single property for the query won't be enough and you might also want to have control over the order in which to process the items.

Load a list of entity objects from a VBO view.

Views Bulk Operations to the rescue! This awesome module for one part allows configuring powerful management interfaces. As the name bulk operations indicate, a checkbox gets added to each of the items within a view and one or many operations can be performed in bulk on those items. Check out the Administration Views module, which uses VBO to spice up all administration views and listing pages.

You may already have guessed it, there is more to that neat module! By using the "Load a list of entity objects from a VBO View" action, we can bring the results of a view into Rules. And this is how it works:

Take any VBO-enabled view or configure your own. For demonstration purposes, I have setup a simple "My Nodes view" that lists all nodes of type article that have been published and sorts them by date. Pretty easy, just make sure that you add the "Bulk operations: Content" field to your view and you are all set.

Drupal rules vbo view config

To make the example a little bit more powerful, I have added a contextual filter for the Tags field. Note, that by using validation, it translates taxonomy term names into a term id. That's just some Views magic, so you can filter the list of nodes to those that have a term by using the term name within a argument/contextual filter.

Drupal rules vbo contextual filter

In the Rules configuration, we can now use the "Load a list of entity objects from a VBO View" action. The VBO-enabled view may be selected within the "View and Display" area. Also, as we have configured the contextual filter, we can pass a term name as argument to the View. Replacement patterns can be used, like in most areas of the powerful Rules module.

d8rules vbo action

In this article, we have learned how to use Rules loops and two methods for retrieving lists of data. The entity property query action is a simple and easy way to retrieve a list of items by specifying a single criterion. In a more advanced scenario, Views Bulk Operations may be used to use arbitrary Views results as a source of list items for the Rules module.

Feel free to post comments on this article and stay tuned for more blog posts on the Rules module.

If you want to dive deep into the Rules module, check out our Learn Rules in a Day! full-day training at DrupalCon Prague. Make sure to reserve a seat as there is a limited capacity!

Also check out our session on using Rules with popular web services like Twitter, Dropbox, flickr or Facebook.