-
Notifications
You must be signed in to change notification settings - Fork 48
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
vasilvestre
wants to merge
2
commits into
Sylius:1.14
Choose a base branch
from
vasilvestre:feat/atttributes/asgrid
base: 1.14
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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'], | ||
|
35 changes: 35 additions & 0 deletions
35
tests/Application/src/Filter/AttributeNationalityFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
tests/Application/src/Grid/Builder/AttributeNationalityFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
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