There are many situations in which a rule could trigger another rule. For example, If I have a rule that reacts to a Node being created, I can trigger that rule with another rule that created a node. The same should hold true for flagging nodes, but I have seen cases in which it does not. I have written 'single flag enforcement' rules that are triggered by a node being flagged, which un-flag all nodes flagged by that user, then re-flag only the triggering node. This rule should trigger itself over and over infinitely, but it does not. How does Drupal's Rules handle these situation? Is there a way to force rules to allow or disallow this form of cascading rule triggering (and potentially cause/prevent an infinite loop?)
1 Answer
It will not trigger recursively, Rules has inbuilt mechanism to prevent this. if you check the function rules_element_invoke_component you can see this.
function rules_element_invoke_component($arguments, RulesPlugin $element) {
....
// Handle recursion prevention.
if ($state->isBlocked($component)) {
return rules_log('Not evaluating @plugin %label to prevent recursion.', $replacements, RulesLog::INFO, $component);
}
$state->block($component);
....
}
-
Is that to say that a rule will not trigger itself, but it can trigger a different rule if the actions match triggers?– HoytmanJun 12, 2014 at 17:49
-
Yes, if you check the rules.state.inc file you can see
static protected $blocked = array();
where they are storing the rule id.– msmaniJun 12, 2014 at 19:06 -
It looks like a rule can be called multiple times on a single page load, but not as a 'child process' of the same rule.– HoytmanJun 13, 2014 at 12:26
-
-
1Yeah, as you said a rule cannot be called as a child process. But as per VBO, assume that we have a rule that triggers when a user is updated and we are performing bulk user update. Even though this is a bulk operation for us internally it is only loop. And after each updation if a rule is not triggering any other rule then it will unblock
$state->unblock($component);
, so it will trigger for the next update also.– msmaniJun 13, 2014 at 13:31