function LoopExpression::executeWithState

Execute the expression with a given Rules state.

Note that this does not auto-save any changes.

Parameters

\Drupal\rules\Context\ExecutionStateInterface $state: The state with all the execution variables in it.

Return value

null|bool The expression may return a boolean value after execution, this is used by conditions that return their evaluation result.

Overrides ExpressionInterface::executeWithState

File

src/Plugin/RulesExpression/LoopExpression.php, line 35

Class

LoopExpression
Holds a set of actions that are executed over the iteration of a list.

Namespace

Drupal\rules\Plugin\RulesExpression

Code

public function executeWithState(ExecutionStateInterface $state) {
  $list_data = $state->fetchDataByPropertyPath($this->configuration['list']);
  $list_item_name = $this->configuration['list_item'];
  $this->rulesDebugLogger
    ->info('Looping over the list items of %selector.', [
    '%selector' => $this->configuration['list_item'],
    'element' => $this,
  ]);
  foreach ($list_data as $item) {
    $state->setVariableData($list_item_name, $item);
    // Use the iterator to ensure the conditions are sorted.
    foreach ($this as $action) {
      /** @var \Drupal\rules\Engine\ExpressionInterface $action */
      $action->executeWithState($state);
    }
  }
  // After the loop the list item is out of scope and cannot be used by any
  // following actions.
  $state->removeVariable($list_item_name);
}