Skip to content

Commit 6ce7b95

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

File tree

14 files changed

+728
-628
lines changed

14 files changed

+728
-628
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
enum BookCategory: string
17+
{
18+
case FICTION = 'fiction';
19+
case NON_FICTION = 'non-fiction';
20+
case MYSTERY = 'mystery';
21+
case THRILLER = 'thriller';
22+
case SCIENCE_FICTION = 'science-fiction';
23+
case FANTASY = 'fantasy';
24+
case ROMANCE = 'romance';
25+
case HORROR = 'horror';
26+
case BIOGRAPHY = 'biography';
27+
case HISTORY = 'history';
28+
case SCIENCE = 'science';
29+
case TECHNOLOGY = 'technology';
30+
case BUSINESS = 'business';
31+
case COOKING = 'cooking';
32+
case TRAVEL = 'travel';
33+
case POETRY = 'poetry';
34+
case DRAMA = 'drama';
35+
case CHILDREN = 'children';
36+
case YOUNG_ADULT = 'young adult';
37+
case RELIGION = 'religion';
38+
case PHILOSOPHY = 'philosophy';
39+
case ART = 'art';
40+
case MANGA = 'manga';
41+
case COMICS = 'comics';
42+
}

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: 13 additions & 1 deletion
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,7 +23,9 @@
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;
@@ -39,9 +42,13 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
3942
{
4043
$gridBuilder
4144
->orderBy('title')
42-
->addFilter(
45+
->withFilters(
4346
StringFilter::create('search', ['title', 'authorName'])
4447
->setLabel('sylius.ui.search'),
48+
EnumFilter::create(name: 'category', enumClass: BookCategory::class, field: 'category')
49+
->addFormOption(option: 'choice_value', value: fn (?BookCategory $enum) => $enum?->value)
50+
->addFormOption(option: 'choice_label', value: fn (BookCategory $enum) => ucfirst(str_replace(search: '_', replace: ' ', subject: $enum->value)))
51+
->setLabel('app.ui.category'),
4552
)
4653
->addField(
4754
StringField::create('title')
@@ -53,6 +60,11 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
5360
->setLabel('app.ui.author_name')
5461
->setSortable(true),
5562
)
63+
->addField(
64+
EnumField::create('category')
65+
->setLabel('app.ui.category')
66+
->setSortable(true),
67+
)
5668
->addActionGroup(
5769
MainActionGroup::create(
5870
CreateAction::create(),

0 commit comments

Comments
 (0)