Skip to content

Commit bad57de

Browse files
committed
Add docs
1 parent 6583cdc commit bad57de

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

doc/actions.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ strings with the action names (``'index'``, ``'detail'``, ``'edit'``, etc.) you
3131
can also use constants for these values: ``Action::INDEX``, ``Action::DETAIL``,
3232
``Action::EDIT``, etc. (they are defined in the ``EasyCorp\Bundle\EasyAdminBundle\Config\Action`` class).
3333

34+
.. _actions-built-in:
35+
3436
Built-in Actions
3537
----------------
3638

@@ -996,6 +998,63 @@ This is no longer needed in modern EasyAdmin versions and is now a discouraged
996998
practice that you should avoid in your applications. Instead, see the previous
997999
section about :ref:`how to integrate custom Symfony controllers into EasyAdmin dashboards <actions-integrating-symfony>`.
9981000

1001+
Actions Extensions
1002+
------------------
1003+
1004+
Applications using EasyAdmin define their actions in the ``configureActions()``
1005+
method of the :doc:`CRUD controllers </crud>`. You can enable, disable, or modify
1006+
:ref:`built-in actions <actions-built-in>`, and also create your own
1007+
:ref:`custom actions <actions-custom>`.
1008+
1009+
EasyAdmin provides an additional feature to add, remove, or change actions
1010+
(built-in or custom) dynamically at runtime: **action extensions**. They allow
1011+
your application (or third-party bundles installed in it) to modify the actions
1012+
defined for your controllers.
1013+
1014+
Action extensions are PHP classes that receive the full configuration of
1015+
actions in your backend so they can add, remove, or update any of them.
1016+
1017+
For example, imagine you need a **Duplicate** action across most of your
1018+
backends. Instead of defining it repeatedly, you can create a reusable package
1019+
(such as a `Symfony bundle`_) and add the following class::
1020+
1021+
// <your-package>/src/DuplicateActionExtension.php
1022+
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
1023+
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
1024+
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
1025+
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
1026+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Action\ActionsExtensionInterface;
1027+
1028+
final class DuplicateActionExtension implements ActionsExtension
1029+
{
1030+
// return true in this method to enable the extension for
1031+
// the current backend request
1032+
public function supports(AdminContext $context): bool
1033+
{
1034+
// enable the extension only on some pages
1035+
return $context->getCrud()->getCurrentPage() === Crud::PAGE_DETAIL;
1036+
1037+
// enable it on all except some entities
1038+
$entityFqcn = $context->getCrud()->getEntityFqcn();
1039+
return null !== $entityFqcn && !\in_array(entityFqcn, ['...'], true);
1040+
1041+
// or use any other admin context data to make the decision
1042+
}
1043+
1044+
public function extend(Actions $actions, AdminContext $context): void
1045+
{
1046+
$duplicate = Action::new('duplicate', 'Duplicate', 'fa fa-clone')
1047+
->linkToCrudAction('duplicate')
1048+
->asSuccessAction();
1049+
1050+
$actions->add(Crud::PAGE_DETAIL, $duplicate);
1051+
1052+
// you can add single actions, groups of actions, etc.
1053+
// you can also remove or update existing actions
1054+
}
1055+
}
1056+
9991057
.. _`FontAwesome`: https://fontawesome.com/
10001058
.. _`Symfony base controller class`: https://symfony.com/doc/current/controller.html#the-base-controller-class-services
10011059
.. _`Symfony controllers`: https://symfony.com/doc/current/controller.html
1060+
.. _`Symfony bundle`: https://symfony.com/doc/current/bundles.html

0 commit comments

Comments
 (0)