-
Notifications
You must be signed in to change notification settings - Fork 49
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
) { | ||
} | ||
} |
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. 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. Yes, this is a bit strange... but I understand why. 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. @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 commentThe reason will be displayed to describe this comment to others. Learn more. I did line 35 |
||
null, | ||
['author.nationality'], | ||
)) | ||
->addFilter(StringFilter::create( | ||
'currencyCode', | ||
['price.currencyCode'], | ||
|
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); | ||
} | ||
} |
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; | ||
} | ||
} |
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