function LoopExpression::executeWithState

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);
}