2

I have a Rules loop in which a variable parameter is modified. The variable was created before the loop, and added as parameter in the loop.

However, If I check the value of the variable after the loop, it is unchanged.

Is this by design or what causes this?

0

2 Answers 2

0

Have a look at this example of a Rule (in Rules export format), which is a variation of what you're asking for:

{ "rules_show_list_of_fields" : {
    "LABEL" : "Show list of fields",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_insert" : [], "node_update" : [] },
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "article" : "article" } } } }
    ],
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "StartOfList-" },
          "PROVIDE" : { "variable_added" : { "email_list" : "List of eMail IDs" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "node:field-email-addresses" ] },
          "ITEM" : { "current_email" : "Current eMail" },
          "DO" : [
            { "data_set" : {
                "data" : [ "email-list" ],
                "value" : "[email-list:value][current-email:value]-"
              }
            }
          ]
        }
      },
      { "data_set" : { "data" : [ "email-list" ], "value" : "[email-list:value]EndOfList" } },
      { "drupal_message" : { "message" : "The value of the email_list variable is: [email-list:value]." } }
    ]
  }
}

Here is a summary of what this rule does:

  • Add a new variable email_list.
  • Init the email_list value with some prefix.
  • For each item within the loop, append the value of that item, followed by a separator character.
  • Add a suffix to the email_list value.

In my case I'm using a multi value text field with machine name field_email_addresses, which contains eMail Ids, and just want to dispay a Status message with a concatenation of those eMail IDs, separated by a "-", and with a prefix (=StartOfList) / suffix (=EndOfList). And I also use Rules Events "Update existing node".

BTW, You could use this rule as a template to group multiple eMails (to individual eMail IDs) in a single eMail to all of those eMail IDs.

Demo

Here is how the Status message looks like after you create a new node, or update an existing node (of type Article):

The value of the email_list variable is: [email protected]@example.com-EndOfList.

Conclusion

The variable was created before the loop, and modified in the loop. And I display the value of the variable after the loop, it is changed like I wanted it to be changed.

As per what is shown in my answer here, it seems something is wrong in your rule (which you did not show in your question).

PS: I assume this question is about D7 ...

0
0

I agree with the conclusion of Pierre.Vriens. However, I have stumbled over a similar behavior as the OP.

If you alter the number of list items within the loop, the loop counter is not affected.

Example:

* List A contains three elements
* Loop the list
  * Remove one of the elements
* End loop

The loop will run three times, regardless of the change of the number of items in the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.