Fields

Configuring Fields

Each field can be configured with several configuration keys, to make it more suitable to your grid requirements.

Name
Type
Description

type

string

Type of column. Default field types are described herearrow-up-right.

label

string

Label displayed in the field header. By default, it is field name.

path

string

Path to property displayed in field (can be property of resource or one of its referenced objects).

position

int

Position of field in the grid index view.

options

array

Array of field options (see below).

The options field can itself contain the following fields:

Name
Type
Description
Default

template

string

Available (and required) only for twig column type. Path to template that is used to render column value.

format

string

Available only for datetime field type.

Y:m:d H:i:s

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:

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:

circle-exclamation

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.

circle-exclamation

Creating a custom Field Type

There are certain cases when built-in field types are not enough. Sylius Grids make it easy to define new types.

All you need to do is create your own class implementing FieldTypeInterface and register it as a service.

That is all. Now register your new field type as a service.

Now you can use your new column type in the grid configuration!

Last updated

Was this helpful?