Skip to content

Commit

Permalink
admin value resolver accept generat AdminInterface type
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert authored and jordisala1991 committed Jun 15, 2022
1 parent 384cbc4 commit cddc750
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
23 changes: 23 additions & 0 deletions docs/cookbook/recipe_decouple_crud_controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ You can add your Admin as parameter of the action::
}
}

Or if you have a reusable action for **all** admin::

// src/Controller/CarAdminController.php

namespace App\Controller;

use Sonata\AdminBundle\Admin\AdminInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

final class CloneAdminController
{
public function clone(AdminInterface $admin, Request $request)
{
$object = $admin->getSubject();

// ...

$request->getSession()->getFlashBag()->add('sonata_flash_success', 'Cloned successfully');

return new RedirectResponse($admin->generateUrl('list'));
}
}

Or you can use ``AdminFetcherInterface`` service to fetch the admin from the request, in this example we transformed
the controller to make it Invokable::

Expand Down
7 changes: 6 additions & 1 deletion src/ArgumentResolver/AdminValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ public function __construct(AdminFetcherInterface $adminFetcher)
public function supports(Request $request, ArgumentMetadata $argument): bool
{
$type = $argument->getType();
if (null === $type || !is_subclass_of($type, AdminInterface::class)) {

if (null === $type) {
return false;
}

if (AdminInterface::class !== $type && !is_subclass_of($type, AdminInterface::class)) {
return false;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/ArgumentResolver/AdminValueResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\AdminBundle\Tests\ArgumentResolver;

use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\ArgumentResolver\AdminValueResolver;
use Sonata\AdminBundle\Request\AdminFetcher;
Expand Down Expand Up @@ -93,6 +94,12 @@ public function supportDataProvider(): iterable
$request,
new ArgumentMetadata('_sonata_admin', PostAdmin::class, false, false, null),
];

yield 'Admin can fetch by interface' => [
true,
$request,
new ArgumentMetadata('_sonata_admin', AdminInterface::class, false, false, null),
];
}

public function testResolveAdmin(): void
Expand Down

0 comments on commit cddc750

Please sign in to comment.