How to Customize the Import Behavior

Importations in the PIM are largely automatized by using the Doctrine metadata, through a set of guessers and property transformers. Import behavior is customizable for all entities by adding new prioritized guessers and transformers.

Creating a Guesser

Guessers are used at the beginning of each importation to link each property of the imported data to a property transformer.

For example, to skip all columns starting with a #, the following guesser could be created:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php

namespace Acme\Bundle\DemoConnectorBundle\Transformer\Guesser;

use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Pim\Bundle\TransformBundle\Transformer\Guesser\GuesserInterface;
use Pim\Bundle\TransformBundle\Transformer\ColumnInfo\ColumnInfoInterface;
use Pim\Bundle\TransformBundle\Transformer\Property\PropertyTransformerInterface;

class CommentGuesser implements GuesserInterface
{
    protected $transformer;

    public function __construct(PropertyTransformerInterface $transformer)
    {
        $this->transformer = $transformer;
    }

    public function getTransformerInfo(ColumnInfoInterface $columnInfo, ClassMetadataInfo $metadata)
    {
        if ('#' !== substr($columnInfo->getLabel(), 0, 1)) {
            return;
        }

        return array($this->transformer, array());
    }
}

The way the guesser works is simple:

  • If the column’s title does not start with a #, null is returned.
  • If the column starts with a #, an array containing the transformer service and its options is returned

The guesser must be included in the following way in the DIC:

1
2
3
4
5
6
7
8
# /src/Acme/Bundle/DemoConnectorBundle/Resources/config/guessers.yml
services:
    acme_democonnector.transformer.guesser.comment:
        class: Acme\Bundle\DemoConnectorBundle\Transformer\Guesser\CommentGuesser
        arguments:
            - '@pim_import_export.transformer.property.skip'
        tags:
            - { name: pim_import_export.transformer.guesser, priority: 40 }

As you can see, the transformer returned by the guesser will be the pim_import_export.transformer.property.skip transformer.

Creating a Transformer

In the following example, we create a transformer which prepends a string to a scalar value:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php

namespace Acme\Bundle\DemoConnectorBundle\Transformer\Property;

use Pim\Bundle\TransformBundle\Transformer\Property\PropertyTransformerInterface;

class PrependTransformer implements PropertyTransformerInterface
{
    public function transform($value, array $options = array())
    {
        $value = trim($value);

        return empty($value) ? null : $options['prepend'] . $value;
    }
}

This transformer requires that the string to be prepended is passed in the options, to make it work, you should therefore add a custom guesser which will pass it.

To add the transformer to the DIC, proceed in the following way:

1
2
3
4
5
# /src/Acme/Bundle/DemoConnectorBundle/Resources/config/transformers.yml
services:
    acme_democonnector.transformer.property.prepend:
        class: Acme\Bundle\DemoConnectorBundle\Transformer\Property\PrependTransformer
        public: false