Actions are each of the tasks that you can perform on CRUD pages. In the
index page for example, you have tasks to "edit" and "delete" each entity
displayed in the listing and you have another task to "create" a new entity.
Actions are configured in the configureActions() method of your
:doc:`dashboard </dashboards>` or :doc:`CRUD controller </crud>`:
namespace App\Controller\Admin;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class ProductCrudController extends AbstractCrudController
{
// ...
public function configureActions(Actions $actions): Actions
{
// ...
}
}
These are the actions included by default in each page:
| Page | Default Actions |
|---|---|
detail |
delete, new, index |
edit |
index, delete |
index |
delete, detail, new |
new |
index |
Some methods require as argument the name of some action. You can use a string
with the action name ('index', 'detail', 'edit', etc.). If you prefer
to use constants for these values, use Action::INDEX, Action::DETAIL,
Action::EDIT, etc. (they are defined in the EasyCorp\Bundle\EasyAdminBundle\Config\Action class).
Use the add() method to add built-in actions (those defined as Action::*
constants) and your own custom actions (explained later in this article):
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
public function configureActions(Actions $actions): Actions
{
return $actions
// ...
->add(Crud::PAGE_INDEX, Action::DETAIL)
->add(Crud::PAGE_EDIT, Action::SAVE_AND_ADD_ANOTHER)
;
}
Removing actions makes them unavailable in the interface, so the user can't
click on buttons/links to run those actions. However, users can hack the URL
to run the action. To fully disable an action, use the disableActions()
method explained later:
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
public function configureActions(Actions $actions): Actions
{
return $actions
// ...
->remove(Crud::PAGE_INDEX, Action::NEW)
->remove(Crud::PAGE_DETAIL, Action::EDIT)
;
}
This is mostly useful to change built-in actions (e.g. to change their icon,
update or remove their label, etc.). The update() method expects a callable
and EasyAdmin passes the action to it automatically:
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
public function configureActions(Actions $actions): Actions
{
return $actions
// ...
->update(Crud::PAGE_DETAIL, Action::NEW, function (Action $action) {
return $action->setIcon('fa fa-file-alt')->setLabel(false);
})
// in PHP 7.4 and newer you can use arrow functions
// ->update(Crud::PAGE_DETAIL, Action::NEW,
// fn (Action $action) => $action->setIcon('fa fa-file-alt')->setLabel(false))
;
}
Some actions must displayed only when some conditions met. For example, a
"View Invoice" action may be displayed only when the order status is "paid".
Use the displayIf() method to configure when the action should be visible
to users:
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
public function configureActions(Actions $actions): Actions
{
$viewInvoice = Action::new('View Invoice', 'fas fa-file-invoice')
->displayIf(static function ($entity) {
return $entity->isPublished();
});
// in PHP 7.4 and newer you can use arrow functions
// ->displayIf(fn ($entity) => $entity->isPublished())
return $actions
// ...
->add(Crud::PAGE_INDEX, $viewInvoice);
}
Disabling an action means that it's not displayed in the interface and the user can't run the action even if they hack the URL. If they try to do that, they will see a "Forbidden Action" exception.
Actions are disabled globally, you cannot disable them per page:
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
public function configureActions(Actions $actions): Actions
{
return $actions
// ...
// this will forbid to create or delete entities in the backend
->disableActions(Action::NEW, Action::DELETE)
;
}
Instead of disabling actions, you can restrict their execution to certain users.
Use the setPermission() to define the Symfony Security permission needed to
view and run some action.
Permissions are defined globally; you cannot define different permissions per page:
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
public function configureActions(Actions $actions): Actions
{
return $actions
// ...
->setPermission(Action::NEW, 'ROLE_ADMIN')
->setPermission(Action::NEW, 'ROLE_SUPER_ADMIN')
;
}
Use the setActionOrder() to define the order in which actions are displayed
in some page:
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
public function configureActions(Actions $actions): Actions
{
return $actions
// ...
->setActionOrder(Crud::PAGE_INDEX, [Action::DELETE, Action::DETAIL, Action::EDIT])
;
}
If you display lots of fields on each row of the index page, there won't be
enough room for the item actions. In those cases, you can display the actions in
a dropdown menu instead of the expanded design used by default.
To do so, use the showEntityActionsAsDropdown() method:
namespace App\Controller\Admin;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class ProductCrudController extends AbstractCrudController
{
// ...
public function configureCrud(Crud $crud): Crud
{
return $crud
// ...
->showEntityActionsAsDropdown()
;
}
}
In addition to the built-in actions provided by EasyAdmin, you can create your
own actions. An action always results in the execution of some method of some of
your controllers. If the method is defined in the CRUD controller, use
linkToCrudAction(); if the method is defined somewhere else, define a route
for it and use linkToRoute():
namespace App\Controller\Admin;
use App\Entity\Invoice;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class ProductCrudController extends AbstractCrudController
{
// ...
public function configureActions(Actions $actions): Actions
{
// this action executes the 'invoice()' method of the current CRUD controller
$viewInvoice = Action::new('View invoice', 'fa fa-file-invoice')
->linkToCrudAction('renderInvoice');
// if the method is not defined in a CRUD controller, link to its route
$sendInvoice = Action::new('Send invoice', 'fa fa-envelope')
// if the route needs parameters, you can define them:
// 1) using an array
->linkToRoute('invoice_send', [
'send_at' => (new \DateTime('+ 10 minutes'))->format('YmdHis'),
])
// 2) using a callable (useful if parameters depend on the entity instance)
// (the type-hint of the function argument is optional but useful)
->linkToRoute('invoice_send', function (Invoice $entity) {
return [
'uuid' => $entity->getId(),
'method' => $entity->sendMethod(),
];
});
return $actions
// ...
->add('viewInvoice', $viewInvoice)
->add('sendInvoice', $sendInvoice)
;
}
}
To link to an external URL, use linkToUrl():
namespace App\Controller\Admin;
use App\Entity\Invoice;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class ProductCrudController extends AbstractCrudController
{
// ...
public function configureActions(Actions $actions): Actions
{
// this action go to the invoice on Stripe
$viewStripeInvoice = Action::new('View invoice', 'fa fa-file-invoice')
->linkToUrl(function (Invoice $entity) {
return 'https://www.stripe.com/invoice/'.$entity->getStripeReference();
});
return $actions
// ...
->add('viewInvoice', $viewStripeInvoice)
;
}
}
Note
Batch actions are not ready yet, but we're working on adding support for them.