⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace App\Entity;

use App\Enum\BookCategory;
use App\Form\BookType;
use App\Grid\BookGrid;
use App\Repository\BookRepository;
use App\Responder\ExportGridToCsvResponder;
Expand All @@ -31,6 +33,7 @@
#[ORM\Entity(repositoryClass: BookRepository::class)]
#[AsResource(
section: 'admin',
formType: BookType::class,
templatesDir: '@SyliusAdminUi/crud',
routePrefix: '/admin',
operations: [
Expand Down Expand Up @@ -79,6 +82,9 @@ class Book implements ResourceInterface
#[NotBlank]
private ?string $authorName = null;

#[ORM\Column(type: 'enum', length: 255, nullable: true)]
private ?BookCategory $category = null;

#[ORM\Column(type: 'datetime_immutable')]
private \DateTimeImmutable $createdAt;

Expand Down Expand Up @@ -121,4 +127,14 @@ public function setCreatedAt(\DateTimeImmutable $createdAt): void
{
$this->createdAt = $createdAt;
}

public function getCategory(): ?BookCategory
{
return $this->category;
}

public function setCategory(?BookCategory $category): void
{
$this->category = $category;
}
}
42 changes: 42 additions & 0 deletions app/Enum/BookCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\Enum;

enum BookCategory: string
{
case FICTION = 'fiction';
case NON_FICTION = 'non-fiction';
case MYSTERY = 'mystery';
case THRILLER = 'thriller';
case SCIENCE_FICTION = 'science-fiction';
case FANTASY = 'fantasy';
case ROMANCE = 'romance';
case HORROR = 'horror';
case BIOGRAPHY = 'biography';
case HISTORY = 'history';
case SCIENCE = 'science';
case TECHNOLOGY = 'technology';
case BUSINESS = 'business';
case COOKING = 'cooking';
case TRAVEL = 'travel';
case POETRY = 'poetry';
case DRAMA = 'drama';
case CHILDREN = 'children';
case YOUNG_ADULT = 'young adult';
case RELIGION = 'religion';
case PHILOSOPHY = 'philosophy';
case ART = 'art';
case MANGA = 'manga';
case COMICS = 'comics';
}
7 changes: 7 additions & 0 deletions app/Factory/BookFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace App\Factory;

use App\Entity\Book;
use App\Enum\BookCategory;
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;

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

public function withCategory(BookCategory $category): self
{
return $this->with(['category' => $category]);
}

protected function defaults(): array|callable
{
return [
'title' => ucfirst(self::faker()->words(3, true)),
'authorName' => self::faker()->firstName() . ' ' . self::faker()->lastName(),
'category' => self::faker()->randomElement(BookCategory::cases()),
];
}
}
50 changes: 50 additions & 0 deletions app/Form/BookType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\Form;

use App\Entity\Book;
use App\Enum\BookCategory;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BookType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title')
->add('authorName')
->add('category', EnumType::class, [
'class' => BookCategory::class,
'choice_value' => fn (?BookCategory $enum) => $enum?->value,
'choice_label' => fn (BookCategory $choice) => ucfirst($choice->value),
'required' => false,
])
;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Book::class,
]);
}

public function getBlockPrefix(): string
{
return 'sylius_resource';
}
}
36 changes: 19 additions & 17 deletions app/Grid/BookGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace App\Grid;

use App\Entity\Book;
use App\Enum\BookCategory;
use Sylius\Bundle\GridBundle\Builder\Action\Action;
use Sylius\Bundle\GridBundle\Builder\Action\CreateAction;
use Sylius\Bundle\GridBundle\Builder\Action\DeleteAction;
Expand All @@ -22,36 +23,42 @@
use Sylius\Bundle\GridBundle\Builder\ActionGroup\BulkActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\MainActionGroup;
use Sylius\Bundle\GridBundle\Builder\Field\EnumField;
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
use Sylius\Bundle\GridBundle\Builder\Filter\EnumFilter;
use Sylius\Bundle\GridBundle\Builder\Filter\StringFilter;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
use Sylius\Component\Grid\Attribute\AsGrid;

final class BookGrid extends AbstractGrid implements ResourceAwareGridInterface
#[AsGrid(
resourceClass: Book::class,
name: 'app_book',
)]
final class BookGrid extends AbstractGrid
{
public static function getName(): string
{
return 'app_book';
}

public function buildGrid(GridBuilderInterface $gridBuilder): void
public function __invoke(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->orderBy('title')
->addFilter(
->withFilters(
StringFilter::create('search', ['title', 'authorName'])
->setLabel('sylius.ui.search'),
EnumFilter::create(name: 'category', enumClass: BookCategory::class, field: 'category')
->addFormOption('choice_value', fn (?BookCategory $enum) => $enum?->value)
->addFormOption('choice_label', fn (BookCategory $choice) => ucfirst($choice->value))
->setLabel('app.ui.category'),
)
->addField(
->withFields(
StringField::create('title')
->setLabel('app.ui.title')
->setSortable(true),
)
->addField(
StringField::create('authorName')
->setLabel('app.ui.author_name')
->setSortable(true),
EnumField::create('category')
->setLabel('app.ui.category')
->setSortable(true),
)
->addActionGroup(
MainActionGroup::create(
Expand All @@ -78,9 +85,4 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
)
;
}

public function getResourceClass(): string
{
return Book::class;
}
}
7 changes: 4 additions & 3 deletions config/packages/sylius_resource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ sylius_resource:

# Configure your resources
resources:
#app.book:
#classes:
#model: App\Entity\Book
app.book:
classes:
model: App\Entity\Book
form: App\Form\BookType
Loading
Loading