class LoopExpression

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

Plugin annotation


@RulesExpression(
  id = "rules_loop",
  label = @Translation("Loop")
)

Hierarchy

Expanded class hierarchy of LoopExpression

File

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

Namespace

Drupal\rules\Plugin\RulesExpression
View source
class LoopExpression extends ActionExpressionContainer {
    
    /**
     * {@inheritdoc}
     */
    public function defaultConfiguration() {
        return [
            // Default to 'list_item' as variable name for the list item.
'list_item' => 'list_item',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    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);
    }
    
    /**
     * {@inheritdoc}
     */
    public function checkIntegrity(ExecutionMetadataStateInterface $metadata_state, $apply_assertions = TRUE) {
        $violation_list = new IntegrityViolationList();
        if (empty($this->configuration['list'])) {
            $violation_list->addViolationWithMessage($this->t('List variable is missing.'));
            return $violation_list;
        }
        try {
            $list_definition = $metadata_state->fetchDefinitionByPropertyPath($this->configuration['list']);
        } catch (IntegrityException $e) {
            $violation_list->addViolationWithMessage($this->t('List variable %list does not exist. @message', [
                '%list' => $this->configuration['list'],
                '@message' => $e->getMessage(),
            ]));
            return $violation_list;
        }
        $list_item_name = $this->configuration['list_item'] ?? 'list_item';
        if ($metadata_state->hasDataDefinition($list_item_name)) {
            $violation_list->addViolationWithMessage($this->t('List item name %name conflicts with an existing variable.', [
                '%name' => $list_item_name,
            ]));
            return $violation_list;
        }
        if (!$list_definition instanceof ListDataDefinitionInterface) {
            $violation_list->addViolationWithMessage($this->t('The data type of list variable %list is not a list.', [
                '%list' => $this->configuration['list'],
            ]));
            return $violation_list;
        }
        // So far all ok, so continue with checking integrity in contained actions.
        // The parent implementation will take care of invoking pre/post traversal
        // metadata state preparations.
        $violation_list = parent::checkIntegrity($metadata_state, $apply_assertions);
        return $violation_list;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function allowsMetadataAssertions() {
        // As the list can be empty, we cannot ensure child expressions are
        // executed at all - thus no assertions can be added.
        return FALSE;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function prepareExecutionMetadataStateBeforeTraversal(ExecutionMetadataStateInterface $metadata_state) {
        try {
            $list_definition = $metadata_state->fetchDefinitionByPropertyPath($this->configuration['list']);
            $list_item_definition = $list_definition->getItemDefinition();
            $metadata_state->setDataDefinition($this->configuration['list_item'], $list_item_definition);
        } catch (IntegrityException $e) {
            // Silently eat the exception: we just continue without adding the list
            // item definition to the state.
        }
    }
    
    /**
     * {@inheritdoc}
     */
    protected function prepareExecutionMetadataStateAfterTraversal(ExecutionMetadataStateInterface $metadata_state) {
        // Remove the list item variable after the loop, it is out of scope now.
        $metadata_state->removeDataDefinition($this->configuration['list_item']);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ActionExpressionContainer::$actions protected property List of actions that will be executed.
ActionExpressionContainer::addAction public function Creates an action expression and adds it to the container. Overrides ActionExpressionContainerInterface::addAction
ActionExpressionContainer::addExpressionObject public function Adds an expression object. Overrides ExpressionContainerInterface::addExpressionObject
ActionExpressionContainer::deleteExpression public function Deletes an expression identified by the specified UUID in the container. Overrides ExpressionContainerInterface::deleteExpression
ActionExpressionContainer::getConfiguration public function Gets this plugin's configuration. Overrides ExpressionBase::getConfiguration
ActionExpressionContainer::getExpression public function Looks up the expression by UUID in this container. Overrides ExpressionContainerInterface::getExpression
ActionExpressionContainer::getIterator public function Returns an iterator for expressions in this container. Overrides ExpressionContainerInterface::getIterator
ActionExpressionContainer::__clone public function PHP magic __clone function.
ActionExpressionContainer::__construct public function Constructor. Overrides ExpressionBase::__construct
ExpressionBase::$configEntityId protected property The config entity this expression is associated with, if any.
ExpressionBase::$configuration protected property The plugin configuration.
ExpressionBase::$root protected property The root expression if this object is nested.
ExpressionBase::$uuid protected property The UUID of this expression.
ExpressionBase::$weight protected property The weight (list order) of this expression.
ExpressionBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ExpressionBase::execute public function Executes a rules expression. Overrides ExecutableInterface::execute
ExpressionBase::getFormHandler public function Returns the form handling class for this expression. Overrides ExpressionInterface::getFormHandler 2
ExpressionBase::getLabel public function The label of this expression element that can be shown in the UI. Overrides ExpressionInterface::getLabel 2
ExpressionBase::getRoot public function Returns the root expression if this expression is nested. Overrides ExpressionInterface::getRoot
ExpressionBase::getUuid public function Returns the UUID of this expression if it is nested in another expression. Overrides ExpressionInterface::getUuid
ExpressionBase::getWeight public function Returns the list order of this expression. Overrides ExpressionInterface::getWeight
ExpressionBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration 2
ExpressionBase::setRoot public function Set the root expression for this expression if it is nested. Overrides ExpressionInterface::setRoot
ExpressionBase::setUuid public function Sets the UUID of this expression in an expression tree. Overrides ExpressionInterface::setUuid
ExpressionBase::setWeight public function Sets the list order of this expression in an expression tree. Overrides ExpressionInterface::setWeight
ExpressionContainerBase::$expressionManager protected property The expression manager.
ExpressionContainerBase::$rulesDebugLogger protected property The rules debug logger channel.
ExpressionContainerBase::addExpression public function Creates and adds an expression. Overrides ExpressionContainerInterface::addExpression
ExpressionContainerBase::create public static function
ExpressionContainerBase::prepareExecutionMetadataState public function Prepares the execution metadata state by adding metadata to it. Overrides ExpressionInterface::prepareExecutionMetadataState
ExpressionContainerBase::sortByWeightProperty public static function Sorts an array of expressions by 'weight' property.
LoopExpression::allowsMetadataAssertions protected function Determines whether child-expressions are allowed to assert metadata. Overrides ExpressionContainerBase::allowsMetadataAssertions
LoopExpression::checkIntegrity public function Verifies that this expression is configured correctly. Overrides ExpressionContainerBase::checkIntegrity
LoopExpression::defaultConfiguration public function Gets default configuration for this plugin. Overrides ExpressionBase::defaultConfiguration
LoopExpression::executeWithState public function Execute the expression with a given Rules state. Overrides ExpressionInterface::executeWithState
LoopExpression::prepareExecutionMetadataStateAfterTraversal protected function Prepares execution metadata state after traversing through children. Overrides ExpressionContainerBase::prepareExecutionMetadataStateAfterTraversal
LoopExpression::prepareExecutionMetadataStateBeforeTraversal protected function Prepares execution metadata state before traversing through children. Overrides ExpressionContainerBase::prepareExecutionMetadataStateBeforeTraversal
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin_id of the plugin instance. 2