Skip to content

Commit

Permalink
Show matching filters in search results
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Apr 9, 2021
1 parent db09b03 commit 7104342
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Block/AdminSearchBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -166,12 +167,17 @@ 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,
'filters' => $filters,
'admin' => $admin,
'show_empty_boxes' => $this->emptyBoxesOption,
], $response);
Expand Down
10 changes: 10 additions & 0 deletions src/Resources/public/css/layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 17 additions & 2 deletions src/Resources/views/Block/block_search_result.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,26 @@ file that was distributed with this source code.
</div>
{% if results_count > 0 %}
<div class="box-body no-padding">
<ul class="nav nav-stacked sonata-search-result-list">
<ul class="nav nav-stacked sonata-search-result-list products-list">
{% for result in current_page_results %}
{% set link = admin.getSearchResultLink(result) %}
{% if link %}
<li><a href="{{ link }}">{{ admin.toString(result) }}</a></li>
<li class="item no-padding">
<a class="main pull-left" href="{{ link }}">{{ admin.toString(result) }}</a>
<span class="matches pull-right">
{% for name, filter in filters %}
{% set match = name|split('.')|reduce((carry, v) => carry ? attribute(carry, v) : null, result) %}

{% if match and (((filter.options.case_sensitive or filter.options.case_sensitive is null) and filter.value.value in match)
or filter.options.case_sensitive is same as(false) and filter.value.value|lower in match|lower)
%}
<a class="label label-default" href="{{ admin.generateUrl('list', {'filter': {(filter.formName): filter.value}}) }}">
{{ filter.options.label|trans({}, filter.options.translation_domain|default(admin.translationDomain)) }}
</a>
{% endif %}
{% endfor %}
</span>
</li>
{% else %}
<li><a>{{ admin.toString(result) }}</a></li>
{% endif %}
Expand Down

0 comments on commit 7104342

Please sign in to comment.