Skip to content

Commit

Permalink
38.1. A Global "Export" Action
Browse files Browse the repository at this point in the history
  There are actually three different types of actions in EasyAdmin. The first consists of the normal actions, like Add, Edit, Delete, and Detail. These operate on a single entity. The second type is batch actions, which operate on a selection of entities. For example, we can click two of these check boxes and use the Delete button up here. That is the batch Delete, and it's the only built-in batch action.

  Side note: to make sure approved questions aren't deleted - which is work we just finished, you should also remove the batch Delete action for the Question crud. Otherwise, people might try to batch Delete questions. That won't work... thanks to some code we wrote, but they'll get a very-unfriendly 500 error.

  Anyways, the third type of action is called a "global action", which operates on all the entities in a section. There are no built-in global actions, but we're going to add one: a button to "export" the entire questions list to a CSV file.

  Creating the Global Action
  - For the most part, creating a global action... isn't much different than creating a normal custom action. It starts the same. Over in the actions config, create a new $exportAction = Action::new() and call it export. Below, we'll ->linkToCrudAction() and also call it export. Then, add some CSS classes... and an icon. Cool. We're ready to add this to the index page: ->add(Crud::PAGE_INDEX, $exportAction) to get that button on the main list page.

  If we stopped now, this would be a normal action. When we refresh... yup! It shows up next to each item in the list. Not what we wanted.
  • Loading branch information
petrero committed May 25, 2022
1 parent df361fb commit 3ee8e0f
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Controller/Admin/QuestionCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public function configureActions(Actions $actions): Actions {
return !$question->getIsApproved();
});

$exportAction = Action::new('export')
->linkToCrudAction('export')
->addCssClass('btn btn-success')
->setIcon('fa fa-download');

return parent::configureActions($actions)
->update(Crud::PAGE_INDEX, Action::DELETE, function(Action $action){
$action->displayIf(static function(Question $question){
Expand All @@ -122,7 +127,8 @@ public function configureActions(Actions $actions): Actions {
->setPermission(Action::BATCH_DELETE, 'ROLE_SUPER_ADMIN')
->add(Crud::PAGE_DETAIL, $viewAction()->addCssClass('btn btn-success'))
->add(Crud::PAGE_INDEX, $viewAction())
->add(Crud::PAGE_DETAIL, $approveAction);
->add(Crud::PAGE_DETAIL, $approveAction)
->add(Crud::PAGE_INDEX, $exportAction);
}

public function configureFilters(Filters $filters): Filters {
Expand Down

0 comments on commit 3ee8e0f

Please sign in to comment.