Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin value resolver accept generat AdminInterface type #7847

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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