function ContainerAwareEventDispatcher::addSubscriber

Same name in other branches
  1. 9 core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher::addSubscriber()
  2. 8.9.x core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher::addSubscriber()
  3. 11.x core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher::addSubscriber()

phpcs:ignore Drupal.Commenting.FunctionComment.VoidReturn

Return value

void

File

core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php, line 268

Class

ContainerAwareEventDispatcher
A performance optimized container aware event dispatcher.

Namespace

Drupal\Component\EventDispatcher

Code

public function addSubscriber(EventSubscriberInterface $subscriber) {
    foreach ($subscriber->getSubscribedEvents() as $event_name => $params) {
        if (is_string($params)) {
            $this->addListener($event_name, [
                $subscriber,
                $params,
            ]);
        }
        elseif (is_string($params[0])) {
            $this->addListener($event_name, [
                $subscriber,
                $params[0],
            ], $params[1] ?? 0);
        }
        else {
            foreach ($params as $listener) {
                $this->addListener($event_name, [
                    $subscriber,
                    $listener[0],
                ], $listener[1] ?? 0);
            }
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.