Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce AsFilter attribute to define filter type and form type #348

Open
wants to merge 2 commits into
base: 1.14
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions docs/custom_filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ namespace App\Grid\Filter;

use App\Form\Type\Filter\SuppliersStatisticsFilterType;
use Sylius\Bundle\GridBundle\Doctrine\DataSourceInterface;
use Sylius\Component\Grid\Filtering\ConfiguragurableFilterInterface;
use Sylius\Component\Grid\Filtering\FilterInterface;
use Sylius\Component\Grid\Attribute\AsFilter;

class SuppliersStatisticsFilter implements ConfiguragurableFilterInterface
#[AsFilter(formType: SuppliersStatisticsFilterType::class)]
class SuppliersStatisticsFilter implements FilterInterface
{
public function apply(DataSourceInterface $dataSource, $name, $data, array $options = []): void
{
Expand All @@ -32,16 +34,6 @@ class SuppliersStatisticsFilter implements ConfiguragurableFilterInterface
// here is an example
$dataSource->restrict($dataSource->getExpressionBuilder()->equals('stats', $data['stats']));
}

public static function getType() : string
{
return 'suppliers_statistics';
}

public static function getFormType() : string
{
return SuppliersStatisticsFilterType::class;
}
}
```

Expand Down Expand Up @@ -116,7 +108,7 @@ sylius_grid:
resource: app.tournament
filters:
stats:
type: suppliers_statistics
type: !php/const App\Grid\Filter\SuppliersStatisticsFilter::class
form_options:
range: [0, 100]
templates:
Expand Down
17 changes: 17 additions & 0 deletions src/Bundle/DependencyInjection/SyliusGridExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
use Sylius\Bundle\CurrencyBundle\SyliusCurrencyBundle;
use Sylius\Bundle\GridBundle\Grid\GridInterface;
use Sylius\Bundle\GridBundle\SyliusGridBundle;
use Sylius\Component\Grid\Attribute\AsFilter;
use Sylius\Component\Grid\Data\DataProviderInterface;
use Sylius\Component\Grid\Filtering\FilterInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
Expand Down Expand Up @@ -60,6 +62,21 @@ public function load(array $configs, ContainerBuilder $container): void
->addTag('sylius.grid')
;

$container->registerAttributeForAutoconfiguration(
AsFilter::class,
static function (ChildDefinition $definition, AsFilter $attribute, \Reflector $reflector): void {
// Helps to avoid issues with psalm
if (!$reflector instanceof \ReflectionClass) {
return;
}

$definition->addTag(AsFilter::SERVICE_TAG, [
'type' => $attribute->type ?? $reflector->getName(),
'form_type' => $attribute->formType,
]);
},
);

$container->registerForAutoconfiguration(FilterInterface::class)
->addTag('sylius.grid_filter')
;
Expand Down
29 changes: 29 additions & 0 deletions src/Component/Attribute/AsFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Grid\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsFilter
{
public const SERVICE_TAG = 'sylius.grid.filter';

/**
* @param class-string $formType The form type class name to use for filter rendering
*/
public function __construct(
public readonly string $formType,
public readonly ?string $type = null,
) {
}
}
6 changes: 6 additions & 0 deletions tests/Application/config/sylius/grids/book.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use App\Entity\Author;
use App\Entity\Book;
use App\Grid\Builder\AttributeNationalityFilter;
use App\Grid\Builder\NationalityFilter;
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
use Sylius\Bundle\GridBundle\Builder\Filter\EntityFilter;
Expand All @@ -31,6 +32,11 @@
null,
['author.nationality'],
))
->addFilter(AttributeNationalityFilter::create(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is it tested? Does it override nationality filter and is covered by this test:

public function it_includes_all_rows_even_when_sorting_by_a_nullable_path(): void
{
$this->client->request('GET', '/authors/');
$totalItemsCountBeforeSorting = count($this->getAuthorNamesFromResponse());
$this->client->request('GET', '/authors/?sorting[nationality]=desc');
$totalItemsCountAfterSorting = count($this->getAuthorNamesFromResponse());
$this->assertSame($totalItemsCountBeforeSorting, $totalItemsCountAfterSorting);
}
or is not covered by test and we don't know if it works?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AttributeNationalityFilter::class,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems to be incorrect, or at least misleading

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ? We can use FQCN as name and also call the create method of the filter.

null,
['author.nationality'],
))
->addFilter(StringFilter::create(
'currencyCode',
['price.currencyCode'],
Expand Down
35 changes: 35 additions & 0 deletions tests/Application/src/Filter/AttributeNationalityFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Filter;

use App\Grid\Type\NationalityFilterType;
use Sylius\Component\Grid\Attribute\AsFilter;
use Sylius\Component\Grid\Data\DataSourceInterface;
use Sylius\Component\Grid\Filter\EntityFilter;
use Sylius\Component\Grid\Filtering\FilterInterface;

#[AsFilter(
formType: NationalityFilterType::class,
vasilvestre marked this conversation as resolved.
Show resolved Hide resolved
)]
final class AttributeNationalityFilter implements FilterInterface
{
public function __construct(private EntityFilter $decorated)
{
}

public function apply(DataSourceInterface $dataSource, string $name, $data, array $options): void
{
$this->decorated->apply($dataSource, $name, $data, $options);
}
}
39 changes: 39 additions & 0 deletions tests/Application/src/Grid/Builder/AttributeNationalityFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Grid\Builder;

use App\Entity\Nationality;
use App\Filter\AttributeNationalityFilter as GridNationalityFilter;
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\Filter\FilterInterface;

final class AttributeNationalityFilter
{
public static function create(string $name, ?bool $multiple = null, ?array $fields = null): FilterInterface
{
$filter = Filter::create($name, GridNationalityFilter::class);

$filter->setFormOptions(['class' => Nationality::class]);

if (null !== $fields) {
$filter->setOptions(['fields' => $fields]);
}

if (null !== $multiple) {
$filter->addFormOption('multiple', $multiple);
}

return $filter;
}
}
Loading