Last updated November 8, 2011. Created by Itangalo on October 5, 2011.
Edited by videographics. Log in to edit this page.
An important functionality in Rules 2 is the ability to use lists of data to create loops. Loops allow performing actions on each of the items included in a list, for example to send e-mail to all users in a multiple-value user reference field, or unpublish all comments belonging to a certain node.
To use loops, you must have a list available in your rules configuration. Loops are created in the following way:
- In the action section of the rule configuration, click the add loop link.
- At the top of the loop configuration screen, select the list you want to loop over.
- At the bottom of the loop configuration screen, provide a label and machine name to use for handling each individual item in the list, while inside the loop.
- In the rule configuration, use the add action link on the same row as your loop to add actions to the loop. The per-item variable will be avaialble in the action configuration.
- You can, as usual, drag and drop to reorder actions in your rule. Any actions indented under your loop will be a part of that loop.
Note 1: If you want to perform conditional actions on items inside a loop, you will have to call a rule component that does the condition checks – you cannot add conditions inside the loop.
Note 2: If you loop over lists with hundereds or thousands of entries, you may very well run out of memory on your site. (There is work going on to add batching functionality to loops.)
Note 3: If your loop contains a "Create a new entity" element followed by one or more "Set a data value" elements to set data in the new entity, then you must follow the last "Set a data value" with a "Save entity" element with the "Force saving immediately" option checked in order to ensure the values are updated in the new entity.
Comments
Conditions in loops
I have been trying for a while now to do this and can't see a way to. My use case is that I want to send an email to users of a certain role, but only those who have checked at particular field in their user profile. I can create an action that loads all users of that role into a list, I can create a loop that goes through and removes users from that list one by one, and I can create a condition set that filters by my field, but I can't bring them all together to create a list including just the users I need that I can use in an email action. Is this possible?
Question posted in issue queue
I'm asking this question in #1379306: How to evaluate a condition iteratively?
Load a list with Views
In this case you could also use VBO and its neat functionality to load lists of entities using Views.
In short: If you can create a list of the users you're interested in with Views, you can use VBO to load that list in Rules.
There's a screencast showing how to do that here: http://dev.nodeone.se/en/using-vbo-to-load-list-of-objects-into-rules
Good luck!
Want to help make Drupal better? Answer a question or two in the forum!
how to programmatically
how to programmatically create data for loop?