From a106327f843174525bc849b9fd2eb3414d456301 Mon Sep 17 00:00:00 2001 From: Valentin Silvestre Date: Wed, 23 Oct 2024 10:40:57 +0200 Subject: [PATCH] feat(attributes): AsFilter attribute --- docs/custom_filter.md | 18 +++------ .../SyliusGridExtension.php | 17 ++++++++ src/Component/Attribute/AsFilter.php | 39 +++++++++++++++++++ .../Application/config/sylius/grids/book.php | 6 +++ .../src/Filter/AttributeNationalityFilter.php | 35 +++++++++++++++++ .../Builder/AttributeNationalityFilter.php | 39 +++++++++++++++++++ 6 files changed, 141 insertions(+), 13 deletions(-) create mode 100644 src/Component/Attribute/AsFilter.php create mode 100644 tests/Application/src/Filter/AttributeNationalityFilter.php create mode 100644 tests/Application/src/Grid/Builder/AttributeNationalityFilter.php diff --git a/docs/custom_filter.md b/docs/custom_filter.md index 6b39abe3..c1d85a6a 100644 --- a/docs/custom_filter.md +++ b/docs/custom_filter.md @@ -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 { @@ -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; - } } ``` @@ -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: diff --git a/src/Bundle/DependencyInjection/SyliusGridExtension.php b/src/Bundle/DependencyInjection/SyliusGridExtension.php index 493e1dab..49e0a70f 100644 --- a/src/Bundle/DependencyInjection/SyliusGridExtension.php +++ b/src/Bundle/DependencyInjection/SyliusGridExtension.php @@ -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; @@ -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->getType() ?? $reflector->getName(), + 'form_type' => $attribute->getFormType(), + ]); + }, + ); + $container->registerForAutoconfiguration(FilterInterface::class) ->addTag('sylius.grid_filter') ; diff --git a/src/Component/Attribute/AsFilter.php b/src/Component/Attribute/AsFilter.php new file mode 100644 index 00000000..8207824d --- /dev/null +++ b/src/Component/Attribute/AsFilter.php @@ -0,0 +1,39 @@ +formType; + } + + public function getType(): ?string + { + return $this->type; + } +} diff --git a/tests/Application/config/sylius/grids/book.php b/tests/Application/config/sylius/grids/book.php index 460521a3..1732da27 100644 --- a/tests/Application/config/sylius/grids/book.php +++ b/tests/Application/config/sylius/grids/book.php @@ -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; @@ -31,6 +32,11 @@ null, ['author.nationality'], )) + ->addFilter(AttributeNationalityFilter::create( + AttributeNationalityFilter::class, + null, + ['author.nationality'], + )) ->addFilter(StringFilter::create( 'currencyCode', ['price.currencyCode'], diff --git a/tests/Application/src/Filter/AttributeNationalityFilter.php b/tests/Application/src/Filter/AttributeNationalityFilter.php new file mode 100644 index 00000000..678e6a64 --- /dev/null +++ b/tests/Application/src/Filter/AttributeNationalityFilter.php @@ -0,0 +1,35 @@ +decorated->apply($dataSource, $name, $data, $options); + } +} diff --git a/tests/Application/src/Grid/Builder/AttributeNationalityFilter.php b/tests/Application/src/Grid/Builder/AttributeNationalityFilter.php new file mode 100644 index 00000000..c6b8d7db --- /dev/null +++ b/tests/Application/src/Grid/Builder/AttributeNationalityFilter.php @@ -0,0 +1,39 @@ +setFormOptions(['class' => Nationality::class]); + + if (null !== $fields) { + $filter->setOptions(['fields' => $fields]); + } + + if (null !== $multiple) { + $filter->addFormOption('multiple', $multiple); + } + + return $filter; + } +}