From 936627fa6ccd185795809342c53ad1360f2284ff Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 7 Nov 2024 18:00:26 +0100 Subject: [PATCH] Fix filters when using pretty URLs --- src/Factory/FormFactory.php | 6 +----- src/Field/Configurator/BooleanConfigurator.php | 8 ++++++++ src/Router/AdminRouteGenerator.php | 7 ++++++- .../PrettyUrls/PrettyUrlsControllerTest.php | 12 ++++++++---- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Factory/FormFactory.php b/src/Factory/FormFactory.php index cc02c084a1..b6193a3928 100644 --- a/src/Factory/FormFactory.php +++ b/src/Factory/FormFactory.php @@ -5,7 +5,6 @@ use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection; use EasyCorp\Bundle\EasyAdminBundle\Config\Action; use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore; -use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA; use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext; use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudFormType; @@ -66,10 +65,7 @@ public function createFiltersForm(FilterCollection $filters, Request $request): { // filtering always returns to the index page of the same CRUD entity and with the same query parameters $urlQueryParameters = $request->query->all(); - if ($urlQueryParameters[EA::CRUD_ACTION] ?? null) { - $urlQueryParameters[EA::CRUD_ACTION] = Action::INDEX; - } - $actionUrl = $this->adminUrlGenerator->setAll($urlQueryParameters)->generateUrl(); + $actionUrl = $this->adminUrlGenerator->setAll($urlQueryParameters)->setAction(Action::INDEX)->generateUrl(); $filtersForm = $this->symfonyFormFactory->createNamed('filters', FiltersFormType::class, null, [ 'method' => 'GET', diff --git a/src/Field/Configurator/BooleanConfigurator.php b/src/Field/Configurator/BooleanConfigurator.php index 543aa5900d..b8111f148e 100644 --- a/src/Field/Configurator/BooleanConfigurator.php +++ b/src/Field/Configurator/BooleanConfigurator.php @@ -35,6 +35,14 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c $isRenderedAsSwitch = true === $field->getCustomOption(BooleanField::OPTION_RENDER_AS_SWITCH); if ($isRenderedAsSwitch) { + // this happens when processing fields not for a specific entity (e.g. when + // rendering the filters from the 'index' page) + if (null === $entityDto->getPrimaryKeyValue()) { + $field->setCustomOption(BooleanField::OPTION_RENDER_AS_SWITCH, false); + + return; + } + $crudDto = $context->getCrud(); if (null !== $crudDto && Action::NEW !== $crudDto->getCurrentAction()) { diff --git a/src/Router/AdminRouteGenerator.php b/src/Router/AdminRouteGenerator.php index 49623cbfaa..a749fe96d6 100644 --- a/src/Router/AdminRouteGenerator.php +++ b/src/Router/AdminRouteGenerator.php @@ -39,7 +39,7 @@ final class AdminRouteGenerator implements AdminRouteGeneratorInterface ], 'batchDelete' => [ 'routePath' => '/batchDelete', - 'routeName' => 'batchDelete', + 'routeName' => 'batch_delete', 'methods' => ['POST'], ], 'autocomplete' => [ @@ -47,6 +47,11 @@ final class AdminRouteGenerator implements AdminRouteGeneratorInterface 'routeName' => 'autocomplete', 'methods' => ['GET'], ], + 'renderFilters' => [ + 'routePath' => '/renderFilters', + 'routeName' => 'render_filters', + 'methods' => ['GET'], + ], 'edit' => [ 'routePath' => '/{entityId}/edit', 'routeName' => 'edit', diff --git a/tests/Controller/PrettyUrls/PrettyUrlsControllerTest.php b/tests/Controller/PrettyUrls/PrettyUrlsControllerTest.php index c5ca44d9f6..718c7d6827 100644 --- a/tests/Controller/PrettyUrls/PrettyUrlsControllerTest.php +++ b/tests/Controller/PrettyUrls/PrettyUrlsControllerTest.php @@ -31,22 +31,25 @@ public function testGeneratedRoutes() $expectedRoutes['second_dashboard'] = '/second/dashboard'; $expectedRoutes['admin_pretty_blog_post_index'] = '/admin/pretty/urls/blog_post/'; $expectedRoutes['admin_pretty_blog_post_new'] = '/admin/pretty/urls/blog_post/new'; - $expectedRoutes['admin_pretty_blog_post_batchDelete'] = '/admin/pretty/urls/blog_post/batchDelete'; + $expectedRoutes['admin_pretty_blog_post_batch_delete'] = '/admin/pretty/urls/blog_post/batchDelete'; $expectedRoutes['admin_pretty_blog_post_autocomplete'] = '/admin/pretty/urls/blog_post/autocomplete'; + $expectedRoutes['admin_pretty_blog_post_render_filters'] = '/admin/pretty/urls/blog_post/renderFilters'; $expectedRoutes['admin_pretty_blog_post_edit'] = '/admin/pretty/urls/blog_post/{entityId}/edit'; $expectedRoutes['admin_pretty_blog_post_delete'] = '/admin/pretty/urls/blog_post/{entityId}/delete'; $expectedRoutes['admin_pretty_blog_post_detail'] = '/admin/pretty/urls/blog_post/{entityId}'; $expectedRoutes['admin_pretty_category_index'] = '/admin/pretty/urls/category/'; $expectedRoutes['admin_pretty_category_new'] = '/admin/pretty/urls/category/new'; - $expectedRoutes['admin_pretty_category_batchDelete'] = '/admin/pretty/urls/category/batchDelete'; + $expectedRoutes['admin_pretty_category_batch_delete'] = '/admin/pretty/urls/category/batchDelete'; $expectedRoutes['admin_pretty_category_autocomplete'] = '/admin/pretty/urls/category/autocomplete'; + $expectedRoutes['admin_pretty_category_render_filters'] = '/admin/pretty/urls/category/renderFilters'; $expectedRoutes['admin_pretty_category_edit'] = '/admin/pretty/urls/category/{entityId}/edit'; $expectedRoutes['admin_pretty_category_delete'] = '/admin/pretty/urls/category/{entityId}/delete'; $expectedRoutes['admin_pretty_category_detail'] = '/admin/pretty/urls/category/{entityId}'; $expectedRoutes['admin_pretty_external_user_editor_custom_route_for_index'] = '/admin/pretty/urls/user-editor/custom/path-for-index'; $expectedRoutes['admin_pretty_external_user_editor_custom_route_for_new'] = '/admin/pretty/urls/user-editor/new'; - $expectedRoutes['admin_pretty_external_user_editor_batchDelete'] = '/admin/pretty/urls/user-editor/batchDelete'; + $expectedRoutes['admin_pretty_external_user_editor_batch_delete'] = '/admin/pretty/urls/user-editor/batchDelete'; $expectedRoutes['admin_pretty_external_user_editor_autocomplete'] = '/admin/pretty/urls/user-editor/autocomplete'; + $expectedRoutes['admin_pretty_external_user_editor_render_filters'] = '/admin/pretty/urls/user-editor/renderFilters'; $expectedRoutes['admin_pretty_external_user_editor_edit'] = '/admin/pretty/urls/user-editor/{entityId}/edit'; $expectedRoutes['admin_pretty_external_user_editor_delete'] = '/admin/pretty/urls/user-editor/{entityId}/delete'; $expectedRoutes['admin_pretty_external_user_editor_detail'] = '/admin/pretty/urls/user-editor/custom/path-for-detail/{entityId}'; @@ -54,8 +57,9 @@ public function testGeneratedRoutes() $expectedRoutes['admin_pretty_external_user_editor_foofoo'] = '/admin/pretty/urls/user-editor/bar/bar'; $expectedRoutes['second_dashboard_external_user_editor_custom_route_for_index'] = '/second/dashboard/user-editor/custom/path-for-index'; $expectedRoutes['second_dashboard_external_user_editor_custom_route_for_new'] = '/second/dashboard/user-editor/add-new'; - $expectedRoutes['second_dashboard_external_user_editor_batchDelete'] = '/second/dashboard/user-editor/batchDelete'; + $expectedRoutes['second_dashboard_external_user_editor_batch_delete'] = '/second/dashboard/user-editor/batchDelete'; $expectedRoutes['second_dashboard_external_user_editor_autocomplete'] = '/second/dashboard/user-editor/autocomplete'; + $expectedRoutes['second_dashboard_external_user_editor_render_filters'] = '/second/dashboard/user-editor/renderFilters'; $expectedRoutes['second_dashboard_external_user_editor_change'] = '/second/dashboard/user-editor/edit/---{entityId}---'; $expectedRoutes['second_dashboard_external_user_editor_delete_this_now'] = '/second/dashboard/user-editor/{entityId}/delete'; $expectedRoutes['second_dashboard_external_user_editor_detail'] = '/second/dashboard/user-editor/custom/path-for-detail/{entityId}';