-
Notifications
You must be signed in to change notification settings - Fork 5
Import Field Operator
As we can see in the Documentation, in CSV Import, value contained in a simple cell may be imported in a complex way. Pimcore allow to manage cell values in order to properly import all kind of fields.
In order to implement a custom field operator, just create a PHP class that extends the Pimcore AbstractOperator class.
In the following example, we will see a simple operator that parse a column text and decode html entities (e.g. the string "<br>" is trasformed in "<br>").
We use the additional data to let Pimcore know in which object field the formatted value must be set. Let's say that we want to use our custom operator for the "description" field; the additional data passed to the operator will be a simple json string like:
{"field": "description"}
Using PHP reflection, we are able to properly set the parsed field.
namespace SintraPimcoreBundle\Import\Operators;
use Pimcore\DataObject\Import\ColumnConfig\Operator\AbstractOperator;
/**
* Escape HTML tags
*/
abstract class HTMLOperator extends AbstractOperator{
private $additionalData;
public function __construct(\stdClass $config, $context = null)
{
parent::__construct($config, $context);
$this->additionalData = json_decode($config->additionalData,true);
}
/**
* Dynamically invoke field setter for html fields
*/
public function process($element, &$target, array &$rowData, $colIndex, array &$context = array()) {
$text = $rowData[$colIndex];
$field = $this->additionalData["field"];
$reflection = new \ReflectionObject($target);
$setFieldMethod = $reflection->getMethod('set'. ucfirst($field));
$setFieldMethod->invoke($target, $this->escapeHTML($text));
}
public function escapeHTML($text){
return html_entity_decode($text);
}
}