PreprocessEventService.php in Hook Event Dispatcher 8
File
src/Service/PreprocessEventService.php
View source
<?php
namespace Drupal\hook_event_dispatcher\Service;
use Drupal\hook_event_dispatcher\Event\Preprocess\PreprocessEntityEventInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
final class PreprocessEventService {
private $dispatcher;
private $mapper;
public function __construct(EventDispatcherInterface $dispatcher, PreprocessEventFactoryMapper $mapper) {
$this->dispatcher = $dispatcher;
$this->mapper = $mapper;
}
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);
}
}
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);
}
}