AdminContext missing entity in custom action
I added a custom action to a crudcontroller on the detail page of an entity. (using EA 4.20, SF 7.2, PHP 8.3)
In this action I need the entity instance so, I got the AdminContext using
public function myAction(AdminContext $context)
But when loaded this AdminContext does not have the crudcontroller and entityId available. The entity instance is null.
In a base action such as 'detail' it is all properly loaded in the AbstractCrudController.
It seems that the AdminRouterSubscriber is looking at keys in the $request->attributes or in $request->query. When executing a custom action the data it needs seem to be missing in the right location?
The issue is only happening when I enable pretty URLs. Without them, it works fine.
Here is what I do for my customActions :
#[AdminAction(routePath: '{entityId}/custom', routeName: 'custom')]
public function myAction(AdminContext $context): Response
{
if (!$context->getEntity()->isAccessible()) {
throw new InsufficientEntityPermissionException($context);
}
$entity = $context->getEntity()->getInstance();
Yeah I do that too, but when I enable pretty URLs $context->getEntity() has no entity.
message:Uncaught PHP Exception TypeError: "EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext::getEntity(): Return value must be of type EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto, null returned" at AdminContext.php line 97
Same for me . Custom actions are broken for me alors with pretty URLs (@javiereguiluz it may interests you)
Hey, adding AdminAction attribute worked in my case. Thanks @octoseth
My current working code
...
#[\Override]
public function configureActions(Actions $actions): Actions
{
$customAction = Action::new('custom', label: false)
->linkToCrudAction('customAction');
$actions->add(Crud::PAGE_INDEX, $customAction);
return $actions;
}
...
#[AdminAction(routePath: '{entityId}/custom-action', routeName: 'custom_action')]
public function customAction(AdminContext $context): Response
{
$crud = $context->getEntity();
...
}
With this attribute it uses pretty URL
https://example.com/{entityName}/{entityId}/custom-action
and without this attribute it is using the old URL type with crudAction and crudControllerFqcn GET parameters.
thanks, adding AdminAction attribute works for me too. Also for batch actions.
Had same issue. thanks to @michalschroeder
In EasyAdmin versions prior to 4.25.0, instead of
#[AdminRoute]you had to use the#[AdminAction]attribute, which is now deprecated and will be removed in EasyAdmin 5.0.0.
Any alternative to #[AdminAction]?