Skip to content

Commit c0d378a

Browse files
committedJan 27, 2025
[Docs] add grid callable field doc
1 parent 16d6815 commit c0d378a

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
 

‎docs/grid/field_types.md

+113
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,116 @@ $field->setOptions([
311311
```
312312
{% endcode %}
313313
{% endhint %}
314+
315+
## Callable
316+
317+
The Callable column aims to offer almost as much flexibility as the Twig column, but without requiring the creation of a template.
318+
You simply need to specify a callable, which allows you to transform the 'data' variable on the fly.
319+
320+
When defining callables in YAML, only string representations of callables are supported.
321+
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).
322+
By contrast, when configuring grids with service definitions, you can use both callables and closures.
323+
324+
Here are some examples of what you can do:
325+
326+
{% tabs %}
327+
{% tab title="YAML" %}
328+
{% code title="config/packages/sylius_grid.yaml" lineNumbers="true" %}
329+
```yaml
330+
sylius_grid:
331+
grids:
332+
app_user:
333+
fields:
334+
id:
335+
type: callable
336+
options:
337+
callable: "callable:App\\Helper\\GridHelper::addHashPrefix"
338+
label: app.ui.id
339+
name:
340+
type: callable
341+
options:
342+
callable: "callable:strtoupper"
343+
label: app.ui.name
344+
```
345+
{% endcode %}
346+
{% endtab %}
347+
{% tab title="PHP" %}
348+
{% code title="config/packages/sylius_grid.php" lineNumbers="true" %}
349+
```php
350+
<?php
351+
352+
use Sylius\Bundle\GridBundle\Builder\Field\CallableField;
353+
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
354+
use Sylius\Bundle\GridBundle\Config\GridConfig;
355+
356+
return static function (GridConfig $grid): void {
357+
$grid->addGrid(GridBuilder::create('app_user', '%app.model.user.class%')
358+
->addField(
359+
CallableField::create('id', 'App\\Helper\\GridHelper::addHashPrefix')
360+
->setLabel('app.ui.id')
361+
)
362+
// or
363+
->addField(
364+
CallableField::create('id', ['App\\Helper\\GridHelper', 'addHashPrefix'])
365+
->setLabel('app.ui.id')
366+
)
367+
368+
->addField(
369+
CallableField::create('name', 'strtoupper')
370+
->setLabel('app.ui.name')
371+
)
372+
)
373+
};
374+
```
375+
{% endcode %}
376+
377+
OR
378+
379+
{% code title="src/Grid/UserGrid.php" lineNumbers="true" %}
380+
```php
381+
<?php
382+
383+
declare(strict_types=1);
384+
385+
namespace App\Grid;
386+
387+
use App\Entity\User;
388+
use Sylius\Bundle\GridBundle\Builder\Field\CallableField;
389+
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
390+
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
391+
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
392+
393+
final class UserGrid extends AbstractGrid implements ResourceAwareGridInterface
394+
{
395+
public static function getName(): string
396+
{
397+
return 'app_user';
398+
}
399+
400+
public function buildGrid(GridBuilderInterface $gridBuilder): void
401+
{
402+
$gridBuilder
403+
->addField(
404+
CallableField::create('id', GridHelper::addHashPrefix(...))
405+
->setLabel('app.ui.id')
406+
)
407+
->addField(
408+
CallableField::create('name', 'strtoupper')
409+
->setLabel('app.ui.name')
410+
)
411+
->addField(
412+
CallableField::create('roles' fn (array $roles): string => implode(', ', $roles))
413+
->setLabel('app.ui.roles')
414+
)
415+
;
416+
}
417+
418+
public function getResourceClass(): string
419+
{
420+
return User::class;
421+
}
422+
}
423+
```
424+
{% endcode %}
425+
{% endtab %}
426+
{% endtabs %}

0 commit comments

Comments
 (0)