class EventComponentResolver

Resolves components that hold all reaction rules for a given event.

Hierarchy

Expanded class hierarchy of EventComponentResolver

1 string reference to 'EventComponentResolver'
rules.services.yml in ./rules.services.yml
rules.services.yml
1 service uses EventComponentResolver
rules.component_resolver.event_component in ./rules.services.yml
Drupal\rules\ComponentResolver\EventComponentResolver

File

src/ComponentResolver/EventComponentResolver.php, line 13

Namespace

Drupal\rules\ComponentResolver
View source
class EventComponentResolver implements RulesComponentResolverInterface {
    
    /**
     * The rules component entity storage.
     *
     * @var \Drupal\Core\Entity\EntityStorageInterface
     */
    protected $entityStorage;
    
    /**
     * The Rules expression manager.
     *
     * @var \Drupal\rules\Engine\ExpressionManagerInterface
     */
    protected $expressionManager;
    
    /**
     * Constructs the object.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\rules\Engine\ExpressionManagerInterface $expression_manager
     *   The rules expression plugin manager.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager, ExpressionManagerInterface $expression_manager) {
        $this->entityStorage = $entity_type_manager->getStorage('rules_reaction_rule');
        $this->expressionManager = $expression_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getMultiple(array $event_ids) {
        // @todo Improve this by adding a custom expression plugin that clones
        // the state after each rule, such that added variables added by one rule
        // are not interfering with the variables of another rule.
        $results = [];
        foreach ($event_ids as $event_id) {
            $action_set = $this->expressionManager
                ->createActionSet();
            // Only load active reaction rules - inactive (disabled) Rules should
            // not be executed, so we shouldn't even load them.
            $configs = $this->entityStorage
                ->loadByProperties([
                'events.*.event_name' => $event_id,
                'status' => TRUE,
            ]);
            if ($configs) {
                // We should only produce $results if there are loaded reaction rules.
                foreach ($configs as $config) {
                    $action_set->addExpressionObject($config->getExpression());
                }
                $results[$event_id] = RulesComponent::create($action_set);
            }
        }
        return $results;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
EventComponentResolver::$entityStorage protected property The rules component entity storage.
EventComponentResolver::$expressionManager protected property The Rules expression manager.
EventComponentResolver::getMultiple public function Gets multiple components. Overrides RulesComponentResolverInterface::getMultiple
EventComponentResolver::__construct public function Constructs the object.