Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resource metadata on Twig context #833

Merged
Merged
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
192 changes: 100 additions & 92 deletions docs/configure_your_operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
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.

<!-- TOC -->
* [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)
* [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 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)
* [Show operation](#show-operation)
* [Create operation](#create-operation)
* [Update operation](#update-operation)
* [Delete operation](#delete-operation)
* [Bulk delete operation](#bulk-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 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)
<!-- TOC -->

## Basic operations
Expand All @@ -46,22 +49,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
diimpp marked this conversation as resolved.
Show resolved Hide resolved
the [Sylius grid package](https://github.com/Sylius/SyliusGridBundle/)

```php
namespace App\Entity;
Expand All @@ -84,15 +89,51 @@ 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`.

### 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.
Expand Down Expand Up @@ -120,12 +161,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`.

Expand Down Expand Up @@ -156,12 +198,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

Expand Down Expand Up @@ -211,45 +254,10 @@ 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\Component\Resource\Metadata\Show |
| app | Symfony\Bridge\Twig\AppVariable |
| app_book_bulk_delete | DELETE | /books/bulk_delete |

### 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.
Expand All @@ -271,9 +279,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

Expand Down Expand Up @@ -388,14 +396,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

Expand Down Expand Up @@ -469,7 +477,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 |
Expand Down
45 changes: 25 additions & 20 deletions docs/configure_your_resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
Read the previous chapter to [create a new resource](create_new_resource.md).

<!-- TOC -->
* [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 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)
<!-- TOC -->

## 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
Expand Down Expand Up @@ -64,7 +66,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

Expand All @@ -89,12 +92,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

Expand All @@ -117,12 +121,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 |
|-------------------|-------------------------------------------|
| resources | Pagerfanta\Pagerfanta |
| library | APagerfanta\Pagerfanta |
| operation | Sylius\Resource\Metadata\Index |
| resource_metadata | Sylius\Resource\Metadata\ResourceMetadata |
| app | Symfony\Bridge\Twig\AppVariable |

### Configure the resource vars

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
Expand All @@ -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,
]);
Expand Down
Loading