Actions ======= **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 ` or :doc:`CRUD controller `:: namespace App\Controller\Admin; use EasyCorp\Bundle\EasyAdminBundle\Config\Actions; use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; class ProductCrudController extends AbstractCrudController { // ... public function configureActions(Actions $actions): Actions { // ... } } Action Names and Constants -------------------------- Some methods require as argument the name of some action. In addition to plain strings with the action names (``'index'``, ``'detail'``, ``'edit'``, etc.) you can also use constants for these values: ``Action::INDEX``, ``Action::DETAIL``, ``Action::EDIT``, etc. (they are defined in the ``EasyCorp\Bundle\EasyAdminBundle\Config\Action`` class). Built-in Actions ---------------- These are the built-in actions included by default in each page: * Page ``Crud::PAGE_INDEX`` (``'index'``): * Added by default globally: ``Action::NEW`` * Added by default per entry: ``Action::EDIT``, ``Action::DELETE`` * Other available actions per entry: ``Action::DETAIL`` * Page ``Crud::PAGE_DETAIL`` (``'detail'``): * Added by default: ``Action::EDIT``, ``Action::DELETE``, ``Action::INDEX`` * Other available actions: - * Page ``Crud::PAGE_EDIT`` (``'edit'``): * Added by default: ``Action::SAVE_AND_RETURN``, ``Action::SAVE_AND_CONTINUE`` * Other available actions: ``Action::DELETE``, ``Action::DETAIL``, ``Action::INDEX`` * Page ``Crud::PAGE_NEW`` (``'new'``): * Added by default: ``Action::SAVE_AND_RETURN``, ``Action::SAVE_AND_ADD_ANOTHER`` * Other available actions: ``Action::SAVE_AND_CONTINUE``, ``Action::INDEX`` Adding Actions -------------- Use the ``add()`` method to add any built-in actions and your own custom actions (which are explained later in this article):: 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 // ... ->add(Crud::PAGE_INDEX, Action::DETAIL) ->add(Crud::PAGE_EDIT, Action::SAVE_AND_ADD_ANOTHER) ; } Removing Actions ---------------- 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 ``disable()`` method explained later:: 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 // ... ->remove(Crud::PAGE_INDEX, Action::NEW) ->remove(Crud::PAGE_DETAIL, Action::EDIT) ; } Updating an Action ------------------ 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_INDEX, 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_INDEX, Action::NEW, // fn (Action $action) => $action->setIcon('fa fa-file-alt')->setLabel(false)) ; } Displaying Actions Conditionally -------------------------------- 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->isPaid(); }); // in PHP 7.4 and newer you can use arrow functions // ->displayIf(fn ($entity) => $entity->isPaid()) return $actions // ... ->add(Crud::PAGE_INDEX, $viewInvoice); } Disabling Actions ----------------- 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 ->disable(Action::NEW, Action::DELETE) ; } Restricting Actions ------------------- 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::DELETE, 'ROLE_SUPER_ADMIN') ; } Reordering Actions ------------------ Use the ``reorder()`` 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 // ... // you can reorder built-in actions... ->reorder(Crud::PAGE_INDEX, [Action::DETAIL, Action::DELETE, Action::EDIT]) // ...and your own custom actions too ->reorder(Crud::PAGE_INDEX, [Action::DETAIL, 'viewInvoice', Action::DELETE, Action::EDIT]) // you can pass only a few actions to this method and the rest of actions // will be appended in their original order. In the following example, the // DELETE and EDIT actions are missing but they will be added automatically // after DETAIL and 'viewInvoice' actions ->reorder(Crud::PAGE_INDEX, [Action::DETAIL, 'viewInvoice']) ; } Dropdown and Inline Entity Actions ---------------------------------- In the ``index`` page, the entity actions (``edit``, ``delete``, etc.) are displayed by default in a dropdown. This is done to better display the field contents on each row. If you prefer to display all the actions *inlined* (that is, without a dropdown) use the ``showEntityActionsInlined()`` 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 // ... ->showEntityActionsInlined() ; } } .. _actions-custom: Adding Custom Actions --------------------- In addition to the built-in actions provided by EasyAdmin, you can create your own actions. First, define the basics of your action (name, label, icon) with the ``Action`` class constructor:: // the only mandatory argument is the internal name of the action (which is // used to add the action to some pages, to reorder the action position, etc.) $viewInvoice = Action::new('viewInvoice'); // the second optional argument is the label visible to end users $viewInvoice = Action::new('viewInvoice', 'Invoice'); // not defining the label explicitly or setting it to NULL means // that the label is autogenerated from the name (e.g. 'viewInvoice' -> 'View Invoice') $viewInvoice = Action::new('viewInvoice', null); // set the label to FALSE to not display any label for this action (but make sure // to display an icon for the action; otherwise users won't see it) $viewInvoice = Action::new('viewInvoice', false); // the third optional argument is the full CSS class of a FontAwesome icon // see https://fontawesome.com/v6/search?m=free $viewInvoice = Action::new('viewInvoice', 'Invoice', 'fa fa-file-invoice'); Then you can configure the basic HTML/CSS attributes of the button/element that will represent the action:: $viewInvoice = Action::new('viewInvoice', 'Invoice', 'fa fa-file-invoice') // renders the action as a HTML element ->displayAsLink() // renders the action as a