Simplest column type, which basically renders the value at given path as a string.
By default, it uses the name of the field, but you can specify a different path if needed. For example:
Yaml
config/packages/sylius_grid.yaml
sylius_grid:grids:app_user:fields:email:type:stringlabel:app.ui.email# each field type can have a label, we suggest using translation keys instead of messagespath:contactDetails.email
PHP
config/packages/sylius_grid.php
<?phpuseSylius\Bundle\GridBundle\Builder\Field\StringField;useSylius\Bundle\GridBundle\Builder\GridBuilder;useSylius\Bundle\GridBundle\Config\GridConfig;returnstaticfunction (GridConfig $grid):void { $grid->addGrid(GridBuilder::create('app_user','%app.model.user.class%')->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')))};
OR
src/Grid/UserGrid.php
<?phpdeclare(strict_types=1);namespaceApp\Grid;useApp\Entity\User;useSylius\Bundle\GridBundle\Builder\Field\StringField;useSylius\Bundle\GridBundle\Builder\GridBuilderInterface;useSylius\Bundle\GridBundle\Grid\AbstractGrid;useSylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;finalclassUserGridextendsAbstractGridimplementsResourceAwareGridInterface{publicstaticfunctiongetName():string {return'app_user'; }publicfunctionbuildGrid(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')) ; }publicfunctiongetResourceClass():string {returnUser::class; }}
This configuration will display the value from $user->getContactDetails()->getEmail().
DateTime
This column type works exactly the same way as string, but expects a DateTime instance and outputs a formatted date and time string.
Available options:
format - default is Y:m:d H:i:s, you can modify it with any supported format (see https://www.php.net/manual/en/datetime.format.php)
timezone - default is %sylius_grid.timezone% parameter, null if such a parameter does not exist, you can modify it with any supported timezone (see https://www.php.net/manual/en/timezones.php)
<?phpuseSylius\Bundle\GridBundle\Builder\Field\DateTimeField;useSylius\Bundle\GridBundle\Builder\GridBuilder;useSylius\Bundle\GridBundle\Config\GridConfig;returnstaticfunction (GridConfig $grid):void { $grid->addGrid(GridBuilder::create('app_user','%app.model.user.class%')->addField(DateTimeField::create('birthday','Y:m:d H:i:s', null)// this format and timezone are the default value, but you can modify them->setLabel('app.ui.birthday')))};
OR
src/Grid/UserGrid.php
<?phpdeclare(strict_types=1);namespaceApp\Grid;useApp\Entity\User;useSylius\Bundle\GridBundle\Builder\Field\DateTimeField;useSylius\Bundle\GridBundle\Builder\GridBuilderInterface;useSylius\Bundle\GridBundle\Grid\AbstractGrid;useSylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;finalclassUserGridextendsAbstractGridimplementsResourceAwareGridInterface{publicstaticfunctiongetName():string {return'app_user'; }publicfunctionbuildGrid(GridBuilderInterface $gridBuilder):void { $gridBuilder->addField(DateTimeField::create('birthday','Y:m:d H:i:s', null)// this format and timezone are the default value, but you can modify them->setLabel('app.ui.birthday')) ; }publicfunctiongetResourceClass():string {returnUser::class; }}
Warning
You have to pass 'format' and 'timezone' again if you want to call the setOptions function. Otherwise, it will be unset:
Example:
$field->setOptions(['format'=>'Y-m-d H:i:s','timezone'=>'null'// Your options here]);
Twig (twig)
The Twig column type is the most flexible one, because it delegates the logic of rendering the value to the Twig templating engine. You just have to specify the template and it will be rendered with the data variable available to you.
In the @Grid/Column/_prettyName.html.twig template, you just need to render the value. For example:
<strong>{{ data }}</strong>
If you wish to render more complex grid fields, just redefine the path of the field to root – path: . and then you can access all attributes of the object instance: