function GenericEventSubscriber::onRulesEvent

Reacts on the given event and invokes configured reaction rules.

Parameters

object $event: The event object containing context for the event. In Drupal 9 this will be a \Symfony\Component\EventDispatcher\Event, In Drupal 10 this will be a \Symfony\Contracts\EventDispatcher\Event.

string $event_name: The event name.

File

src/EventSubscriber/GenericEventSubscriber.php, line 108

Class

GenericEventSubscriber
Subscribes to Symfony events and maps them to Rules events.

Namespace

Drupal\rules\EventSubscriber

Code

public function onRulesEvent(object $event, $event_name) {
    // @todo The 'object' type hint should be replaced with the appropriate
    // class once Symfony 4 is no longer supported, and the assert() should be
    // removed.
    assert($event instanceof SymfonyComponentEvent || $event instanceof SymfonyContractsEvent);
    // Get event metadata and the to-be-triggered events.
    $event_definition = $this->eventManager
        ->getDefinition($event_name);
    $handler_class = $event_definition['class'];
    $triggered_events = [
        $event_name,
    ];
    if (is_subclass_of($handler_class, RulesConfigurableEventHandlerInterface::class)) {
        $qualified_event_suffixes = $handler_class::determineQualifiedEvents($event, $event_name, $event_definition);
        foreach ($qualified_event_suffixes as $qualified_event_suffix) {
            // This is where we add the bundle-specific event suffix, e.g.
            // rules_entity_insert:node--page if the content entity was type 'page'.
            $triggered_events[] = "{$event_name}--{$qualified_event_suffix}";
        }
    }
    // Setup the execution state.
    $state = ExecutionState::create();
    foreach ($event_definition['context_definitions'] as $context_name => $context_definition) {
        // If there is a getter method set in the event definition, use that.
        // @see https://www.drupal.org/project/rules/issues/2762517
        if ($context_definition->hasGetter()) {
            $value = $event->{$context_definition->getGetter()}();
        }
        elseif ($event instanceof GenericEvent) {
            $value = $event->getArgument($context_name);
        }
        else {
            $getter = function ($property) {
                return $this->{$property};
            };
            $value = $getter->call($event, $context_name);
        }
        $state->setVariable($context_name, $context_definition, $value);
    }
    $components = $this->componentRepository
        ->getMultiple($triggered_events, 'rules_event');
    foreach ($components as $component) {
        $this->rulesDebugLogger
            ->info('Reacting on event %label.', [
            '%label' => $event_definition['label'],
            'element' => NULL,
            'scope' => TRUE,
        ]);
        $component->getExpression()
            ->executeWithState($state);
        $this->rulesDebugLogger
            ->info('Finished reacting on event %label.', [
            '%label' => $event_definition['label'],
            'element' => NULL,
            'scope' => FALSE,
        ]);
    }
    $state->autoSave();
}