EasyAdminBundle
EasyAdminBundle copied to clipboard
AdminContext not available in configureActions
Describe the bug
From the docs:
public function configureActions(Actions $actions): Actions
{
$viewInvoice = Action::new('View Invoice', 'fas fa-file-invoice')
->displayIf(static function ($entity) {
return $entity->isPaid();
});
return $actions
// ...
->add(Crud::PAGE_INDEX, $viewInvoice);
}
Now, where do I get $entity
from?
$this->getContext()->getEntity()->getInstance()
results in an NPE for the AdminContext
itself.
To Reproduce
Place the following code in any CrudController
:
public function configureActions(Actions $actions): Actions
{
// weird hack as we cannot access the AdminContext from here
$employeeId = $this->requestStack->getCurrentRequest()->get('entityId');
$employee = $this->getContext()->getEntity()->getInstance();
}
Visit the Controller's URL.
You don't have to get entity. You just need to provide callable for an action, which takes one argument (for example $entity from the docs) and EasyAdmin will call this defined callable and pass the entity as argument at runtime.
https://github.com/EasyCorp/EasyAdminBundle/blob/4a080891dbc54480853653936f91104234096572/src/Dto/ActionDto.php#L242-L245
@Macko82 I'm sorry, but I don't understand the answer. Can you explain this in more detail, please?
I can find an "entity-callable" in "->displayIf()" as @Chrisissorry described, but how can I get the entity-ID if I want to include it in a modal dialog (data-bs-target), for example?
public function configureActions(Actions $actions): Actions
{
//...
$myAction = Action::new('myAction', null, 'fa-solid fa-my-action')
->linkToCrudAction('myAction')
->setHtmlAttributes([
'title' => $this->translator->trans('tooltip.my.action'),
'data-bs-toggle' => 'modal',
'data-bs-target' => '#modal-myAction-' . $entity->getId(),
])
->setTemplatePath('admin/my_action.html.twig')
;
$actions->add(Crud::PAGE_INDEX, $myAction);
//...
}