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 12, 2021
1 parent f15b39a commit a2d7df2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
11 changes: 10 additions & 1 deletion 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 @@ -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')
);
Expand All @@ -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);
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
24 changes: 22 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,31 @@ 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) %}

{#
Some model managers are not respecting the "sonata_admin.global_search.case_sensitive" configuration option.
If you see no matching filters for a search result, it is probably the reason. In that case,
you SHOULD consider using `false` as value for this option.
#}
{% if match and ((filter.option('case_sensitive') is not same as(false) and term in match)
or filter.option('case_sensitive') is same as(false) and term|lower in match|lower)
%}
<a class="label label-primary" href="{{ admin.generateUrl('list', {'filter': {(filter.formName): term}}) }}">
{{ filter.option('label')|trans({}, filter.option('translation_domain', admin.translationDomain)) }}
</a>
{% endif %}
{% endfor %}
</span>
</li>
{% else %}
<li><a>{{ admin.toString(result) }}</a></li>
{% endif %}
Expand Down

0 comments on commit a2d7df2

Please sign in to comment.