From 1535f2453e3a994f13aa0be845ab551f8d350023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Rou=C3=A9?= Date: Sun, 19 Nov 2023 13:59:47 +0100 Subject: [PATCH] Set twig globals after first admin instanciation Fixes #8120 --- src/Controller/CRUDController.php | 19 +++++ src/EventListener/AdminEventListener.php | 83 ------------------- .../ConfigureCRUDControllerListener.php | 2 + src/Resources/config/event_listener.php | 8 -- tests/Controller/CRUDControllerTest.php | 29 ++++++- 5 files changed, 49 insertions(+), 92 deletions(-) delete mode 100644 src/EventListener/AdminEventListener.php diff --git a/src/Controller/CRUDController.php b/src/Controller/CRUDController.php index 998d937855..b7245b8d91 100644 --- a/src/Controller/CRUDController.php +++ b/src/Controller/CRUDController.php @@ -989,6 +989,25 @@ final public function configureAdmin(Request $request): void $this->templateRegistry = $this->admin->getTemplateRegistry(); } + /** + * Add twig globals which are used in every template. + */ + final public function setTwigGlobals(Request $request): void + { + $twig = $this->container->get('twig'); + \assert($twig instanceof Environment); + + $twig->addGlobal('admin', $this->admin); + + if ($this->isXmlHttpRequest($request)) { + $baseTemplate = $this->templateRegistry->getTemplate('ajax'); + } else { + $baseTemplate = $this->templateRegistry->getTemplate('layout'); + } + + $twig->addGlobal('base_template', $baseTemplate); + } + /** * Renders a view while passing mandatory parameters on to the template. * diff --git a/src/EventListener/AdminEventListener.php b/src/EventListener/AdminEventListener.php deleted file mode 100644 index 64bbb57bd6..0000000000 --- a/src/EventListener/AdminEventListener.php +++ /dev/null @@ -1,83 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Sonata\AdminBundle\EventListener; - -use Sonata\AdminBundle\Request\AdminFetcherInterface; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Event\KernelEvent; -use Symfony\Component\HttpKernel\KernelEvents; -use Twig\Environment; - -/** - * @author Christian Gripp - */ -final class AdminEventListener implements EventSubscriberInterface -{ - public function __construct(private Environment $twig, private AdminFetcherInterface $adminFetcher) - { - } - - public static function getSubscribedEvents(): array - { - return [ - KernelEvents::REQUEST => [['onKernelRequest', -50]], - ]; - } - - public function onKernelRequest(KernelEvent $event): void - { - $request = $event->getRequest(); - - try { - $admin = $this->adminFetcher->get($request); - } catch (\InvalidArgumentException) { - return; - } - - $this->addVariable('admin', $admin); - - $templateRegistry = $admin->getTemplateRegistry(); - - if ($this->isXmlHttpRequest($request)) { - $baseTemplate = $templateRegistry->getTemplate('ajax'); - } else { - $baseTemplate = $templateRegistry->getTemplate('layout'); - } - - $this->addVariable('base_template', $baseTemplate); - } - - /** - * Returns true if the request is a XMLHttpRequest. - * - * @return bool True if the request is an XMLHttpRequest, false otherwise - */ - private function isXmlHttpRequest(Request $request): bool - { - if ($request->isXmlHttpRequest()) { - return true; - } - - return null !== $request->attributes->get('_xml_http_request'); - } - - private function addVariable(string $name, mixed $value): void - { - try { - $this->twig->addGlobal($name, $value); - } catch (\LogicException) { - } - } -} diff --git a/src/EventListener/ConfigureCRUDControllerListener.php b/src/EventListener/ConfigureCRUDControllerListener.php index d7fb30b286..cc3179abfc 100644 --- a/src/EventListener/ConfigureCRUDControllerListener.php +++ b/src/EventListener/ConfigureCRUDControllerListener.php @@ -38,6 +38,8 @@ public function onKernelController(ControllerEvent $event): void $request = $event->getRequest(); $controller->configureAdmin($request); + + $controller->setTwigGlobals($request); } public static function getSubscribedEvents(): array diff --git a/src/Resources/config/event_listener.php b/src/Resources/config/event_listener.php index c585909e9c..105ddc77bb 100644 --- a/src/Resources/config/event_listener.php +++ b/src/Resources/config/event_listener.php @@ -13,19 +13,11 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; -use Sonata\AdminBundle\EventListener\AdminEventListener; use Sonata\AdminBundle\EventListener\ConfigureCRUDControllerListener; return static function (ContainerConfigurator $containerConfigurator): void { $containerConfigurator->services() - ->set('sonata.admin.event_listener.admin_event', AdminEventListener::class) - ->tag('kernel.event_subscriber') - ->args([ - new ReferenceConfigurator('twig'), - new ReferenceConfigurator('sonata.admin.request.fetcher'), - ]) - ->set('sonata.admin.event_listener.configure_crud_controller', ConfigureCRUDControllerListener::class) ->tag('kernel.event_subscriber'); }; diff --git a/tests/Controller/CRUDControllerTest.php b/tests/Controller/CRUDControllerTest.php index 4f23eabb4e..4b4f3ae65e 100644 --- a/tests/Controller/CRUDControllerTest.php +++ b/tests/Controller/CRUDControllerTest.php @@ -70,10 +70,11 @@ use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer; use Symfony\Component\Serializer\Serializer; -use Symfony\Component\Validator\Constraints\Uuid; use Symfony\Component\Validator\ConstraintViolation; +use Symfony\Component\Validator\Constraints\Uuid; use Symfony\Contracts\Translation\TranslatorInterface; use Twig\Environment; +use Twig\Loader\ArrayLoader; /** * @author Andrej Hudec @@ -383,6 +384,32 @@ public function testConfigureAdminWithoutTemplateRegistryThrowsException(): void $controller->configureAdmin($this->request); } + public function testSetTwigGlobals(): void + { + $twig = new Environment(new ArrayLoader([])); + $this->container->set('twig', $twig); + + $this->controller->setTwigGlobals($this->request); + + $globals = $twig->getGlobals(); + static::assertSame($this->admin, $globals['admin']); + static::assertSame('@SonataAdmin/standard_layout.html.twig', $globals['base_template']); + } + + public function testSetTwigGlobalsWithAjaxRequest(): void + { + $this->request->request->set('_xml_http_request', true); + + $twig = new Environment(new ArrayLoader([])); + $this->container->set('twig', $twig); + + $this->controller->setTwigGlobals($this->request); + + $globals = $twig->getGlobals(); + static::assertSame($this->admin, $globals['admin']); + static::assertSame('@SonataAdmin/ajax_layout.html.twig', $globals['base_template']); + } + public function testGetBaseTemplate(): void { static::assertSame(