From 1edb927a5185a599a2e82747ed2ae28ac767e0cd Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 10 Apr 2024 16:26:04 +0200 Subject: [PATCH 1/6] Introduce SearchHandlerInterface (#8170) --- src/Block/AdminSearchBlockService.php | 4 ++-- src/Resources/config/core.php | 3 +++ src/Search/SearchHandler.php | 10 +-------- src/Search/SearchHandlerInterface.php | 30 +++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 src/Search/SearchHandlerInterface.php diff --git a/src/Block/AdminSearchBlockService.php b/src/Block/AdminSearchBlockService.php index 372a55b96d..674715ef7e 100644 --- a/src/Block/AdminSearchBlockService.php +++ b/src/Block/AdminSearchBlockService.php @@ -16,7 +16,7 @@ use Sonata\AdminBundle\Admin\Pool; use Sonata\AdminBundle\Filter\FilterInterface; use Sonata\AdminBundle\Search\SearchableFilterInterface; -use Sonata\AdminBundle\Search\SearchHandler; +use Sonata\AdminBundle\Search\SearchHandlerInterface; use Sonata\AdminBundle\Templating\TemplateRegistryInterface; use Sonata\BlockBundle\Block\BlockContextInterface; use Sonata\BlockBundle\Block\Service\AbstractBlockService; @@ -37,7 +37,7 @@ final class AdminSearchBlockService extends AbstractBlockService public function __construct( Environment $twig, private Pool $pool, - private SearchHandler $searchHandler, + private SearchHandlerInterface $searchHandler, private TemplateRegistryInterface $templateRegistry, private string $emptyBoxesOption, private string $adminRoute diff --git a/src/Resources/config/core.php b/src/Resources/config/core.php index f57a51cf67..3ff2683220 100644 --- a/src/Resources/config/core.php +++ b/src/Resources/config/core.php @@ -33,6 +33,7 @@ use Sonata\AdminBundle\Request\AdminFetcherInterface; use Sonata\AdminBundle\Route\AdminPoolLoader; use Sonata\AdminBundle\Search\SearchHandler; +use Sonata\AdminBundle\Search\SearchHandlerInterface; use Sonata\AdminBundle\SonataConfiguration; use Sonata\AdminBundle\Templating\TemplateRegistry; use Sonata\AdminBundle\Translator\BCLabelTranslatorStrategy; @@ -124,6 +125,8 @@ ->set('sonata.admin.search.handler', SearchHandler::class) + ->alias(SearchHandlerInterface::class, 'sonata.admin.search.handler') + ->set('sonata.admin.controller.crud', CRUDController::class) ->public() ->tag('container.service_subscriber') diff --git a/src/Search/SearchHandler.php b/src/Search/SearchHandler.php index 36e4af2008..0432542977 100644 --- a/src/Search/SearchHandler.php +++ b/src/Search/SearchHandler.php @@ -15,26 +15,18 @@ use Sonata\AdminBundle\Admin\AdminInterface; use Sonata\AdminBundle\Datagrid\PagerInterface; -use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; use Sonata\AdminBundle\Filter\FilterInterface; /** * @author Thomas Rabaix */ -final class SearchHandler +final class SearchHandler implements SearchHandlerInterface { /** * @var array */ private array $adminsSearchConfig = []; - /** - * @throws \RuntimeException - * - * @phpstan-template T of object - * @phpstan-param AdminInterface $admin - * @phpstan-return PagerInterface>|null - */ public function search(AdminInterface $admin, string $term, int $page = 0, int $offset = 20): ?PagerInterface { // If the search is disabled for the whole admin, skip any further processing. diff --git a/src/Search/SearchHandlerInterface.php b/src/Search/SearchHandlerInterface.php new file mode 100644 index 0000000000..c4dbfcee2d --- /dev/null +++ b/src/Search/SearchHandlerInterface.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Search; + +use Sonata\AdminBundle\Admin\AdminInterface; +use Sonata\AdminBundle\Datagrid\PagerInterface; +use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; + +interface SearchHandlerInterface +{ + /** + * @throws \RuntimeException + * + * @phpstan-template T of object + * @phpstan-param AdminInterface $admin + * @phpstan-return PagerInterface>|null + */ + public function search(AdminInterface $admin, string $term, int $page = 0, int $offset = 20): ?PagerInterface; +} From f01fcd07e1af5f73b230341d312875eca818d88e Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 10 Apr 2024 16:58:41 +0200 Subject: [PATCH 2/6] Rely on filter methods rather than option (#8172) --- src/Datagrid/Datagrid.php | 23 ++++++++++++++- src/Filter/Filter.php | 18 ++++++++++++ src/Filter/FilterInterface.php | 3 ++ .../views/Block/block_search_result.html.twig | 6 ++-- src/Resources/views/CRUD/base_list.html.twig | 16 ++++++---- tests/Datagrid/DatagridTest.php | 29 +++++++++++++++---- 6 files changed, 80 insertions(+), 15 deletions(-) diff --git a/src/Datagrid/Datagrid.php b/src/Datagrid/Datagrid.php index 0ffb9eaedb..98048744f3 100644 --- a/src/Datagrid/Datagrid.php +++ b/src/Datagrid/Datagrid.php @@ -325,7 +325,28 @@ private function buildForm(): FormInterface // NEXT_MAJOR: Keep the if part. if (method_exists($filter, 'getFormOptions')) { $type = FilterDataType::class; - $options = $filter->getFormOptions(); + + // NEXT_MAJOR: Keep the if part. + if (method_exists($filter, 'getLabelTranslationParameters')) { + $labelTranslationParameters = $filter->getLabelTranslationParameters(); + } else { + @trigger_error( + 'Not implementing "getLabelTranslationParameters()" is deprecated since sonata-project/admin-bundle 4.30' + .' and will throw an error in 5.0.', + \E_USER_DEPRECATED + ); + + $labelTranslationParameters = $filter->getOption('label_translation_parameters'); + } + + $defaultFormOptions = [ + 'label' => $filter->getLabel(), + 'label_translation_parameters' => $labelTranslationParameters, + 'translation_domain' => $filter->getTranslationDomain(), + 'field_type' => $filter->getFieldType(), + 'field_options' => $filter->getFieldOptions(), + ]; + $options = array_merge($defaultFormOptions, $filter->getFormOptions()); } else { @trigger_error( 'Not implementing "getFormOptions()" is deprecated since sonata-project/admin-bundle 4.15' diff --git a/src/Filter/Filter.php b/src/Filter/Filter.php index e54be96277..4231947f87 100644 --- a/src/Filter/Filter.php +++ b/src/Filter/Filter.php @@ -248,6 +248,24 @@ public function getRenderSettings(): array ]; } + final public function showFilter(): ?bool + { + return $this->getOption('show_filter'); + } + + /** + * @return array + */ + final public function getLabelTranslationParameters(): array + { + return $this->getOption('label_translation_parameters'); + } + + final public function withAdvancedFilter(): bool + { + return $this->getOption('advanced_filter'); + } + final protected function setActive(bool $active): void { $this->active = $active; diff --git a/src/Filter/FilterInterface.php b/src/Filter/FilterInterface.php index 74479309d8..d0f59b5c58 100644 --- a/src/Filter/FilterInterface.php +++ b/src/Filter/FilterInterface.php @@ -20,6 +20,9 @@ * @author Thomas Rabaix * * @method array getFormOptions(); + * @method bool|null showFilter(); + * @method array getLabelTranslationParameters(); + * @method bool withAdvancedFilter(); */ interface FilterInterface { diff --git a/src/Resources/views/Block/block_search_result.html.twig b/src/Resources/views/Block/block_search_result.html.twig index 19b67bc784..07e6413b40 100644 --- a/src/Resources/views/Block/block_search_result.html.twig +++ b/src/Resources/views/Block/block_search_result.html.twig @@ -45,10 +45,10 @@ file that was distributed with this source code.
{% for name, filter in filters %} - {% if filter.option('translation_domain') is same as(false) %} - {{ filter.option('label') }} + {% if filter.translationDomain is same as(false) %} + {{ filter.label }} {% else %} - {{ filter.option('label')|trans({}, filter.option('translation_domain', admin.translationDomain)) }} + {{ filter.label|trans({}, filter.translationDomain ?? admin.translationDomain) }} {% endif %} {% endfor %} diff --git a/src/Resources/views/CRUD/base_list.html.twig b/src/Resources/views/CRUD/base_list.html.twig index b833b0df95..5692b296e2 100644 --- a/src/Resources/views/CRUD/base_list.html.twig +++ b/src/Resources/views/CRUD/base_list.html.twig @@ -256,7 +256,8 @@ file that was distributed with this source code. {% endblock %} {% block list_filters_actions %} - {% set displayableFilters = admin.datagrid.filters|filter(filter => filter.option('show_filter') is not same as (false)) %} + {# NEXT_MAJOR: Remove |default(filter.option('show_filter')) #} + {% set displayableFilters = admin.datagrid.filters|filter(filter => filter.showFilter|default(filter.option('show_filter')) is not same as (false)) %} {%- if displayableFilters|length %}