diff --git a/src/Bundle/DependencyInjection/Compiler/RegisterFiltersPass.php b/src/Bundle/DependencyInjection/Compiler/RegisterFiltersPass.php index 71565417..bc25a674 100644 --- a/src/Bundle/DependencyInjection/Compiler/RegisterFiltersPass.php +++ b/src/Bundle/DependencyInjection/Compiler/RegisterFiltersPass.php @@ -44,6 +44,10 @@ public function process(ContainerBuilder $container): void } foreach ($attributes as $attribute) { + if (isset($attribute['type'], $attribute['form_type'])) { + return; + } + if (null === $type && null === ($attribute['type'] ?? null)) { throw new InvalidArgumentException(sprintf('Tagged grid filters needs to have "type" attributes or implements "%s".', TypeAwareFilterInterface::class)); } diff --git a/src/Bundle/DependencyInjection/SyliusGridExtension.php b/src/Bundle/DependencyInjection/SyliusGridExtension.php index 493e1dab..aeeef017 100644 --- a/src/Bundle/DependencyInjection/SyliusGridExtension.php +++ b/src/Bundle/DependencyInjection/SyliusGridExtension.php @@ -18,7 +18,9 @@ use Sylius\Bundle\GridBundle\SyliusGridBundle; use Sylius\Component\Grid\Data\DataProviderInterface; use Sylius\Component\Grid\Filtering\FilterInterface; +use Sylius\Component\Grid\Metadata\AsFilter; 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,14 @@ public function load(array $configs, ContainerBuilder $container): void ->addTag('sylius.grid') ; + $container->registerAttributeForAutoconfiguration(AsFilter::class, static function (ChildDefinition $definition, AsFilter $attribute, \ReflectionClass $reflector) { + $tagAttributes = [ + 'type' => $attribute->type, + 'form_type' => $attribute->formType, + ]; + $definition->addTag('sylius.grid_filter', $tagAttributes); + }); + $container->registerForAutoconfiguration(FilterInterface::class) ->addTag('sylius.grid_filter') ; diff --git a/src/Component/Metadata/AsFilter.php b/src/Component/Metadata/AsFilter.php new file mode 100644 index 00000000..a7747ab0 --- /dev/null +++ b/src/Component/Metadata/AsFilter.php @@ -0,0 +1,24 @@ +addFilter(AttributeNationalityFilter::create( + 'attribute_nationality', + 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..078fcdab --- /dev/null +++ b/tests/Application/src/Filter/AttributeNationalityFilter.php @@ -0,0 +1,36 @@ +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..e247dcbb --- /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; + } +}