EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

AdminContext missing entity in custom action

Open finnef opened this issue 1 year ago • 7 comments

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.

finnef avatar Feb 03 '25 14:02 finnef

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();

octoseth avatar Feb 03 '25 21:02 octoseth

Yeah I do that too, but when I enable pretty URLs $context->getEntity() has no entity.

finnef avatar Feb 04 '25 07:02 finnef

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)

Huluti avatar Feb 12 '25 12:02 Huluti

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.

michalschroeder avatar Feb 13 '25 21:02 michalschroeder

thanks, adding AdminAction attribute works for me too. Also for batch actions.

finnef avatar Feb 17 '25 09:02 finnef

Had same issue. thanks to @michalschroeder

andrii-boiko avatar Jul 30 '25 07:07 andrii-boiko

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]?

ucscode avatar Dec 09 '25 07:12 ucscode