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.
We can emulate a for loop by an action "create a counter array" like this pseudocode:
list=counter-array(number)
foreach(list) do stuff
where counter-array(5) == {0,1,2,3,4}
See: #1525596: [Meta] Rules loops extended
Related: #1301022: Action: type conversion for numbers, integers, and strings
Comments
Comment #1
geek-merlin@fago: what do you think about a patch directly rolled from #1329904-11: Loops: while and tail recursion support?
Comment #1.0
geek-merlinadd related
Comment #1.1
geek-merlinadded meta
Comment #2
geek-merlinbanzai! herre's the patch, with test that passes on my box.
Comment #3
mitchell CreditAttribution: mitchell commentedComment #4
mitchell CreditAttribution: mitchell commented[edit] next comment has more useful info
Comment #5
mitchell CreditAttribution: mitchell commentedThere are three different versions of this for loops implementation (not including the Views based ones in Rules Bonus Pack and VBO): this, #1329904-11: Loops: while and tail recursion support, and Rules Array Create, that all have practically the same exact code.
This implementation is actually a patch with a test (props axel.rutz!), so the only part remaining is finding a suitable name for the action. Here are four different names to choose from (in no particular order):
For future reference, my personal opinion is that this should simply be put in 'Calculate a value' as the multiplication of an integer by a NULL value. I outlined this here: #1540634: Calculate a value: support strings, lists, & files.
Comment #6
geek-merlin#5: i see the beauty of said approach, and as a mathematician i like it.
i'm not so sure if everyone can appreciate and understand that beauty.
so it's up to fago to point an architectural direction.
Comment #7
klausiindentation error. Also, this needs some description to explain what is happening here.
comma is missing on the last array element, see http://drupal.org/coding-standards#array
indentation error
please break that line up for readability.
But otherwise I think such an action would make sense. Personally I like the name "Generate a list from an integer" best.
Comment #8
mesch CreditAttribution: mesch commentedMy original action function can be reduced from 5 to 1 lines of code by using the "range" function as follows:
Comment #9
mitchell CreditAttribution: mitchell commentedStatus?
Comment #10
kenyan CreditAttribution: kenyan commentedThis works perfectly.
I really wish I'd seen this patch before chasing my tail trying to write the loop in php.
Use case (If no one minds).
I have a field collection that holds 3 fields (multiple collection):
1) full names
2) email
3) number of invites (integer)
Additionaly I have an event content type.
When a user creates an event, he sends invites using signup to an email list with rules handling the html emails.
Rules then sends the user to the content type the field collection is attached to.
Here, the user then fills out a friends who he can send a certain number of invites to 'share' with friends (number of invites).
I use rules to create a new node of a content type called shared invites. A new node is created for each of 'number of invites'.
This patch has of course made this oh so easy for me to convert the integer into a list which I can then loop through to create the invites for each friend to share.
All I can say is, thanks for the patch big time.
Comment #11
geek-merlinhere's #2 re-rolled with all the valuable input from #7 and #8.
Please review so we can finally get this in.
Comment #13
geek-merlinC'mon, be a good testbot...
Comment #14
geek-merlinComment #14.0
geek-merlinmoved related
Comment #15
JuliaKoelsch CreditAttribution: JuliaKoelsch commentedI ran into an issue trying to use the Role Reference value in a Rule action because the role reference field provides an integer, not a list (https://drupal.org/node/1617880). I applied the patch in #13, added an action to create a list from the field value, and then was able to use the value. In other words, the patch works for me.
I feel a bit presumptuous changing the status because I don't know that anyone else but me and testbot have tested it, but it looks like it was code reviewed above and requested changes were made. So, changing to RTBC in hope that this gets into the next release. But feel free to change back if I jumped the gun. Thanks!
Comment #16
fagoCode looks good, but I think the description and label is confusing and does not tell you what it does. What kind of list does it create? What's in there?
Also, would it make sense to have similar options as php's range() function has?
Comment #17
eileenmcnaughton CreditAttribution: eileenmcnaughton commentedhmm - this seems to be hung up for a long time on agreeing a label. I guess it creates a list of integers up to the value of the integer. But, I think I'll just create a little custom module that implements it as the code seems to be on this thread to do that
Comment #18
eileenmcnaughton CreditAttribution: eileenmcnaughton commentedhere it is as a mini-module if this helps anyone
https://github.com/eileenmcnaughton/rules_convert_list