Transformations configuration

A number of properties relate to transforming the data during the import process. All of these properties are used during the "Transform data" step. They are sub-properties of the transformations property.

Properties

Property Data type Step/Scope
isEmpty array Transform data
mapping Mapping configuration Transform data
rteEnabled boolean Transform data
trim boolean Transform data
userFunction array Transform data
value simple type (string, integer, float, boolean) Transform data

mapping

Type
Mapping configuration
Description
This property can be used to map values from the external data to values coming from some internal table. A typical example might be to match 2-letter country ISO codes to the uid of the "static_countries" table.
Scope
Transform data

value

Type
Simple type (string, integer, float, boolean)
Description

With this property, it is possible to set a fixed value for a given field. For example, this might be used to set a flag for all imported records. Or you might want to use different types for different import sources.

Example:

EXT:my_extension/Configuration/Overrides/tx_sometable.php
$GLOBALS['TCA']['tx_sometable'] = array_replace_recursive($GLOBALS['TCA']['tx_sometable'],
[
  // ...
    'columns' => [
        'type' => [
            'external' => [
                0 => [
                    'transformations' => [
                        10 => [
                            // Default type
                            'value' => 0
                        ]
                    ],
                ],
                'another_import' => [
                    'transformations' => [
                        10 => [
                            // Another type
                            'value' => 1
                        ]
                    ],
                ]
            ]
        ],
     // ...
    ],
]);
Copied!
Scope
Transform data

trim

Type
boolean
Description

If set to true, every value for this column will be trimmed during the transformation step.

Scope
Transform data

rteEnabled

Type
boolean
Description

If set to true when importing HTML data into a RTE-enable field, the imported data will go through the usual RTE transformation process on the way to the database.

Scope
Transform data

userFunction

Type
array
Description

This property can be used to define a function that will be called on each record to transform the data from the given field. See example below.

Example

Here is a sample setup referencing a user function:

$GLOBALS['TCA']['fe_users']['columns']['starttime']['external'] = [
 0 => [
    'field' => 'start_date',
    'transformations' => [
       10 => [
          'userFunction' => [
             'class' => \Cobweb\ExternalImport\Transformation\DateTimeTransformation::class,
             'method' => 'parseDate'
          ]
       ]
    ]
 ]
];
Copied!

The definition of a user function takes three parameters:

class
(string) Required. Name of the class to be instantiated.
method
(string) Required. Name of the method that should be called.
parameters (formerly "params")
(array) Optional. Can contain any number of data, which will be passed to the method. This used to be called "params". Backwards-compatibility is ensured for now, but please update your configuration as soon as possible.

In the example above we are using a sample class provided by External Import that can be used to parse a date and either return it as a timestamp or format it using either of the PHP functions date() or strftime() .

For more details about creating a user function, please refer to the Developer's Guide.

Scope
Transform data

isEmpty

Type
array
Description

This property is used to assess if a value in the given column can be considered empty or not and, if yes, act on it. The action can be either to set a default value or to remove the entire record from the imported dataset.

Deciding whether a given value is "empty" is a bit tricky, since null, false, 0 or an empty string - to name a few - could all be considered empty depending on the circumstances. By default, this property will rely on the PHP function empty(). However it is also possible to evaluate an expression based on the values in the record using the Symfony Expression Language.

expression

(string) A condition using the Symfony Expression Language syntax. If it evaluates to true, the action (see below) will be triggered. The values in the record can be used, by simply referencing them with the column name.

If no expression is defined, the PHP function empty() is used.

See the Symfony documentation for reference.

invalidate
(bool) Set this property to true to discard the entire record from the imported dataset if the expression (or empty()) evaluated to true. invalidate takes precedence over default.
default
(mixed) If the expression (or empty()) evaluates to true, this value will be set in the record instead of the empty value.

Example

'store_code' => [
    'exclude' => 0,
    'label' => 'Code',
    'config' => [
        'type' => 'input',
        'size' => 10
    ],
    'external' => [
        0 => [
            'field' => 'code',
            'transformations' => [
                10 => [
                    'trim' => true
                ],
                20 => [
                    'isEmpty' => [
                        'expression' => 'store_code === ""',
                        'invalidate' => true
                    ]
                ],
            ]
        ]
    ]
],
Copied!

In this example, the store_code field is compared with an empty string. Any record with an empty string in that column will be removed from the dataset.