From 013f49b1d902d80538059bb370d7f0457e224b14 Mon Sep 17 00:00:00 2001 From: Florian Merle Date: Mon, 27 Jan 2025 11:51:32 +0100 Subject: [PATCH 1/2] [Docs] add grid callable field doc --- docs/grid/field_types.md | 113 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/docs/grid/field_types.md b/docs/grid/field_types.md index 9e9b1622..0c9ed0b1 100644 --- a/docs/grid/field_types.md +++ b/docs/grid/field_types.md @@ -311,3 +311,116 @@ $field->setOptions([ ``` {% endcode %} {% endhint %} + +## Callable + +The Callable column aims to offer almost as much flexibility as the Twig column, but without requiring the creation of a template. +You simply need to specify a callable, which allows you to transform the `data` variable on the fly. + +When defining callables in YAML, only string representations of callables are supported. +When configuring grids using PHP (as opposed to service grid configuration), both string and array callables are supported. However, closures cannot be used due to restrictions in Symfony's configuration (values of type "Closure" are not permitted in service configuration files). +By contrast, when configuring grids with service definitions, you can use both callables and closures. + +Here are some examples of what you can do: + +{% tabs %} +{% tab title="YAML" %} +{% code title="config/packages/sylius_grid.yaml" lineNumbers="true" %} +```yaml +sylius_grid: + grids: + app_user: + fields: + id: + type: callable + options: + callable: "callable:App\\Helper\\GridHelper::addHashPrefix" + label: app.ui.id + name: + type: callable + options: + callable: "callable:strtoupper" + label: app.ui.name +``` +{% endcode %} +{% endtab %} +{% tab title="PHP" %} +{% code title="config/packages/sylius_grid.php" lineNumbers="true" %} +```php +addGrid(GridBuilder::create('app_user', '%app.model.user.class%') + ->addField( + CallableField::create('id', 'App\\Helper\\GridHelper::addHashPrefix') + ->setLabel('app.ui.id') + ) + // or + ->addField( + CallableField::create('id', ['App\\Helper\\GridHelper', 'addHashPrefix']) + ->setLabel('app.ui.id') + ) + + ->addField( + CallableField::create('name', 'strtoupper') + ->setLabel('app.ui.name') + ) + ) +}; +``` +{% endcode %} + +OR + +{% code title="src/Grid/UserGrid.php" lineNumbers="true" %} +```php +addField( + CallableField::create('id', GridHelper::addHashPrefix(...)) + ->setLabel('app.ui.id') + ) + ->addField( + CallableField::create('name', 'strtoupper') + ->setLabel('app.ui.name') + ) + ->addField( + CallableField::create('roles' fn (array $roles): string => implode(', ', $roles)) + ->setLabel('app.ui.roles') + ) + ; + } + + public function getResourceClass(): string + { + return User::class; + } +} +``` +{% endcode %} +{% endtab %} +{% endtabs %} From a6ee6012d09aeecdbd2852c9ecc6534a1ee96a86 Mon Sep 17 00:00:00 2001 From: Florian Merle Date: Fri, 7 Mar 2025 11:19:02 +0100 Subject: [PATCH 2/2] [Docs] add grid callable service field doc --- docs/grid/field_types.md | 81 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/docs/grid/field_types.md b/docs/grid/field_types.md index 0c9ed0b1..1c74ca6b 100644 --- a/docs/grid/field_types.md +++ b/docs/grid/field_types.md @@ -315,8 +315,13 @@ $field->setOptions([ ## Callable The Callable column aims to offer almost as much flexibility as the Twig column, but without requiring the creation of a template. + You simply need to specify a callable, which allows you to transform the `data` variable on the fly. +This field type has can be configured in two differents ways. Either you define a callable using the `callable` options or you can define a service with the `service` option. + +### Using `callable` option + When defining callables in YAML, only string representations of callables are supported. When configuring grids using PHP (as opposed to service grid configuration), both string and array callables are supported. However, closures cannot be used due to restrictions in Symfony's configuration (values of type "Closure" are not permitted in service configuration files). By contrast, when configuring grids with service definitions, you can use both callables and closures. @@ -424,3 +429,79 @@ final class UserGrid extends AbstractGrid implements ResourceAwareGridInterface {% endcode %} {% endtab %} {% endtabs %} + +### Using `service` option + +For more complex tasks, the callable option may not be the most suited choice, especially when accessing a service is required to transform data. In such cases, you can use the `service` option to define a callable service. + +If the service itself is callable, specifying the `service` option is sufficient. Alternatively, you can define a specific method within the service by using the `method` option. + +Internally, Sylius uses a tagged locator to provide these services. To use a custom service, it must be tagged with `sylius.grid_field_callable_service`. As an alternative, you can use the `AsGridFieldCallableService` attribute on your service, which will automatically apply the required tag. + +Before diving in examples, let's create a service that uses both a `PriceHelper` and `ChannelContextInterface` to retrieve the price of a product variant. + +{% code title="src/Helper/ProductVariantHelper.php" lineNumbers="true" %} +```php +priceHelper->getPrice($variant, [ + 'channel' => $this->channelContext->getChannel(), + ]); + } +} +``` +{% endcode %} + +{% tabs %} +{% tab title="YAML" %} +{% code title="config/packages/sylius_grid.yaml" lineNumbers="true" %} +```yaml +sylius_grid: + grids: + app_product_variant: + fields: + price: + type: callable + path: . + options: + service: "App\Helper\ProductVariantHelper" + method: 'getPrice' +``` +{% endcode %} +{% endtab %} +{% tab title="PHP" %} +{% code title="config/packages/sylius_grid.php" lineNumbers="true" %} +```php +addGrid(GridBuilder::create('app_product_variant', '%app.model.product_variant.class%') + ->addField( + CallableField::createForService('price', \App\Helper\ProductVariantHelper, 'getPrice') + ->setPath('.') + ) + ) +}; +``` +{% endcode %} +{% endtab %} +{% endtabs %}