Skip to content

Commit

Permalink
Fix block_search_result
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jul 24, 2021
1 parent b9b03b6 commit abdf0a4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 36 deletions.
8 changes: 4 additions & 4 deletions assets/scss/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ th {
> li {
word-wrap: break-word;
}
}

&.nav-stacked > li.item > span.matches {
display: block;
padding: 10px 15px;
position: relative;
.search-box-item {
.matches {
overflow-x: scroll;

> a.label {
margin: 0 1px;
Expand Down
3 changes: 2 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\Pool;
use Sonata\AdminBundle\Filter\FilterInterface;
use Sonata\AdminBundle\Search\SearchableFilterInterface;
use Sonata\AdminBundle\Search\SearchHandler;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Sonata\BlockBundle\Block\BlockContextInterface;
Expand Down Expand Up @@ -93,7 +94,7 @@ public function execute(BlockContextInterface $blockContext, ?Response $response
}

$filters = array_filter($admin->getDatagrid()->getFilters(), static function (FilterInterface $filter): bool {
return $filter->isActive();
return $filter instanceof SearchableFilterInterface && $filter->isSearchEnabled();
});

return $this->renderPrivateResponse($this->templateRegistry->getTemplate('search_result_block'), [
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/app.css

Large diffs are not rendered by default.

47 changes: 17 additions & 30 deletions src/Resources/views/Block/block_search_result.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ file that was distributed with this source code.
{% extends sonata_block.templates.block_base %}

{% block block %}
{% set visibility_class = 'sonata-search-result-' ~ show_empty_boxes %}
{% set current_page_results = [] %}
{% set results_count = pager ? pager.countResults() : 0 %}
{% if results_count > 0 %}
{% set current_page_results = pager.currentPageResults %}
{% set visibility_class = 'sonata-search-result-show' %}
{% endif %}
{% set has_results = results_count > 0 %}
{% set current_page_results = has_results ? pager.currentPageResults : [] %}
{% set visibility_class = 'sonata-search-result-' ~ has_result ? 'show' : show_empty_boxes %}

This comment has been minimized.

Copy link
@vindert

vindert Aug 9, 2021

Contributor

There is a typo on this line, I think it should read has_results

This comment has been minimized.

Copy link
@VincentLanglet

VincentLanglet Aug 10, 2021

Author Member

<div class="col-lg-4 col-md-6 search-box-item {{ visibility_class }}">
<div class="box box-solid {{ visibility_class }}">
Expand All @@ -32,7 +29,7 @@ file that was distributed with this source code.
</h3>

<div class="box-tools pull-right">
{% if results_count > 0 %}
{% if has_results %}
<span class="badge">{{ results_count }}</span>
{% elseif admin.hasRoute('create') and admin.hasAccess('create') %}
<a href="{{ admin.generateUrl('create') }}" class="btn btn-box-tool">
Expand All @@ -45,37 +42,27 @@ file that was distributed with this source code.
</a>
{% endif %}
</div>

<div class="matches">
{% for name, filter in filters %}
<a class="label label-primary" href="{{ admin.generateUrl('list', {'filter': {(filter.formName): {'value': term}}}) }}">
{% if filter.option('translation_domain') is same as(false) %}
{{ filter.option('label') }}
{% else %}
{{ filter.option('label')|trans({}, filter.option('translation_domain', admin.translationDomain)) }}
{% endif %}
</a>
{% endfor %}
</div>
</div>
{% if results_count > 0 %}
{% if has_results %}
<div class="box-body no-padding">
<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 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): {'value': term}}}) }}">
{% if filter.option('translation_domain') is same as(false) %}
{{ filter.option('label') }}
{% else %}
{{ filter.option('label')|trans({}, filter.option('translation_domain', admin.translationDomain)) }}
{% endif %}
</a>
{% endif %}
{% endfor %}
</span>
</li>
{% else %}
<li><a>{{ admin.toString(result) }}</a></li>
Expand Down

0 comments on commit abdf0a4

Please sign in to comment.