From 6d77bdcc49262813b4070e3e4765ca16c736a398 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Thu, 1 Apr 2021 18:16:47 -0300 Subject: [PATCH] Show matching filters in search results --- src/Block/AdminSearchBlockService.php | 11 ++++++++- src/Resources/public/css/layout.css | 10 ++++++++ .../views/Block/block_search_result.html.twig | 24 +++++++++++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/Block/AdminSearchBlockService.php b/src/Block/AdminSearchBlockService.php index a911115d4d..a9241ae2fc 100644 --- a/src/Block/AdminSearchBlockService.php +++ b/src/Block/AdminSearchBlockService.php @@ -15,6 +15,7 @@ use Sonata\AdminBundle\Admin\AdminInterface; use Sonata\AdminBundle\Admin\Pool; +use Sonata\AdminBundle\Filter\FilterInterface; use Sonata\AdminBundle\Search\SearchHandler; use Sonata\BlockBundle\Block\BlockContextInterface; use Sonata\BlockBundle\Block\Service\AbstractBlockService; @@ -153,9 +154,11 @@ public function execute(BlockContextInterface $blockContext, ?Response $response $admin->checkAccess('list'); + $term = $blockContext->getSetting('query'); + $pager = $this->searchHandler->search( $admin, - $blockContext->getSetting('query'), + $term, $blockContext->getSetting('page'), $blockContext->getSetting('per_page') ); @@ -166,12 +169,18 @@ public function execute(BlockContextInterface $blockContext, ?Response $response return $response->setContent('')->setStatusCode(204); } + $filters = array_filter($admin->getDatagrid()->getFilters(), static function (FilterInterface $filter): bool { + return $filter->isActive(); + }); + return $this->renderPrivateResponse($admin->getTemplate('search_result_block'), [ 'block' => $blockContext->getBlock(), 'settings' => $blockContext->getSettings(), // NEXT_MAJOR: Remove next line. 'admin_pool' => $this->pool, 'pager' => $pager, + 'term' => $term, + 'filters' => $filters, 'admin' => $admin, 'show_empty_boxes' => $this->emptyBoxesOption, ], $response); diff --git a/src/Resources/public/css/layout.css b/src/Resources/public/css/layout.css index 8e8dec7b4b..1dba784b24 100644 --- a/src/Resources/public/css/layout.css +++ b/src/Resources/public/css/layout.css @@ -355,6 +355,16 @@ body.fixed .content-header .navbar.stuck { word-wrap: break-word; } +.sonata-search-result-list .box .nav-stacked > li.item > span.matches { + position: relative; + display: block; + padding: 10px 15px; +} + +.sonata-search-result-list .box .nav-stacked > li.item > span.matches > a.label { + margin: 0 1px; +} + .form-actions.stuck { position:fixed; bottom:0; diff --git a/src/Resources/views/Block/block_search_result.html.twig b/src/Resources/views/Block/block_search_result.html.twig index b9a90e2689..1666bc64d9 100644 --- a/src/Resources/views/Block/block_search_result.html.twig +++ b/src/Resources/views/Block/block_search_result.html.twig @@ -52,11 +52,31 @@ file that was distributed with this source code. {% if results_count > 0 %}
-