Commit 39428409 authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3271039: Check events against the triggered event name

parent 9c314d43
Loading
Loading
Loading
Loading
+14 −13
Original line number Diff line number Diff line
@@ -107,11 +107,13 @@ class EcaStorage extends ConfigEntityStorage {
   *
   * @param \Drupal\Component\EventDispatcher\Event $event
   *   The event object.
   * @param string $event_name
   *   The name of the event.
   *
   * @return \Drupal\eca\Entity\Eca[]
   *   The configurations, keyed by entity ID.
   */
  public function loadByEvent(Event $event): array {
  public function loadByEvent(Event $event, string $event_name): array {
    if (!isset($this->configByEvents)) {
      $cid = 'eca:storage:events';
      if ($cached = $this->cacheBackend->get($cid)) {
@@ -130,16 +132,16 @@ class EcaStorage extends ConfigEntityStorage {
          foreach ($eca->getUsedEvents() as $ecaEvent) {
            $eca_id = $eca->id();
            $plugin = $ecaEvent->getPlugin();
            $used_class = $plugin->drupalEventClass();
            $drupal_id = $plugin->drupalId();
            $wildcard = $plugin->lazyLoadingWildcard($eca_id, $ecaEvent);
            if (!isset($this->configByEvents[$used_class])) {
              $this->configByEvents[$used_class] = [$eca_id => [$wildcard]];
            if (!isset($this->configByEvents[$drupal_id])) {
              $this->configByEvents[$drupal_id] = [$eca_id => [$wildcard]];
            }
            elseif (!isset($this->configByEvents[$used_class][$eca_id])) {
              $this->configByEvents[$used_class][$eca_id] = [$wildcard];
            elseif (!isset($this->configByEvents[$drupal_id][$eca_id])) {
              $this->configByEvents[$drupal_id][$eca_id] = [$wildcard];
            }
            elseif (!in_array($wildcard, $this->configByEvents[$used_class][$eca_id], TRUE)) {
              $this->configByEvents[$used_class][$eca_id][] = $wildcard;
            elseif (!in_array($wildcard, $this->configByEvents[$drupal_id][$eca_id], TRUE)) {
              $this->configByEvents[$drupal_id][$eca_id][] = $wildcard;
            }
          }
        }
@@ -147,14 +149,13 @@ class EcaStorage extends ConfigEntityStorage {
        $this->logger->debug('Rebuilt cache array for EcaStorage::loadByEvent().');
      }
    }
    $class = get_class($event);
    if (empty($this->configByEvents[$class])) {
    if (empty($this->configByEvents[$event_name])) {
      return [];
    }
    $context = ['%event' => $class];
    $context = ['%event' => $event_name];
    if ($event instanceof ConditionalApplianceInterface) {
      $eca_ids = [];
      foreach ($this->configByEvents[$class] as $eca_id => $wildcards) {
      foreach ($this->configByEvents[$event_name] as $eca_id => $wildcards) {
        $wildcard_passed = FALSE;
        $context['%ecaid'] = $eca_id;
        foreach ($wildcards as $wildcard) {
@@ -170,7 +171,7 @@ class EcaStorage extends ConfigEntityStorage {
      }
    }
    else {
      $eca_ids = array_keys($this->configByEvents[$class]);
      $eca_ids = array_keys($this->configByEvents[$event_name]);
    }
    if ($eca_ids) {
      $context['%eca_ids'] = implode(', ', $eca_ids);
+3 −2
Original line number Diff line number Diff line
@@ -37,10 +37,11 @@ abstract class EcaBase implements EventSubscriberInterface {

  /**
   * @param \Drupal\Component\EventDispatcher\Event $event
   * @param string $event_name
   */
  public function onEvent(Event $event): void {
  public function onEvent(Event $event, string $event_name): void {
    try {
      $this->processor->execute($event);
      $this->processor->execute($event, $event_name);
    }
    catch (InvalidPluginDefinitionException | PluginNotFoundException $e) {
      // This is thrown during installation of eca and we can ignore this.
+3 −2
Original line number Diff line number Diff line
@@ -91,17 +91,18 @@ class Processor {
   * Main method that executes ECA config regards applying events.
   *
   * @param \Drupal\Component\EventDispatcher\Event $event
   * @param string $event_name
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function execute(Event $event): void {
  public function execute(Event $event, string $event_name): void {
    $context = [
      '%event' => get_class($event),
    ];
    /** @var \Drupal\eca\Entity\EcaStorage $eca_storage */
    $eca_storage = $this->entityTypeManager->getStorage('eca');
    foreach ($eca_storage->loadByEvent($event) as $eca) {
    foreach ($eca_storage->loadByEvent($event, $event_name) as $eca) {
      unset($context['%eventlabel'], $context['%eventid']);
      $context['%ecalabel'] = $eca->label();
      $context['%ecaid'] = $eca->id();