You are here

public function GenericEventSubscriber::onRulesEvent in Rules 8.3

Reacts on the given event and invokes configured reaction rules.

Parameters

\Symfony\Component\EventDispatcher\Event $event: The event object containing context for the event.

string $event_name: The event name.

File

src/EventSubscriber/GenericEventSubscriber.php, line 105

Class

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

Namespace

Drupal\rules\EventSubscriber

Code

public function onRulesEvent(Event $event, $event_name) {

  // 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 this is a GenericEvent, get the context for the rule from the event
    // arguments.
    if ($event instanceof GenericEvent) {
      $value = $event
        ->getArgument($context_name);
    }
    else {
      $value = $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();
}