Skip to content

Commit

Permalink
Rely on filter methods rather than option (#8172)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Apr 10, 2024
1 parent 1edb927 commit f01fcd0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 15 deletions.
23 changes: 22 additions & 1 deletion src/Datagrid/Datagrid.php
Expand Up @@ -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'
Expand Down
18 changes: 18 additions & 0 deletions src/Filter/Filter.php
Expand Up @@ -248,6 +248,24 @@ public function getRenderSettings(): array
];
}

final public function showFilter(): ?bool
{
return $this->getOption('show_filter');
}

/**
* @return array<string, mixed>
*/
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;
Expand Down
3 changes: 3 additions & 0 deletions src/Filter/FilterInterface.php
Expand Up @@ -20,6 +20,9 @@
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* @method array getFormOptions();
* @method bool|null showFilter();
* @method array getLabelTranslationParameters();
* @method bool withAdvancedFilter();
*/
interface FilterInterface
{
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/views/Block/block_search_result.html.twig
Expand Up @@ -45,10 +45,10 @@ file that was distributed with this source code.
<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') }}
{% 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 %}
</a>
{% endfor %}
Expand Down
16 changes: 11 additions & 5 deletions src/Resources/views/CRUD/base_list.html.twig
Expand Up @@ -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 %}
<ul class="nav navbar-nav navbar-right">

Expand All @@ -270,14 +271,17 @@ file that was distributed with this source code.

<ul class="dropdown-menu dropdown-menu-scrollable" role="menu">
{% for filter in displayableFilters %}
{% set filterDisplayed = filter.isActive() or filter.option('show_filter') is same as (true) %}
{# NEXT_MAJOR: Remove |default(filter.option('show_filter')) #}
{% set filterDisplayed = filter.isActive() or filter.showFilter|default(filter.option('show_filter')) is same as (true) %}
<li>
<a href="#" class="sonata-toggle-filter sonata-ba-action" filter-target="filter-{{ admin.uniqid }}-{{ filter.name }}" filter-container="filter-container-{{ admin.uniqid() }}">
<i class="far {{ filterDisplayed ? 'fa-check-square' : 'fa-square' }}"></i>
{% if filter.label is not same as(false) %}
{% if filter.translationDomain is same as(false) %}
{{ filter.label }}
{% else %}
{% elseif filter.labelTranslationParameters is defined %}
{{ filter.label|trans(filter.labelTranslationParameters, filter.translationDomain ?? admin.translationDomain) }}
{% else %} {# NEXT_MAJOR: remove the else part and change elseif to else #}
{{ filter.label|trans(filter.option('label_translation_parameters', {}), filter.translationDomain ?? admin.translationDomain) }}
{% endif %}
{% endif %}
Expand Down Expand Up @@ -310,7 +314,8 @@ file that was distributed with this source code.
<div class="col-sm-9">
{% set withAdvancedFilter = false %}
{% for filter in admin.datagrid.filters %}
{% set filterDisplayed = filter.isActive() or filter.option('show_filter') is same as (true) %}
{# NEXT_MAJOR: Remove |default(filter.option('show_filter')) #}
{% set filterDisplayed = filter.isActive() or filter.showFilter|default(filter.option('show_filter')) is same as (true) %}
{% set filterCanBeDisplayed = filter.option('show_filter') is not same as(false) %}
<div class="form-group {% block sonata_list_filter_group_class %}{% endblock %}" id="filter-{{ admin.uniqid }}-{{ filter.name }}" sonata-filter="{{ filterCanBeDisplayed ? 'true' : 'false' }}" style="display: {% if filterDisplayed %}block{% else %}none{% endif %}">
{% if filter.label is not same as(false) %}
Expand Down Expand Up @@ -343,7 +348,8 @@ file that was distributed with this source code.
{% endif %}
</div>

{% if filter.option('advanced_filter') %}
{# NEXT_MAJOR: Remove |default(filter.option('advanced_filter')) #}
{% if filter.withAdvancedFilter|default(filter.option('advanced_filter')) %}
{% set withAdvancedFilter = true %}
{% endif %}
{% endfor %}
Expand Down
29 changes: 23 additions & 6 deletions tests/Datagrid/DatagridTest.php
Expand Up @@ -25,6 +25,7 @@
use Sonata\AdminBundle\Filter\FilterInterface;
use Sonata\AdminBundle\Form\Type\Filter\FilterDataType;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
Expand Down Expand Up @@ -318,7 +319,7 @@ public function testEmptyResults(): void
public function testBuildPager(): void
{
$filter1 = $this->getMockBuilder(FilterInterface::class)
->addMethods(['getFormOptions'])
->addMethods(['getFormOptions', 'getLabelTranslationParameters'])
->getMockForAbstractClass();
$filter1->expects(static::once())
->method('getName')
Expand All @@ -329,14 +330,17 @@ public function testBuildPager(): void
$filter1
->method('isActive')
->willReturn(false);
$filter1
->method('getFieldType')
->willReturn(TextType::class);
$filter1
->method('getFormOptions')
->willReturn(['operator_options' => ['help' => 'baz1']]);

$this->datagrid->addFilter($filter1);

$filter2 = $this->getMockBuilder(FilterInterface::class)
->addMethods(['getFormOptions'])
->addMethods(['getFormOptions', 'getLabelTranslationParameters'])
->getMockForAbstractClass();
$filter2->expects(static::once())
->method('getName')
Expand All @@ -347,6 +351,9 @@ public function testBuildPager(): void
$filter2
->method('isActive')
->willReturn(true);
$filter2
->method('getFieldType')
->willReturn(TextType::class);
$filter2
->method('getFormOptions')
->willReturn(['operator_options' => ['help' => 'baz2']]);
Expand Down Expand Up @@ -374,12 +381,13 @@ public function testApplyFilter(?string $type, ?string $value, int $applyCallNum
$this->datagrid->setValue('fooFormName', $type, $value);

$filter = $this->getMockBuilder(FilterInterface::class)
->addMethods(['getFormOptions'])
->addMethods(['getFormOptions', 'getLabelTranslationParameters'])
->getMockForAbstractClass();
$filter->expects(static::once())->method('getName')->willReturn('foo');
$filter->method('getFormName')->willReturn('fooFormName');
$filter->method('isActive')->willReturn(false);
$filter->method('getFormOptions')->willReturn(['operator_options' => ['help' => 'baz2']]);
$filter->method('getFieldType')->willReturn(TextType::class);
$filter->expects(static::exactly($applyCallNumber))->method('apply');

$this->datagrid->addFilter($filter);
Expand Down Expand Up @@ -430,7 +438,7 @@ public function applyFilterDataProvider(): iterable
public function testBuildPagerWithException(): void
{
$filter = $this->getMockBuilder(FilterInterface::class)
->addMethods(['getFormOptions'])
->addMethods(['getFormOptions', 'getLabelTranslationParameters'])
->getMockForAbstractClass();
$filter->expects(static::once())
->method('getName')
Expand All @@ -443,6 +451,9 @@ public function testBuildPagerWithException(): void
$filter
->method('isActive')
->willReturn(false);
$filter
->method('getFieldType')
->willReturn(TextType::class);
$filter
->method('getFormOptions')
->willReturn(['operator_options' => ['help' => 'baz']]);
Expand Down Expand Up @@ -475,7 +486,7 @@ public function testBuildPagerWithSortBy(): void
$this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, [DatagridInterface::SORT_BY => $sortBy]);

$filter = $this->getMockBuilder(FilterInterface::class)
->addMethods(['getFormOptions'])
->addMethods(['getFormOptions', 'getLabelTranslationParameters'])
->getMockForAbstractClass();
$filter->expects(static::once())
->method('getName')
Expand All @@ -486,6 +497,9 @@ public function testBuildPagerWithSortBy(): void
$filter
->method('isActive')
->willReturn(false);
$filter
->method('getFieldType')
->willReturn(TextType::class);
$filter
->method('getFormOptions')
->willReturn(['operator_options' => ['help' => 'baz']]);
Expand Down Expand Up @@ -526,7 +540,7 @@ public function testBuildPagerWithPage(int $page, int|array $perPage): void
$this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, [DatagridInterface::SORT_BY => $sortBy, DatagridInterface::PAGE => $page, DatagridInterface::PER_PAGE => $perPage]);

$filter = $this->getMockBuilder(FilterInterface::class)
->addMethods(['getFormOptions'])
->addMethods(['getFormOptions', 'getLabelTranslationParameters'])
->getMockForAbstractClass();
$filter->expects(static::once())
->method('getName')
Expand All @@ -537,6 +551,9 @@ public function testBuildPagerWithPage(int $page, int|array $perPage): void
$filter
->method('isActive')
->willReturn(false);
$filter
->method('getFieldType')
->willReturn(TextType::class);
$filter
->method('getFormOptions')
->willReturn(['operator_options' => ['help' => 'baz']]);
Expand Down

0 comments on commit f01fcd0

Please sign in to comment.