EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext::getEntity(): Return value must be of type EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto, null returned
Describe the bug In BeforeCrudActionEvent on new Entity
$context->getEntity() is null
To Reproduce
public static function getSubscribedEvents(): array
{
return [
BeforeCrudActionEvent::class => ['checkUser'],
];
}
public function checkUser(BeforeCrudActionEvent $event): void
{
$context = $event->getAdminContext();
if ($context) {
$entity = $context->getEntity()->getInstance();
...
(OPTIONAL) Additional context IN final class AdminContext implements AdminContextInterface private ?EntityDto $entityDto; <-nullable
public function __construct(Request $request, ?UserInterface $user, I18nDto $i18nDto, CrudControllerRegistry $crudControllers, DashboardDto $dashboardDto, DashboardControllerInterface $dashboardController, AssetsDto $assetDto, ?CrudDto $crudDto, ?EntityDto $entityDto, ?SearchDto $searchDto, MenuFactoryInterface $menuFactory, TemplateRegistry $templateRegistry, bool $usePrettyUrls = false) <-entityDTO nullable
public function getEntity(): EntityDto <- NOT NULLABLE
Is it normal ?
Is this a custom CRUD action? Because I had the same error trying to invoke one of my custom actions, under the new pretty URLs routing.
For me, the solution was to add a #[AdminAction('{entityId}/action', 'admin_entity_action')] attribute to the method that handles the CRUD action.
(FTR, a concrete example for a refresh action on a News entity would be #[AdminAction('{entityId}/refresh', 'admin_news_refresh')].)
Yes, is a custom action if i use adminAction , i no more use Admin context, and I load entity in action wihout it.
#[AdminAction('{entityId}/reSendEmail', 'admin_resend_action')]
public function reSendEmail( AdminContext $context,
#[MapEntity(mapping: ['entityId' => 'id'])] Process $process
): \Symfony\Component\HttpFoundation\RedirectResponse {
// $process = $context->getEntity()->getInstance();
Is this a custom CRUD action? Because I had the same error trying to invoke one of my custom actions, under the new pretty URLs routing.
I have the same issue.
To reproducer:
- enable beautiful URLs
- Declare a custome action and it's controller
- Click on the link
- 💥
same issue and @fwg workaround worked
I did @fwg workaround and still not working, following this for export with filters used to work https://symfonycasts.com/screencast/easyadminbundle/global-action but not it gives
Call to a member function getFiltersConfig() on null
Any other tip to look to fix it ?
After messing around a little more the changes I made is putting the AdminAction and change the link to this:
->linkToCrudAction('export')
Now is working.
I did @fwg workaround and still not working, following this for export with filters used to work https://symfonycasts.com/screencast/easyadminbundle/global-action but not it gives
Call to a member function getFiltersConfig() on nullAny other tip to look to fix it ?
After messing around a little more the changes I made is putting the AdminAction and change the link to this:
->linkToCrudAction('export')Now is working. Call to a member function getFiltersConfig() on null It appears to be the same issue. Could you please explain the solution in more detail? Thank you!
Same but different here. I just:
- overridden the
newaction of a crud controller - added a "new foo" button on another page using this:
$newFooAction = Action::new('newFoo', 'New foo') ->linkToRoute('admin_foo_new', function (Organization $org) { return ['id' => $org->getId()]; }); - ❌ The linkToRoute uses to an old-formatted route
- ❌ The entity is empty in the context of my "new" route
Same here.
1/ https://localhost/easy-admin/foo-bar works
2/ https://localhost/easy-admin/foo-bar works but https://localhost/easy-admin?crudAction=index&crudControllerFqcn=App%5CController%5CAdmin%5CFooBarCrudController won't work
But MenuItem::linkToCrud generate those links...
3/ For some reasons
MenuItem::linkToRoute('FooBar', 'fas fa-list', 'easy_admin_foo_bar_index');
generates https://localhost/easy-admin?routeName=easy_admin_foo_bar_index that has the same problem as OP.
while $this->redirectToRoute('easy_admin_foo_bar_index'); works.
Issue still persists, I suggest moving to version 4.20.2
Is this a custom CRUD action? Because I had the same error trying to invoke one of my custom actions, under the new pretty URLs routing.
For me, the solution was to add a
#[AdminAction('{entityId}/action', 'admin_entity_action')]attribute to the method that handles the CRUD action.(FTR, a concrete example for a
refreshaction on aNewsentity would be#[AdminAction('{entityId}/refresh', 'admin_news_refresh')].)
In last versions we must now use AdminRoute instead of AdminAction.
For eg.
use EasyCorp\Bundle\EasyAdminBundle\Attribute\AdminRoute;
#[AdminRoute(path: '/moveUp', name: 'move_up')]
public function moveUp(AdminContext $context): Response
{
return $this->move($context, Direction::Up);
}
Same issue without any action. Just switched to pretty URLs and my browser still remember old URLs and I stumbled upon the same error.
While looking at it, isn't there a fundamental bug between constructor arg and getter?
https://github.com/EasyCorp/EasyAdminBundle/blob/f718248b96fbff1d1dd4d38acaaa2c25af52e609/src/Context/AdminContext.php#L45
https://github.com/EasyCorp/EasyAdminBundle/blob/f718248b96fbff1d1dd4d38acaaa2c25af52e609/src/Context/AdminContext.php#L82-L85
private readonly ?EntityDto $entityDto takes a nullable type whereas the getter expect an EntityDto?