Important

You are browsing documentation for version 5.1 of OroCommerce, supported until March 2027. Read the documentation for 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. It runs after the datagrid fetches the result set and applies the formatters described in the config.

It also passes 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 a field formatted with a Twig template.

Translatable 

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

Translates the field with the 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 callback method receives the whole node configuration as the $node argument. To pass extra arguments to the callback method, 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:

Format currency where the currency code comes 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