Using Rules, how can you perform a set of Rules Actions WHILE (some condition = TRUE)?

About that condition: I'm not talking here about a Rules Condition ... Instead I mean some sort of boolean expression that evaluates to either TRUE or FALSE.

Also, even though Rules has the concept of "Lists and Loops" (for which you can typically iterate over each item of such list via a Rules Loop), that's not what my question is about either.

up vote 1 down vote accepted

I'm not aware how to implement such WHILE-construction by only using Rules. However, if you also enable the Conditional Rules module, you can implement such WHILE-construction.

Example

Consider this rule (in Rules export format):

{ "rules_execute_a_while_loop" : {
    "LABEL" : "Execute a while loop",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_conditional" ],
    "ON" : { "node_insert" : [], "node_update" : [] },
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "article" : "article" } } } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_free_format_nr" } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "Value specified for \u0022Free format nr\u0022 is: [node:field-free-format-nr]" } },
      { "variable_add" : {
          "USING" : { "type" : "integer", "value" : "1" },
          "PROVIDE" : { "variable_added" : { "loop_counter" : "Loop counter" } }
        }
      },
      {
        "WHILE" : { "NOT data_is" : {
            "data" : [ "loop-counter" ],
            "op" : "\u003E",
            "value" : [ "node:field-free-format-nr" ]
          }
        },
        "DO" : [
          { "drupal_message" : { "message" : "Do your thing for \u0022loop_counter\u0022 value = [loop-counter:value]" } },
          { "data_calc" : {
              "USING" : { "input_1" : [ "loop-counter" ], "op" : "+", "input_2" : "1" },
              "PROVIDE" : { "result" : { "incremented_counter" : "Incremented counter" } }
            }
          },
          { "data_set" : { "data" : [ "loop-counter" ], "value" : [ "incremented-counter" ] } }
        ]
      }
    ]
  }
}

Some more details about this rule:

  • Rules Events: After creating a new node, or after updating an existing node.
  • Rules Conditions:
    • Content is of type "Article".
    • Entity has field, for field with machine name field_free_format_nr (which is of type integer).

The above Rules Events and/or Rules Conditions can be more or less anything (I just used some of the popular ones). The actual magic is in the Rules Actions:

  • Show a message on the site, just for demo / testing purposes, to show the value enter for the field_free_format_nr field.
  • Add a variable, with machine name loop_counter, and initial value 1.
  • While: NOT Data comparison, whereas the test is to check if NOT (loop-counter is greater than [node:field-free-format-nr]), in which case all of the below Rules Actions are performed:

    • Show a message on the site, just for demo / testing purposes, to show a message like

    Do your thing for "loop_counter" value = [loop-counter:value]

    • Calculate a value, to increment loop_counter with 1, and (temporary) store the result in incremented_counter
    • Set a data value, to copy the value of incremented_counter in loop_counter

BTW, it could well be that the last 2 Rules Actions can be consolidates in a single one, though using 2 Rules Actions should be easier to understand what's happening.

Demo

With the above rule enabled, these are the messages shown if I create or update an article, and specify a value of 3 for the field_free_format_nr field:

Value specified for "Free format nr" is: 3
Do your thing for "loop_counter" value = 1
Do your thing for "loop_counter" value = 2
Do your thing for "loop_counter" value = 3

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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