-
Notifications
You must be signed in to change notification settings - Fork 50
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
base: 1.14
Are you sure you want to change the base?
Conversation
bfb5387
to
4b58c75
Compare
cd7c39a
to
215bc11
Compare
tests/Application/src/Grid/Builder/AttributeNationalityFilter.php
Outdated
Show resolved
Hide resolved
f6641a8
to
12a8575
Compare
c62a533
to
f63f29a
Compare
f63f29a
to
50b13cc
Compare
src/Bundle/DependencyInjection/Compiler/RegisterFiltersPass.php
Outdated
Show resolved
Hide resolved
50b13cc
to
81ae79d
Compare
src/Bundle/DependencyInjection/Compiler/RegisterFiltersPass.php
Outdated
Show resolved
Hide resolved
@vasilvestre you have two commits with "-" as commit message |
9edf05d
to
52e0442
Compare
ccb9197
to
a106327
Compare
1c49e46
to
f500a89
Compare
@@ -31,6 +32,11 @@ | |||
null, | |||
['author.nationality'], | |||
)) | |||
->addFilter(AttributeNationalityFilter::create( | |||
AttributeNationalityFilter::class, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is a bit strange... but I understand why.
The FQCN is the type/name of the filter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vasilvestre Could you remember me why you don't use the Filter::create method instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did line 35
@@ -31,6 +32,11 @@ | |||
null, | |||
['author.nationality'], | |||
)) | |||
->addFilter(AttributeNationalityFilter::create( |
There was a problem hiding this comment.
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:
SyliusGridBundle/src/Bundle/Tests/Functional/GridUiTest.php
Lines 235 to 245 in f500a89
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); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's covered in https://github.com/Sylius/SyliusGridBundle/blob/f500a89f5ea35fd675012c30e55d0def0a751cc3/tests/Application/src/Filter/AttributeNationalityFilter.php and https://github.com/Sylius/SyliusGridBundle/blob/f500a89f5ea35fd675012c30e55d0def0a751cc3/tests/Application/src/Grid/Builder/AttributeNationalityFilter.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added tests on the RegisterFiltersPass itself.
I've tried locally and have seen some errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does not change it yourself, I have tests in progress.
ce6ee05
to
8986a33
Compare
8986a33
to
bf029b4
Compare
$container->registerForAutoconfiguration(FilterInterface::class) | ||
->addTag('sylius.grid_filter') | ||
->addTag('sylius.legacy_grid_filter') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We now tagged the Attribute instead, but we need the ones that implements TypeAwareFilterInterface and FormTypeAwareFilterInterface which contains static methods for the type and the form type.
@@ -28,10 +30,21 @@ public function process(ContainerBuilder $container): void | |||
return; | |||
} | |||
|
|||
$registry = $container->getDefinition('sylius.registry.grid_filter'); | |||
$filterRegistry = $container->getDefinition('sylius.registry.grid_filter'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If registry
is renamed to filterRegistry
, then formTypeRegistry
should be become filterFormTypeRegister
, hence I propose to remove this change.
$filterRegistry = $container->getDefinition('sylius.registry.grid_filter'); | |
$registry = $container->getDefinition('sylius.registry.grid_filter'); |
$registry->addMethodCall('register', [$type ?? $attribute['type'], new Reference($id)]); | ||
$formTypeRegistry->addMethodCall('add', [$type ?? $attribute['type'], 'default', $formType ?? $attribute['form_type']]); | ||
if (null === $formType && null === ($attribute['form_type'] ?? null)) { | ||
throw new InvalidArgumentException(sprintf('Tagged grid filters needs to have "form_type" attribute or implements "%s".', FormTypeAwareFilterInterface::class)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to SF CS, which is basis for sylius CS, global classes should be used directly and not added to use
.
throw new InvalidArgumentException(sprintf('Tagged grid filters needs to have "form_type" attribute or implements "%s".', FormTypeAwareFilterInterface::class)); | |
throw new \InvalidArgumentException(sprintf('Tagged grid filters needs to have "form_type" attribute or implements "%s".', FormTypeAwareFilterInterface::class)); |
@@ -14,10 +14,12 @@ | |||
namespace Sylius\Bundle\GridBundle\DependencyInjection\Compiler; | |||
|
|||
use InvalidArgumentException; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use InvalidArgumentException; |
|
||
namespace App\Filter; | ||
|
||
use App\Grid\Type\NationalityFilterType; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move form type to usual SF placement.
use App\Grid\Type\NationalityFilterType; | |
use App\Form\Type\Grid\NationalityFilterType; |
]); | ||
}, | ||
); | ||
|
||
$container->registerForAutoconfiguration(FilterInterface::class) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should use this interface instead, that was a mistake to use it cause the FilterInterface itself is not enough to auconfigure it.
$container->registerForAutoconfiguration(FilterInterface::class) | |
$container->registerForAutoconfiguration(ConfigurableFilterInterface::class) |
This PR introduces a new attribute,
AsFilter
, to provide a more concise and structured way of defining filter types and form types for grid filters.Previously, filter types and form types were defined two methods inherited from
ConfigurableFilterInterface
.By using the
AsFilter
attribute, developers can now explicitly declare the type and form type of a filter directly above the class definition. This approach improves code readability, reduces reliance on tag attributes, and enhances maintainability.Steps:
Example usage:
The
RegisterFiltersPass
compiler pass has been updated to check for theAsFilter
attribute and ignore duplicate filter configuration.Tests are provided in the PR, documentation too.
A new behaviour is that type is now optional as the FQCN is now used by default. Which means you can reference it directly in PHP or YAML .
Future considerations:
In a future PR, we can consider deprecating the
ConfigurableFilterInterface
as it becomes redundant by introducing theAsFilter
attribute. This will further simplify the process of creating grid filters and improve consistency.