Field types
This is the list of built-in field types.
String
The simplest column type, which displays the value at the specified path as plain text.
By default, it uses the name of the field, but you can specify a different path if needed. For example:
<?php
declare(strict_types=1);
namespace App\Grid;
use App\Entity\User;
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Component\Grid\Attribute\AsGrid;
#[AsGrid(
name: 'app_user',
resourceClass: User::class
)]
final class UserGrid extends AbstractGrid
{
public function __invoke(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->addField(
StringField::create('email')
->setLabel('app.ui.email') // # each field type can have a label, we suggest using translation keys instead of messages
->setPath('contactDetails.email')
)
;
}
}This configuration will display the value of $user->getContactDetails()->getEmail().
DateTime
This column type works exactly the same way as StringField, but expects a DateTime instance and outputs a formatted date and time string.
Available options:
format- defaults toY:m:d H:i:s, you can set it to any supported format (see https://www.php.net/manual/en/datetime.format.php)timezone- defaults to%sylius_grid.timezone%parameter, null if such a parameter does not exist, you can set it to any supported timezone (see https://www.php.net/manual/en/timezones.php)
If you want to call the setOptions function, you must pass both 'format' and 'timezone' as arguments again. Otherwise, they will be unset.
Twig
The Twig column type is the most flexible one, because it delegates the logic of rendering the value to the Twig templating engine. First, you must specify the template you want to render.
Then, within the template, you can render the field's value via the data variable.
Binding a Field to the Full Object Instance
To render more complex data in a grid field, you can bind the field to the root object by redefining the field path. This gives you access to all attributes of the underlying object when rendering the field.
This allows you to render multiple properties inside the same field Twig template.
If you want to call the setOptions function, you must pass 'template' as an argument again. Otherwise, it will be unset.
Last updated