Important

You are browsing documentation for version 5.1 of OroCommerce, supported until March 2026. Read the documentation for version 6.0 (the latest LTS version) to get up-to-date information.

See our Release Process documentation for more information on the currently supported and upcoming releases.

Formatter Extension 

This extension does not affect the datasource and is applied after the result set is fetched by the datagrid and provides changes using formatters described in the config. This extension is also responsible for passing column configuration to the view layer.

Formatters 

Field 

column_name:
    type: field # default value `field`, so this key could be skipped here
    frontend_type: date|datetime|decimal|integer|percent|currency|select|text|html|boolean # optional default string
    data_name: someAlias.someField # optional, key in result that should represent this field
    divisor: some number # optional if you need to divide a numeric value by a number before rendering it

Represents the default data field.

URL 

column_name:
    type: url
    route: some_route # required
    isAbsolute: true|false # optional
    params: [] # optional params for route generating, will be took from record
    anchor: string #optional, use it when need to add some #anchor to generated url

Represents URL field, mostly used for generating urls for actions.

Twig 

column_name:
    type: twig
    template: string # required, template name
    context: [] # optional, should not contain reserved keys(record, value)

Represents twig template formatted field.

Translatable 

column_name:
    type: translatable
    data_name: string #optional if need to take value from another column
    domain: string #optional
    locale: string #optional

Used when the field should be translated by Symfony translator.

Callback 

column_name:
    type: callback
    callable: "@link" # required

Used when field should be formatted using a callback, see Reference in YAML Configuration for more information.

The whole node configuration is passed to the callback method as the $node argument. Therefore, if you need to pass some arguments to the callback method, you can add any parameter to the grid config, e.g.:

column_name:
    type: callback
    callable: "@link.to.some.service->myCallbackMethod"
    myCallbackParam: 'Some Value'

And then use this parameter in the callback method like this:

use Oro\Bundle\DataGridBundle\Datasource\ResultRecordInterface;

class MyFormatterService
{
    public function myCallbackMethod($gridName, $keyName, $node)
    {
        if (!array_key_exists('myCallbackParam', $node)) {
            return false;
        }

        $myCallbackParam = $node['myCallbackParam'];

        return function (ResultRecordInterface $record) use ($myCallbackParam) {
            $result = '';
            // Do something using $myCallbackParam

            return $result;
        };
    }
}

Localized Number 

column_name:
    type: localized_number
    method: formatCurrency        # required
    context: []                   # optional
    context_resolver: "@callable" # optional
    divisor: some number # optional if you need to divide a numeric value by a number before rendering it

Used to format numbers using Oro\Bundle\LocaleBundle\Formatter\NumberFormatter on the backend.

  • method - method from NumberFormatter that should be used for formatting

  • context - static arguments for the method that will be called, starts from 2nd arg

  • context_resolver - callback that will resolve dynamic arguments for method that will be called, starts from 2nd arg should be compatible with following declaration: function (ResultRecordInterface $record, $value, NumberFormatter $formatter) {}

Example:

We would like to format currency, but the currency code should be retrieved from the current row.

column_name:
    type: localized_number
    method: formatCurrency
    context_resolver: staticClass::staticFunc
class staticClass {
    public static function staticFunc()
        {
            return function (ResultRecordInterface $record, $value, NumberFormatter $formatter) {
                return [$record->getValue('currencyFieldInResultRow')];
            };
        }
}

// will call
// NumberFormatter->formatCurrency('value of column_name field', 'value of currencyFieldInResultRow field');

Note

Option frontend_type can be applied to the formatter of any type, it will be used to format cell data in the frontend.

Customization 

To implement your own formatter:

  • Develop a class that implements PropertyInterface (there is also basic implementation in AbstractProperty)

  • Register your formatter as a service tagged as { name: oro_datagrid.extension.formatter.property, type: YOUR_TYPE }

Related Articles