From 25e6b40cf7454b56c621828229674aaa722568a1 Mon Sep 17 00:00:00 2001 From: core23 Date: Thu, 7 Dec 2023 18:58:03 +0100 Subject: [PATCH] Fix clashing global twig variable --- src/Controller/CRUDController.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Controller/CRUDController.php b/src/Controller/CRUDController.php index b7245b8d91..888664370b 100644 --- a/src/Controller/CRUDController.php +++ b/src/Controller/CRUDController.php @@ -994,10 +994,7 @@ final public function configureAdmin(Request $request): void */ final public function setTwigGlobals(Request $request): void { - $twig = $this->container->get('twig'); - \assert($twig instanceof Environment); - - $twig->addGlobal('admin', $this->admin); + $this->setTwigGlobal('admin', $this->admin); if ($this->isXmlHttpRequest($request)) { $baseTemplate = $this->templateRegistry->getTemplate('ajax'); @@ -1005,7 +1002,7 @@ final public function setTwigGlobals(Request $request): void $baseTemplate = $this->templateRegistry->getTemplate('layout'); } - $twig->addGlobal('base_template', $baseTemplate); + $this->setTwigGlobal('base_template', $baseTemplate); } /** @@ -1543,6 +1540,18 @@ final protected function checkParentChildAssociation(Request $request, object $o } } + private function setTwigGlobal(string $name, mixed $value): void + { + $twig = $this->container->get('twig'); + \assert($twig instanceof Environment); + + try { + $twig->addGlobal($name, $value); + } catch (\LogicException) { + // Variable already set + } + } + private function getBatchActionExecutable(string $action): callable { $batchActions = $this->admin->getBatchActions();