Skip to content

Commit

Permalink
Merge 4.x into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI authored Nov 21, 2023
2 parents 4295134 + 75d7116 commit c649e6c
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 200 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [4.28.1](https://github.com/sonata-project/SonataAdminBundle/compare/4.28.0...4.28.1) - 2023-11-20
### Fixed
- [[#8125](https://github.com/sonata-project/SonataAdminBundle/pull/8125)] Needing to submit form twice when using CRUDController::render() ([@AntoineRoue](https://github.com/AntoineRoue))

## [4.28.0](https://github.com/sonata-project/SonataAdminBundle/compare/4.27.1...4.28.0) - 2023-10-19
### Changed
- [[#8116](https://github.com/sonata-project/SonataAdminBundle/pull/8116)] Automatically detect `base_template` and `admin` twig variable ([@core23](https://github.com/core23))
Expand Down
19 changes: 19 additions & 0 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,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
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ private function getDefaultMethodCalls(ContainerBuilder $container, string $serv
\E_USER_DEPRECATED
);

// NEXT_MAJOR: Uncomment the exception instead of the deprecation.
// throw new InvalidArgumentException(sprintf('Missing tag information "model_class" on service "%s".', $serviceId));
// NEXT_MAJOR: Uncomment the exception instead of the deprecation.
// throw new InvalidArgumentException(sprintf('Missing tag information "model_class" on service "%s".', $serviceId));
} else {
$methodCalls[] = ['setModelClass', [$modelClass]];

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 <pulzarraider@gmail.com>
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 c649e6c

Please sign in to comment.