Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Is it possible to create a loop to evaluate a condition in a rule? It obviously possible to create loops to perform reactions, but I'd like to know if it's possible to loop through field values, e.g. a multi-value field like taxonomy, in order to see if a node is tagged with a term.
Comments
Comment #1
TR CreditAttribution: TR commentedThere are so many ways to accomplish this - it's impossible to pick the "right" one without knowing a lot more about your use case. But let me offer some ideas:
First, there is no way to add a loop in conditions. Loops are actions. But you don't necessarily need a loop ... for example, if you use the "List contains item" condition you could check to see that the term item is present in the multi-valued field list. My understanding is that this is exactly what you're trying to do.
Or you can use the "Data comparison" condition to compare your field value with a list of terms.
Or you could use an "Fetch entity by property" action to retrieve nodes that have that term, then loop over the resulting nodes.
Perhaps a more interesting thing to do is to use Rules components, specifically Rules and Rule Sets, both of which can be a combination of conditions and actions. You can use a Rule or a Rule Set as an action, which can be in a loop, so this gives you a way to use both conditions and actions within a loop. It also helps by putting your complex logic into a component, so your reaction rule is simplified because you're just looping over the component rather than making tests and setting data etc.
Comment #3
TR CreditAttribution: TR commented