Skip to content

Commit 41dea4e

Browse files
committed
fix(grid): add default EnumFilter Twig template
1 parent b476a41 commit 41dea4e

File tree

14 files changed

+745
-641
lines changed

14 files changed

+745
-641
lines changed

app/Entity/Book.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace App\Entity;
1515

16+
use App\Enum\BookCategory;
1617
use App\Grid\BookGrid;
1718
use App\Repository\BookRepository;
1819
use App\Responder\ExportGridToCsvResponder;
@@ -79,6 +80,9 @@ class Book implements ResourceInterface
7980
#[NotBlank]
8081
private ?string $authorName = null;
8182

83+
#[ORM\Column(type: 'enum', length: 255, nullable: true)]
84+
private ?BookCategory $category = null;
85+
8286
#[ORM\Column(type: 'datetime_immutable')]
8387
private \DateTimeImmutable $createdAt;
8488

@@ -121,4 +125,14 @@ public function setCreatedAt(\DateTimeImmutable $createdAt): void
121125
{
122126
$this->createdAt = $createdAt;
123127
}
128+
129+
public function getCategory(): ?BookCategory
130+
{
131+
return $this->category;
132+
}
133+
134+
public function setCategory(?BookCategory $category): void
135+
{
136+
$this->category = $category;
137+
}
124138
}

app/Enum/BookCategory.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Enum;
15+
16+
use Symfony\Contracts\Translation\TranslatableInterface;
17+
use Symfony\Contracts\Translation\TranslatorInterface;
18+
19+
enum BookCategory: string implements TranslatableInterface
20+
{
21+
case FICTION = 'fiction';
22+
case NON_FICTION = 'non-fiction';
23+
case MYSTERY = 'mystery';
24+
case THRILLER = 'thriller';
25+
case SCIENCE_FICTION = 'science-fiction';
26+
case FANTASY = 'fantasy';
27+
case ROMANCE = 'romance';
28+
case HORROR = 'horror';
29+
case BIOGRAPHY = 'biography';
30+
case HISTORY = 'history';
31+
case SCIENCE = 'science';
32+
case TECHNOLOGY = 'technology';
33+
case BUSINESS = 'business';
34+
case COOKING = 'cooking';
35+
case TRAVEL = 'travel';
36+
case POETRY = 'poetry';
37+
case DRAMA = 'drama';
38+
case CHILDREN = 'children';
39+
case YOUNG_ADULT = 'young adult';
40+
case RELIGION = 'religion';
41+
case PHILOSOPHY = 'philosophy';
42+
case ART = 'art';
43+
case MANGA = 'manga';
44+
case COMICS = 'comics';
45+
46+
/** added for Symfony 6 compatibility */
47+
public function trans(TranslatorInterface $translator, ?string $locale = null): string
48+
{
49+
return ucfirst($translator->trans($this->value, locale: $locale));
50+
}
51+
}

app/Factory/BookFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace App\Factory;
1515

1616
use App\Entity\Book;
17+
use App\Enum\BookCategory;
1718
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
1819

1920
/**
@@ -36,11 +37,17 @@ public function withAuthorName(string $authorName): self
3637
return $this->with(['authorName' => $authorName]);
3738
}
3839

40+
public function withCategory(BookCategory $category): self
41+
{
42+
return $this->with(['category' => $category]);
43+
}
44+
3945
protected function defaults(): array|callable
4046
{
4147
return [
4248
'title' => ucfirst(self::faker()->words(3, true)),
4349
'authorName' => self::faker()->firstName() . ' ' . self::faker()->lastName(),
50+
'category' => self::faker()->randomElement(BookCategory::cases()),
4451
];
4552
}
4653
}

app/Grid/BookGrid.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace App\Grid;
1515

1616
use App\Entity\Book;
17+
use App\Enum\BookCategory;
1718
use Sylius\Bundle\GridBundle\Builder\Action\Action;
1819
use Sylius\Bundle\GridBundle\Builder\Action\CreateAction;
1920
use Sylius\Bundle\GridBundle\Builder\Action\DeleteAction;
@@ -22,36 +23,47 @@
2223
use Sylius\Bundle\GridBundle\Builder\ActionGroup\BulkActionGroup;
2324
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
2425
use Sylius\Bundle\GridBundle\Builder\ActionGroup\MainActionGroup;
26+
use Sylius\Bundle\GridBundle\Builder\Field\EnumField;
2527
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
28+
use Sylius\Bundle\GridBundle\Builder\Filter\EnumFilter;
2629
use Sylius\Bundle\GridBundle\Builder\Filter\StringFilter;
2730
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
2831
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
29-
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
32+
use Sylius\Component\Grid\Attribute\AsGrid;
33+
use Symfony\Contracts\Translation\TranslatorInterface;
3034

31-
final class BookGrid extends AbstractGrid implements ResourceAwareGridInterface
35+
#[AsGrid(
36+
resourceClass: Book::class,
37+
name: 'app_book',
38+
)]
39+
final class BookGrid extends AbstractGrid
3240
{
33-
public static function getName(): string
41+
public function __construct(private readonly TranslatorInterface $translator)
3442
{
35-
return 'app_book';
3643
}
3744

38-
public function buildGrid(GridBuilderInterface $gridBuilder): void
45+
public function __invoke(GridBuilderInterface $gridBuilder): void
3946
{
4047
$gridBuilder
4148
->orderBy('title')
42-
->addFilter(
49+
->withFilters(
4350
StringFilter::create('search', ['title', 'authorName'])
4451
->setLabel('sylius.ui.search'),
52+
EnumFilter::create(name: 'category', enumClass: BookCategory::class, field: 'category')
53+
->addFormOption('choice_value', fn (?BookCategory $enum) => $enum?->value)
54+
->addFormOption('choice_label', fn (BookCategory $choice) => $choice->trans($this->translator))
55+
->setLabel('app.ui.category'),
4556
)
46-
->addField(
57+
->withFields(
4758
StringField::create('title')
4859
->setLabel('app.ui.title')
4960
->setSortable(true),
50-
)
51-
->addField(
5261
StringField::create('authorName')
5362
->setLabel('app.ui.author_name')
5463
->setSortable(true),
64+
EnumField::create('category')
65+
->setLabel('app.ui.category')
66+
->setSortable(true),
5567
)
5668
->addActionGroup(
5769
MainActionGroup::create(
@@ -78,9 +90,4 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
7890
)
7991
;
8092
}
81-
82-
public function getResourceClass(): string
83-
{
84-
return Book::class;
85-
}
8693
}

0 commit comments

Comments
 (0)