Last updated July 5, 2013. Created on October 5, 2011.
Edited by illeace, mitchell, DeNelo, 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.

Simply put:

for each item in collection:
  do something to item

To use loops, you must have a list available in your rules configuration. Loops are created in the following way:

  1. In the action section of the rule configuration, click the add loop link.
  2. At the top of the loop configuration screen, select the list you want to loop over.
  3. 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.
  4. 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 available in the action configuration.
  5. 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 hundreds 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.

Related videos

Looking for support? Visit the forums, or join #drupal-support in IRC.


dafeder’s picture

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.

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?

rfay’s picture

Itangalo’s picture

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:

Good luck!

xandeadx’s picture

how to programmatically create data for loop?

fehin’s picture

Great instruction.

a.milkovsky’s picture

I use rules component in loop
Question. How can I return value from loop component?

(Drupal commerce)
I have two prices in my products. I want to use price 2 field if total of price2 in cart is bigger than 1000$.

I want to count the total price in my cart(Drupal commerce).
1. I create rule with condition Calculating the sell price of a product.
2. I create value to store total
3. I start my loop in cart for each line-item.
4. In loop for each line item I use rule component to get price2 of related product. I send line item and my value as parameters
5. In the component I Set a data value to price2 * quantity.
6. ??? I want to return calculated price of product to main rule and go to nwxt line item in loop

tuccio’s picture

Check out the Conditional Rules module: .
It really deserves to be merged into Rules.

a.milkovsky’s picture

thanks, I've already did my task programmatically
but thanks for the link, good module

flaviovs’s picture

One wants a rule that loops over all users on her site. This is easy to create by following the instructions above. But when one try to execute the rule, or assign it to some event, Drupal asks her to input the list of users to loop over. Of course, this is not what one wants, which is to run the rule on all existing users on the site, by the time the rule is run.

So the question that remains unanswered is: how one populate the list of things to loop over?

Could you please put up an example that loops over a dynamic list of entities (e.g. nodes, vocabularies, terms, etc.)?


pelicani’s picture

another example we are working to solve is looking over all the items in the cart
finding one
then looping over the quantity of that item.

we are creating entities and need multiple when multiple are selected.
still working on setting this up to work.

Web Developer : Philadelphia, PA

flaviovs’s picture

After a few days of research, I think that I came up with a answer to how to loop over a dynamic list of things.

The answer is: you can't.

At least, not out-of-box.

But with the help of Rules Bonus Pack and/or View Rules you can loop over the result of any view. The loophole (no pun intended) is now closed.


sportel’s picture

My case: I have an e-mail field added to a content type. I've set the number of values to unlimited, so a when a user clicks "Add another item", more e-mail addresses can be added.
I want rules to create user accounts for all the e-mail addresses added, so I thought to accomplish this by looping over the values of this field, but it doesn't work. Doesn't Rules see this field as a list? Help please.



mksweet’s picture

Rules can only loop over arrays. So if you have a variable you need to loop over, use this little module to convert it to an array first.