Important
You are browsing the documentation for version 4.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation 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 datasource, it applies after the result set is fetched by the datagrid and provides changes using formatters that are described in the config. This extension is also responsible for passing columns configuration to the view layer.
Formatters¶
Field¶
1 column_name:
2 type: field # default value `field`, so this key could be skipped here
3 frontend_type: date|datetime|decimal|integer|percent|currency|select|text|html|boolean # optional default string
4 data_name: someAlias.someField # optional, key in result that should represent this field
5 divisor: some number # optional if you need to divide a numeric value by a number before rendering it
Represents the default data field.
URL¶
1 column_name:
2 type: url
3 route: some_route # required
4 isAbsolute: true|false # optional
5 params: [] # optional params for route generating, will be took from record
6 anchor: string #optional, use it when need to add some #anchor to generated url
Represents URL field, mostly used for generating urls for actions.
Link¶
1 column_name:
2 type: link
3 route: some_route # required
4 isAbsolute: true|false # optional
5 params: [] # optional params for route generating, will be took from record
6 anchor: string #optional, use it when need to add some #anchor to generated url
7 frontend_type: html #needed to display prepared link (a-tag) as html
Represents a link field to display a link as html. Link text is value of records column_name
, values for url generation are specified via params
.
Twig¶
1 column_name:
2 type: twig
3 template: string # required, template name
4 context: [] # optional, should not contain reserved keys(record, value)
Represents twig template formatted field.
Translatable¶
1 column_name:
2 type: translatable
3 data_name: string #optional if need to took value from another column
4 domain: string #optional
5 locale: string #optional
Used when field should be translated by symfony translator.
Callback¶
1 column_name:
2 type: callback
3 callable: "@link" # required
Used when field should be formatted using a callback, see Reference in YAML Configuration for more information.
Note that the whole node configuration is passed to the callback method as the $node
argument.
Therefore, if you need is to pass some arguments to the callback method, you can add any parameter to the grid config, e.g.:
1 column_name:
2 type: callback
3 callable: "@link.to.some.service->myCallbackMethod"
4 myCallbackParam: 'Some Value'
And then use this parameter in the callback method like this:
1 use Oro\Bundle\DataGridBundle\Datasource\ResultRecordInterface;
2
3 class MyFormatterService
4 {
5 public function myCallbackMethod($gridName, $keyName, $node)
6 {
7 if (!array_key_exists('myCallbackParam', $node)) {
8 return false;
9 }
10
11 $myCallbackParam = $node['myCallbackParam'];
12
13 return function (ResultRecordInterface $record) use ($myCallbackParam) {
14 $result = '';
15 // Do something using $myCallbackParam
16
17 return $result;
18 };
19 }
20 }
Localized Number¶
1 column_name:
2 type: localized_number
3 method: formatCurrency # required
4 context: [] # optional
5 context_resolver: "@callable" # optional
6 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 backend.
method - method from NumberFormatter that should be used for formatting
context - static arguments for 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
1 column_name:
2 type: localized_number
3 method: formatCurrency
4 context_resolver: staticClass::staticFunc
1 class staticClass {
2 public static function staticFunc()
3 {
4 return function (ResultRecordInterface $record, $value, NumberFormatter $formatter) {
5 return [$record->getValue('currencyFieldInResultRow')];
6 };
7 }
8 }
9
10 // will call
11 // 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
(also there is basic implementation inAbstractProperty
)Register your formatter as a service tagged as
{ name: oro_datagrid.extension.formatter.property, type: YOUR_TYPE }