You are here

final class PreprocessEventService in Hook Event Dispatcher 8

Class PreprocessEventService.

Hierarchy

Expanded class hierarchy of PreprocessEventService

2 files declare their use of PreprocessEventService
EntityEventTest.php in tests/src/Unit/Preprocess/EntityEventTest.php
OtherEventTest.php in tests/src/Unit/Preprocess/OtherEventTest.php
1 string reference to 'PreprocessEventService'
hook_event_dispatcher.services.yml in ./hook_event_dispatcher.services.yml
hook_event_dispatcher.services.yml
1 service uses PreprocessEventService
preprocess_event.service in ./hook_event_dispatcher.services.yml
Drupal\hook_event_dispatcher\Service\PreprocessEventService

File

src/Service/PreprocessEventService.php, line 11

Namespace

Drupal\hook_event_dispatcher\Service
View source
final class PreprocessEventService {

  /**
   * Event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  private $dispatcher;

  /**
   * Factory mapper.
   *
   * @var PreprocessEventFactoryMapper
   */
  private $mapper;

  /**
   * PreprocessEventService constructor.
   *
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
   *   Event dispatcher.
   * @param PreprocessEventFactoryMapper $mapper
   *   Factory mapper.
   */
  public function __construct(EventDispatcherInterface $dispatcher, PreprocessEventFactoryMapper $mapper) {
    $this->dispatcher = $dispatcher;
    $this->mapper = $mapper;
  }

  /**
   * Create and dispatch the event.
   *
   * @param string $hook
   *   The hook name.
   * @param array $variables
   *   Variables.
   */
  public function createAndDispatchKnownEvents($hook, array &$variables) {
    $factory = $this->mapper
      ->getFactory($hook);
    if ($factory === NULL) {
      return;
    }
    $event = $factory
      ->createEvent($variables);
    $this->dispatcher
      ->dispatch($event::name(), $event);
    if ($event instanceof PreprocessEntityEventInterface) {
      $this
        ->dispatchEntitySpecificEvents($event);
    }
  }

  /**
   * Dispatch the entity events.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Preprocess\PreprocessEntityEventInterface $event
   *   The event to dispatch.
   */
  private function dispatchEntitySpecificEvents(PreprocessEntityEventInterface $event) {
    $variables = $event
      ->getVariables();
    $withBundle = $event::name($variables
      ->getEntityBundle());
    $this->dispatcher
      ->dispatch($withBundle, $event);
    $withViewMode = $event::name($variables
      ->getEntityBundle(), $variables
      ->getViewMode());
    $this->dispatcher
      ->dispatch($withViewMode, $event);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PreprocessEventService::$dispatcher private property Event dispatcher.
PreprocessEventService::$mapper private property Factory mapper.
PreprocessEventService::createAndDispatchKnownEvents public function Create and dispatch the event.
PreprocessEventService::dispatchEntitySpecificEvents private function Dispatch the entity events.
PreprocessEventService::__construct public function PreprocessEventService constructor.