Skip to content

Commit ae08f3b

Browse files
committed
Introduce AsField attribute to define field type
1 parent e0f24db commit ae08f3b

File tree

6 files changed

+139
-8
lines changed

6 files changed

+139
-8
lines changed

src/Bundle/DependencyInjection/SyliusGridExtension.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Sylius\Bundle\GridBundle\Grid\GridInterface;
2020
use Sylius\Bundle\GridBundle\SyliusGridBundle;
2121
use Sylius\Component\Grid\Annotation\AsGridFieldCallableService;
22+
use Sylius\Component\Grid\Attribute\AsField;
2223
use Sylius\Component\Grid\Attribute\AsFilter;
2324
use Sylius\Component\Grid\Data\DataProviderInterface;
2425
use Sylius\Component\Grid\Filtering\ConfigurableFilterInterface;
@@ -73,12 +74,7 @@ public function load(array $configs, ContainerBuilder $container): void
7374

7475
$container->registerAttributeForAutoconfiguration(
7576
AsFilter::class,
76-
static function (ChildDefinition $definition, AsFilter $attribute, \Reflector $reflector): void {
77-
// Helps to avoid issues with psalm
78-
if (!$reflector instanceof \ReflectionClass) {
79-
return;
80-
}
81-
77+
static function (ChildDefinition $definition, AsFilter $attribute, \ReflectionClass $reflector): void {
8278
$definition->addTag(AsFilter::SERVICE_TAG, [
8379
'type' => $attribute->type ?? $reflector->getName(),
8480
'form_type' => $attribute->formType,
@@ -87,6 +83,15 @@ static function (ChildDefinition $definition, AsFilter $attribute, \Reflector $r
8783
},
8884
);
8985

86+
$container->registerAttributeForAutoconfiguration(
87+
AsField::class,
88+
static function (ChildDefinition $definition, AsField $attribute, \ReflectionClass $reflector): void {
89+
$definition->addTag(AsField::SERVICE_TAG, [
90+
'type' => $attribute->type ?? $reflector->getName(),
91+
]);
92+
},
93+
);
94+
9095
$container->registerForAutoconfiguration(ConfigurableFilterInterface::class)
9196
->addTag(AsFilter::SERVICE_TAG)
9297
;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Integration;
15+
16+
use App\Field\FirstCustomField;
17+
use Sylius\Component\Registry\ServiceRegistry;
18+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
19+
20+
final class CustomFiltersTest extends KernelTestCase
21+
{
22+
public function testItRegistersCustomFilters(): void
23+
{
24+
$container = static::getContainer();
25+
26+
/** @var ServiceRegistry $fieldRegistry */
27+
$fieldRegistry = $container->get('sylius.registry.grid_field');
28+
29+
self::assertTrue($fieldRegistry->has(FirstCustomField::class));
30+
self::assertTrue($fieldRegistry->has('custom_two'));
31+
}
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Component\Grid\Attribute;
15+
16+
#[\Attribute(\Attribute::TARGET_CLASS)]
17+
final class AsField
18+
{
19+
public const SERVICE_TAG = 'sylius.grid_field';
20+
21+
public function __construct(
22+
public readonly ?string $type = null,
23+
) {
24+
}
25+
}

tests/Application/config/services.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ services:
2525
- '@app.repository.author'
2626
public: true
2727

28-
App\Filter\AttributeNationalityFilter: null
29-
App\Filter\NationalityFilter: null
28+
# Fields
29+
App\Field\:
30+
resource: '../src/Field'
31+
32+
# Filters
33+
App\Filter\:
34+
resource: '../src/Filter'
3035

3136
App\BoardGameBlog\:
3237
resource: '../src/BoardGameBlog'
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Field;
15+
16+
use Sylius\Component\Grid\Attribute\AsField;
17+
use Sylius\Component\Grid\Definition\Field;
18+
use Sylius\Component\Grid\FieldTypes\FieldTypeInterface;
19+
use Symfony\Component\OptionsResolver\OptionsResolver;
20+
21+
#[AsField]
22+
class FirstCustomField implements FieldTypeInterface
23+
{
24+
public function render(Field $field, $data, array $options): string
25+
{
26+
return '';
27+
}
28+
29+
public function configureOptions(OptionsResolver $resolver): void
30+
{
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Field;
15+
16+
use Sylius\Component\Grid\Attribute\AsField;
17+
use Sylius\Component\Grid\Definition\Field;
18+
use Sylius\Component\Grid\FieldTypes\FieldTypeInterface;
19+
use Symfony\Component\OptionsResolver\OptionsResolver;
20+
21+
#[AsField(type: 'custom_two')]
22+
class SecondCustomField implements FieldTypeInterface
23+
{
24+
public function render(Field $field, $data, array $options): string
25+
{
26+
return '';
27+
}
28+
29+
public function configureOptions(OptionsResolver $resolver): void
30+
{
31+
}
32+
}

0 commit comments

Comments
 (0)