From a72983d93db0aea73c471e3dfb1e5eb4a0394467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 19 Dec 2023 14:10:16 +0100 Subject: [PATCH 1/4] Add resource metadata on twig context --- docs/configure_your_operations.md | 135 +++++++++--------- docs/configure_your_resource.md | 40 +++--- .../Context/Factory/DefaultContextFactory.php | 5 +- .../Factory/DefaultContextFactorySpec.php | 8 +- 4 files changed, 104 insertions(+), 84 deletions(-) diff --git a/docs/configure_your_operations.md b/docs/configure_your_operations.md index ed5576a54..2464b383d 100644 --- a/docs/configure_your_operations.md +++ b/docs/configure_your_operations.md @@ -3,24 +3,26 @@ Read the previous chapter to [configure your resource](configure_your_resource.md). Now, with your fresh new resource, you have to define the operations that you need to implement. -There are some basic CRUD operations and more. +There are some basic CRUD operations and more. + * [Basic operations](#basic-operations) - * [Index operation](#index-operation) - * [Use a grid for your index operation](#use-a-grid-for-your-index-operation) - * [Create operation](#create-operation) - * [Update operation](#update-operation) - * [Delete operation](#delete-operation) - * [State machine operation](#state-machine-operation) + * [Index operation](#index-operation) + * [Use a grid for your index operation](#use-a-grid-for-your-index-operation) + * [Create operation](#create-operation) + * [Update operation](#update-operation) + * [Delete operation](#delete-operation) + * [State machine operation](#state-machine-operation) * [Advanced configuration](#advanced-configuration) - * [Configure the path](#configure-the-path) - * [Configure the short name](#configure-the-short-name) - * [Configure the templates' dir](#configure-the-templates-dir) - * [Configure the section](#configure-the-section) - * [Configure the routes' prefix](#configure-the-routes-prefix) - * [Configure the resource identifier](#configure-the-resource-identifier) - * [Configure the vars](#configure-the-vars) + * [Configure the path](#configure-the-path) + * [Configure the short name](#configure-the-short-name) + * [Configure the templates' dir](#configure-the-templates-dir) + * [Configure the section](#configure-the-section) + * [Configure the routes' prefix](#configure-the-routes-prefix) + * [Configure the resource identifier](#configure-the-resource-identifier) + * [Configure the vars](#configure-the-vars) + ## Basic operations @@ -46,22 +48,24 @@ class Book implements ResourceInterface It will configure this route for your `index` operation. -| Name | Method | Path | -|-----------------------|-----------------|---------| -| app_book_index | GET | /books | +| Name | Method | Path | +|----------------|--------|--------| +| app_book_index | GET | /books | On your Twig template, these variables are available -| Name | Type | -|-----------|------------------------------------------| -| resources | Pagerfanta\Pagerfanta | -| books | Pagerfanta\Pagerfanta | -| operation | Sylius\Component\Resource\Metadata\Index | -| app | Symfony\Bridge\Twig\AppVariable | +| Name | Type | +|-------------------|-------------------------------------------| +| resources | Pagerfanta\Pagerfanta | +| books | Pagerfanta\Pagerfanta | +| operation | Sylius\Resource\Metadata\Index | +| resource_metadata | Sylius\Resource\Metadata\ResourceMetadata | +| app | Symfony\Bridge\Twig\AppVariable | ### Use a grid for your index operation -To use a grid for you operation, you need to install the [Sylius grid package](https://github.com/Sylius/SyliusGridBundle/) +To use a grid for you operation, you need to install +the [Sylius grid package](https://github.com/Sylius/SyliusGridBundle/) ```php namespace App\Entity; @@ -84,12 +88,13 @@ class Book implements ResourceInterface On your Twig template, these variables are available -| Name | Type | -|-----------|---------------------------------------------------------| -| resources | Sylius\Bundle\ResourceBundle\Grid\View\ResourceGridView | -| books | Sylius\Bundle\ResourceBundle\Grid\View\ResourceGridView | -| operation | Sylius\Component\Resource\Metadata\Index | -| app | Symfony\Bridge\Twig\AppVariable | +| Name | Type | +|-------------------|---------------------------------------------------------| +| resources | Sylius\Bundle\ResourceBundle\Grid\View\ResourceGridView | +| books | Sylius\Bundle\ResourceBundle\Grid\View\ResourceGridView | +| operation | Sylius\Resource\Metadata\Index | +| resource_metadata | Sylius\Resource\Metadata\ResourceMetadata | +| app | Symfony\Bridge\Twig\AppVariable | The iterator for your books will be available as `books.data` or `resources.data`. @@ -120,12 +125,13 @@ It will configure this route for your `create` operation. On your Twig template, these variables are available -| Name | Type | -|-----------|-------------------------------------------| -| resource | App\Entity\Book | -| book | App\Entity\Book | -| operation | Sylius\Component\Resource\Metadata\Create | -| app | Symfony\Bridge\Twig\AppVariable | +| Name | Type | +|-------------------|-------------------------------------------| +| resource | App\Entity\Book | +| book | App\Entity\Book | +| operation | Sylius\Resource\Metadata\Create | +| resource_metadata | Sylius\Resource\Metadata\ResourceMetadata | +| app | Symfony\Bridge\Twig\AppVariable | The iterator for your books will be available as `books.data` or `resources.data`. @@ -156,12 +162,13 @@ It will configure this route for your `update` operation. On your Twig template, these variables are available -| Name | Type | -|-----------|-------------------------------------------| -| resource | App\Entity\Book | -| book | App\Entity\Book | -| operation | Sylius\Component\Resource\Metadata\Update | -| app | Symfony\Bridge\Twig\AppVariable | +| Name | Type | +|-------------------|-------------------------------------------| +| resource | App\Entity\Book | +| book | App\Entity\Book | +| operation | Sylius\Resource\Metadata\Update | +| resource_metadata | Sylius\Resource\Metadata\ResourceMetadata | +| app | Symfony\Bridge\Twig\AppVariable | ### Delete operation @@ -234,22 +241,22 @@ class Book implements ResourceInterface It will configure this route for your `show` operation. -| Name | Method | Path | -|-----------------|--------|-------------| -| app_book_show | GET | /books/{id} | +| Name | Method | Path | +|---------------|--------|-------------| +| app_book_show | GET | /books/{id} | On your Twig template, these variables are available -| Name | Type | -|-----------|-----------------------------------------| -| resource | App\Entity\Book | -| book | App\Entity\Book | -| operation | Sylius\Component\Resource\Metadata\Show | -| app | Symfony\Bridge\Twig\AppVariable | +| Name | Type | +|-------------------|-------------------------------------------| +| resource | App\Entity\Book | +| book | App\Entity\Book | +| operation | Sylius\Resource\Metadata\Show | +| resource_metadata | Sylius\Resource\Metadata\ResourceMetadata | +| app | Symfony\Bridge\Twig\AppVariable | ### State machine operation - `State machine` operation allows to apply a transition to an item of your resource. As an example, we add a `publish` operation to our book resource. @@ -271,9 +278,9 @@ class Book implements ResourceInterface It will configure this route for your `apply_state_machine_transition` operation. -| Name | Method | Path | -|-------------------|--------|---------------------| -| app_book_publish | GET | /books/{id}/publish | +| Name | Method | Path | +|------------------|--------|---------------------| +| app_book_publish | GET | /books/{id}/publish | ## Advanced configuration @@ -388,14 +395,14 @@ class Book implements ResourceInterface ``` -| Name | Method | Path | -|------------------------|-----------------|--------------------------| -| app_book_index | GET | /admin/books/ | -| app_book_create | GET, POST | /admin/books/new | -| app_book_update | GET, PUT, PATCH | /admin/books/{id}/edit | -| app_book_delete | DELETE | /admin/books/{id} | -| app_book_bulk_delete | DELETE | /admin/books/bulk_delete | -| app_book_show | GET | /admin/books/{id} | +| Name | Method | Path | +|----------------------|-----------------|--------------------------| +| app_book_index | GET | /admin/books/ | +| app_book_create | GET, POST | /admin/books/new | +| app_book_update | GET, PUT, PATCH | /admin/books/{id}/edit | +| app_book_delete | DELETE | /admin/books/{id} | +| app_book_bulk_delete | DELETE | /admin/books/bulk_delete | +| app_book_show | GET | /admin/books/{id} | ### Configure the section @@ -469,7 +476,7 @@ class Book implements ResourceInterface | Name | Method | Path | |----------------------|-----------------|--------------------------| | app_book_index | GET | /admin/books/ | -| app_ook_create | GET, POST | /admin/books/new | +| app_book_create | GET, POST | /admin/books/new | | app_book_update | GET, PUT, PATCH | /admin/books/{code}/edit | | app_book_delete | DELETE | /admin/books/{code} | | app_book_bulk_delete | DELETE | /admin/books/bulk_delete | diff --git a/docs/configure_your_resource.md b/docs/configure_your_resource.md index d164e2e6c..a23a0b0e8 100644 --- a/docs/configure_your_resource.md +++ b/docs/configure_your_resource.md @@ -3,17 +3,20 @@ Read the previous chapter to [create a new resource](create_new_resource.md). + * [Implements the Resource interface](#implements-the-resource-interface) * [Use the Resource attribute](#use-the-resource-attribute) * [Advanced configuration](#advanced-configuration) - * [Configure the resource name](#configure-the-resource-name) - * [Configure the resource plural name](#configure-the-resource-plural-name) - * [Configure the resource vars](#configure-the-resource-vars) + * [Configure the resource name](#configure-the-resource-name) + * [Configure the resource plural name](#configure-the-resource-plural-name) + * [Configure the resource vars](#configure-the-resource-vars) + ## Implements the Resource interface -To declare your resource as a Sylius one, you need to implement the ```Sylius\Component\Resource\Model\ResourceInterface``` which requires you to implement a `getId()` method. +To declare your resource as a Sylius one, you need to implement +the ```Sylius\Component\Resource\Model\ResourceInterface``` which requires you to implement a `getId()` method. ```php // src/Entity/Book.php @@ -64,7 +67,8 @@ $ bin/console sylius:debug:resource 'App\Entity\book' +--------------------+------------------------------------------------------------+ ``` -By default, it will have the `app.book` alias in Sylius resource which is a concatenation of the application name and the resource name `{application}.{name}`. +By default, it will have the `app.book` alias in Sylius resource which is a concatenation of the application name and +the resource name `{application}.{name}`. ## Advanced configuration @@ -89,12 +93,13 @@ On your Twig templates, the `order` variable will be replaced by the `cart` one. As an example, on a `show` operation following Twig variables will be available: -| Name | Type | -|-----------|-----------------------------------------| -| resource | App\Entity\Order | -| cart | App\Entity\Order | -| operation | Sylius\Component\Resource\Metadata\Show | -| app | Symfony\Bridge\Twig\AppVariable | +| Name | Type | +|-------------------|-------------------------------------------| +| resource | App\Entity\Order | +| cart | App\Entity\Order | +| operation | Sylius\Resource\Metadata\Show | +| resource_metadata | Sylius\Resource\Metadata\ResourceMetadata | +| app | Symfony\Bridge\Twig\AppVariable | ### Configure the resource plural name @@ -117,12 +122,13 @@ On your Twig templates, the `books` variable will be replaced by the `library` o As an example, on an `index` operation these Twig variables will be available: -| Name | Type | -|-----------|------------------------------------------| -| resource | App\Entity\Book | -| library | App\Entity\Book | -| operation | Sylius\Component\Resource\Metadata\Index | -| app | Symfony\Bridge\Twig\AppVariable | +| Name | Type | +|-------------------|-------------------------------------------| +| resource | App\Entity\Book | +| library | App\Entity\Book | +| operation | Sylius\Resource\Metadata\Index | +| resource_metadata | Sylius\Resource\Metadata\ResourceMetadata | +| app | Symfony\Bridge\Twig\AppVariable | ### Configure the resource vars diff --git a/src/Component/src/Twig/Context/Factory/DefaultContextFactory.php b/src/Component/src/Twig/Context/Factory/DefaultContextFactory.php index b6d0bdb3b..a5eb9dbd7 100644 --- a/src/Component/src/Twig/Context/Factory/DefaultContextFactory.php +++ b/src/Component/src/Twig/Context/Factory/DefaultContextFactory.php @@ -21,7 +21,10 @@ final class DefaultContextFactory implements ContextFactoryInterface { public function create(mixed $data, Operation $operation, Context $context): array { - $twigContext = ['operation' => $operation]; + $twigContext = [ + 'operation' => $operation, + 'resource_metadata' => $operation->getResource(), + ]; if ($operation instanceof CollectionOperationInterface) { $twigContext['resources'] = $data; diff --git a/src/Component/tests/spec/Twig/Context/Factory/DefaultContextFactorySpec.php b/src/Component/tests/spec/Twig/Context/Factory/DefaultContextFactorySpec.php index ed183d366..5c30d6756 100644 --- a/src/Component/tests/spec/Twig/Context/Factory/DefaultContextFactorySpec.php +++ b/src/Component/tests/spec/Twig/Context/Factory/DefaultContextFactorySpec.php @@ -30,10 +30,12 @@ function it_is_initializable(): void function it_creates_twig_context_for_resource( \stdClass $data, ): void { - $operation = (new Show())->withResource(new ResourceMetadata(alias: 'app.dummy', name: 'dummy')); + $resourceMetadata = new ResourceMetadata(alias: 'app.dummy', name: 'dummy'); + $operation = (new Show())->withResource($resourceMetadata); $this->create($data, $operation, new Context())->shouldReturn([ 'operation' => $operation, + 'resource_metadata' => $resourceMetadata, 'resource' => $data, 'dummy' => $data, ]); @@ -42,10 +44,12 @@ function it_creates_twig_context_for_resource( function it_creates_twig_context_for_resource_collection( \stdClass $data, ): void { - $operation = (new Index())->withResource(new ResourceMetadata(alias: 'app.dummy', pluralName: 'dummies')); + $resourceMetadata = new ResourceMetadata(alias: 'app.dummy', pluralName: 'dummies'); + $operation = (new Index())->withResource($resourceMetadata); $this->create($data, $operation, new Context())->shouldReturn([ 'operation' => $operation, + 'resource_metadata' => $resourceMetadata, 'resources' => $data, 'dummies' => $data, ]); From 99d18ab6f2a5c0921b249d93b136b2e6cbf43cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 19 Dec 2023 14:11:36 +0100 Subject: [PATCH 2/4] Update table of contents --- docs/configure_your_operations.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/configure_your_operations.md b/docs/configure_your_operations.md index 2464b383d..49ecaf368 100644 --- a/docs/configure_your_operations.md +++ b/docs/configure_your_operations.md @@ -6,23 +6,24 @@ Now, with your fresh new resource, you have to define the operations that you ne There are some basic CRUD operations and more. - -* [Basic operations](#basic-operations) +* [Configure your operations](#configure-your-operations) + * [Basic operations](#basic-operations) * [Index operation](#index-operation) * [Use a grid for your index operation](#use-a-grid-for-your-index-operation) * [Create operation](#create-operation) * [Update operation](#update-operation) * [Delete operation](#delete-operation) + * [Bulk delete operation](#bulk-delete-operation) + * [Show operation](#show-operation) * [State machine operation](#state-machine-operation) -* [Advanced configuration](#advanced-configuration) + * [Advanced configuration](#advanced-configuration) * [Configure the path](#configure-the-path) * [Configure the short name](#configure-the-short-name) * [Configure the templates' dir](#configure-the-templates-dir) - * [Configure the section](#configure-the-section) * [Configure the routes' prefix](#configure-the-routes-prefix) + * [Configure the section](#configure-the-section) * [Configure the resource identifier](#configure-the-resource-identifier) * [Configure the vars](#configure-the-vars) - ## Basic operations From 92d348acc1864527271c2a54abc880eef498ea15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 19 Dec 2023 14:12:56 +0100 Subject: [PATCH 3/4] Update another table of contents --- docs/configure_your_resource.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/configure_your_resource.md b/docs/configure_your_resource.md index a23a0b0e8..6734b8a60 100644 --- a/docs/configure_your_resource.md +++ b/docs/configure_your_resource.md @@ -3,14 +3,13 @@ Read the previous chapter to [create a new resource](create_new_resource.md). - -* [Implements the Resource interface](#implements-the-resource-interface) -* [Use the Resource attribute](#use-the-resource-attribute) -* [Advanced configuration](#advanced-configuration) +* [Configure your resource](#configure-your-resource) + * [Implements the Resource interface](#implements-the-resource-interface) + * [Use the Resource attribute](#use-the-resource-attribute) + * [Advanced configuration](#advanced-configuration) * [Configure the resource name](#configure-the-resource-name) * [Configure the resource plural name](#configure-the-resource-plural-name) * [Configure the resource vars](#configure-the-resource-vars) - ## Implements the Resource interface From 9f13fba38c1c5e6b9c9a843f07ae4b4852a00e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Thu, 21 Dec 2023 11:47:54 +0100 Subject: [PATCH 4/4] Fix twig vars --- docs/configure_your_operations.md | 74 +++++++++++++++---------------- docs/configure_your_resource.md | 4 +- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/docs/configure_your_operations.md b/docs/configure_your_operations.md index 49ecaf368..212af8f43 100644 --- a/docs/configure_your_operations.md +++ b/docs/configure_your_operations.md @@ -10,11 +10,11 @@ There are some basic CRUD operations and more. * [Basic operations](#basic-operations) * [Index operation](#index-operation) * [Use a grid for your index operation](#use-a-grid-for-your-index-operation) + * [Show operation](#show-operation) * [Create operation](#create-operation) * [Update operation](#update-operation) * [Delete operation](#delete-operation) * [Bulk delete operation](#bulk-delete-operation) - * [Show operation](#show-operation) * [State machine operation](#state-machine-operation) * [Advanced configuration](#advanced-configuration) * [Configure the path](#configure-the-path) @@ -99,6 +99,41 @@ On your Twig template, these variables are available The iterator for your books will be available as `books.data` or `resources.data`. +### Show operation + +`Show` operation allows to view details of an item. + +```php +namespace App\Entity; + +use Sylius\Resource\Model\ResourceInterface; +use Sylius\Resource\Metadata\AsResource; +use Sylius\Resource\Metadata\Show; + +#[AsResource] +#[Show] +class Book implements ResourceInterface +{ +} + +``` + +It will configure this route for your `show` operation. + +| Name | Method | Path | +|---------------|--------|-------------| +| app_book_show | GET | /books/{id} | + +On your Twig template, these variables are available + +| Name | Type | +|-------------------|-------------------------------------------| +| resource | App\Entity\Book | +| book | App\Entity\Book | +| operation | Sylius\Resource\Metadata\Show | +| resource_metadata | Sylius\Resource\Metadata\ResourceMetadata | +| app | Symfony\Bridge\Twig\AppVariable | + ### Create operation `Create` operation allows to add a new item of your resource. @@ -219,42 +254,7 @@ It will configure this route for your `bulk_delete` operation. | Name | Method | Path | |----------------------|--------|--------------------| -| app_book_bulk_delete | DELETE | /books/bulk_delete | - -### Show operation - -`Show` operation allows to view details of an item. - -```php -namespace App\Entity; - -use Sylius\Resource\Model\ResourceInterface; -use Sylius\Resource\Metadata\AsResource; -use Sylius\Resource\Metadata\Show; - -#[AsResource] -#[Show] -class Book implements ResourceInterface -{ -} - -``` - -It will configure this route for your `show` operation. - -| Name | Method | Path | -|---------------|--------|-------------| -| app_book_show | GET | /books/{id} | - -On your Twig template, these variables are available - -| Name | Type | -|-------------------|-------------------------------------------| -| resource | App\Entity\Book | -| book | App\Entity\Book | -| operation | Sylius\Resource\Metadata\Show | -| resource_metadata | Sylius\Resource\Metadata\ResourceMetadata | -| app | Symfony\Bridge\Twig\AppVariable | +| app_book_bulk_delete | DELETE | /books/bulk_delete | ### State machine operation diff --git a/docs/configure_your_resource.md b/docs/configure_your_resource.md index 6734b8a60..db3e2eb87 100644 --- a/docs/configure_your_resource.md +++ b/docs/configure_your_resource.md @@ -123,8 +123,8 @@ As an example, on an `index` operation these Twig variables will be available: | Name | Type | |-------------------|-------------------------------------------| -| resource | App\Entity\Book | -| library | App\Entity\Book | +| resources | Pagerfanta\Pagerfanta | +| library | APagerfanta\Pagerfanta | | operation | Sylius\Resource\Metadata\Index | | resource_metadata | Sylius\Resource\Metadata\ResourceMetadata | | app | Symfony\Bridge\Twig\AppVariable |