src/EventSubscriber/EntityListSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Repository\Configuration\CompanyEntityRepository;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Twig\Environment;
  8. class EntityListSubscriber implements EventSubscriberInterface
  9. {
  10.     private $twig;
  11.     private $companyEntityRepository;
  12.     public function __construct(Environment $twigCompanyEntityRepository $companyEntityRepository)
  13.     {
  14.         $this->twig $twig;
  15.         $this->companyEntityRepository $companyEntityRepository;
  16.     }
  17.     public function onKernelController(ControllerEvent $event)
  18.     {
  19.         // Ne pas exécuter pour les sous-requêtes
  20.         if (!$event->isMainRequest()) {
  21.             return;
  22.         }
  23.         // Ajouter la liste des entités aux variables globales de Twig
  24.         $this->twig->addGlobal('company_entities'$this->companyEntityRepository->findAll());
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             KernelEvents::CONTROLLER => 'onKernelController',
  30.         ];
  31.     }