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.
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:
- 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 available 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 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.