Skip to content

Commit

Permalink
Set twig globals after first admin instanciation
Browse files Browse the repository at this point in the history
Fixes #8120
  • Loading branch information
Antoine Roué committed Nov 19, 2023
1 parent 724c4fc commit 7954685
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 198 deletions.
19 changes: 19 additions & 0 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
83 changes: 0 additions & 83 deletions src/EventListener/AdminEventListener.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/EventListener/ConfigureCRUDControllerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function onKernelController(ControllerEvent $event): void
$request = $event->getRequest();

$controller->configureAdmin($request);

$controller->setTwigGlobals($request);
}

public static function getSubscribedEvents(): array
Expand Down
8 changes: 0 additions & 8 deletions src/Resources/config/event_listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};
27 changes: 27 additions & 0 deletions tests/Controller/CRUDControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

/**
* @author Andrej Hudec <[email protected]>
Expand Down Expand Up @@ -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(
Expand Down
107 changes: 0 additions & 107 deletions tests/EventListener/AdminEventListenerTest.php

This file was deleted.

16 changes: 16 additions & 0 deletions tests/EventListener/ConfigureCRUDControllerListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Twig\Environment;

final class ConfigureCRUDControllerListenerTest extends TestCase
{
Expand Down Expand Up @@ -64,6 +65,21 @@ public function testItConfiguresCRUDController(): void
->with($request)
->willReturn($admin);

$twig = $this->createMock(Environment::class);
$container->set('twig', $twig);

$matcher = static::exactly(2);
$twig
->expects($matcher)
->method('addGlobal')
->willReturnCallback(static function (string $name) use ($matcher) {
match ($matcher->getInvocationCount()) {
1 => static::assertSame($name, 'admin'),
2 => static::assertSame($name, 'base_template'),
default => throw new \LogicException('Exactly 2 calls'),
};
});

$this->listener->onKernelController($controllerEvent);
}
}

0 comments on commit 7954685

Please sign in to comment.