data-importer
data-importer copied to clipboard
[Feature]: ExternalImage / Link setter
Feature description
I think we should add an ExternalImage::url support/setter for this type of dataobject field. It lacks... Note : same thing for the Link dataobject field type
Sample implementation for External Image :
<?php
namespace App\DataHub\DataImporter\Mapping\DataTarget;
use Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException;
use Pimcore\Bundle\DataImporterBundle\Mapping\DataTarget\DataTargetInterface;
use Pimcore\Model\Element\ElementInterface;
class ExternalImage implements DataTargetInterface
{
protected string $fieldName;
protected bool $writeIfSourceIsEmpty;
protected bool $writeIfTargetIsNotEmpty;
/**
* @param array $settings
*
* @throws InvalidConfigurationException
*/
public function setSettings(array $settings): void
{
if (empty($settings['fieldName'])) {
throw new InvalidConfigurationException('Empty field name.');
}
$this->fieldName = $settings['fieldName'];
//note - cannot be replaced with ?? as $settings['writeIfSourceIsEmpty'] can be false on purpose
$this->writeIfSourceIsEmpty = isset($settings['writeIfSourceIsEmpty']) ? $settings['writeIfSourceIsEmpty'] : true;
$this->writeIfTargetIsNotEmpty = isset($settings['writeIfTargetIsNotEmpty']) ? $settings['writeIfTargetIsNotEmpty'] : true;
}
/**
* @param ElementInterface $element
* @param $data
* @return void
*/
public function assignData(ElementInterface $element, $data): void
{
$getter = 'get' . ucfirst($this->fieldName);
if (!$this->writeIfTargetIsNotEmpty && $element->$getter()) {
return;
}
if (!$this->writeIfSourceIsEmpty && empty($data)) {
return;
}
$externalImage = new \Pimcore\Model\DataObject\Data\ExternalImage($data);
$setter = 'set' . ucfirst($this->fieldName);
$element->$setter($externalImage);
}
}
Link :
<?php
namespace App\DataHub\DataImporter\Mapping\DataTarget;
use Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException;
use Pimcore\Bundle\DataImporterBundle\Mapping\DataTarget\DataTargetInterface;
use Pimcore\Model\Element\ElementInterface;
class Link implements DataTargetInterface
{
protected string $fieldName;
protected bool $writeIfSourceIsEmpty;
protected bool $writeIfTargetIsNotEmpty;
/**
* @param array $settings
*
* @throws InvalidConfigurationException
*/
public function setSettings(array $settings): void
{
if (empty($settings['fieldName'])) {
throw new InvalidConfigurationException('Empty field name.');
}
$this->fieldName = $settings['fieldName'];
//note - cannot be replaced with ?? as $settings['writeIfSourceIsEmpty'] can be false on purpose
$this->writeIfSourceIsEmpty = isset($settings['writeIfSourceIsEmpty']) ? $settings['writeIfSourceIsEmpty'] : true;
$this->writeIfTargetIsNotEmpty = isset($settings['writeIfTargetIsNotEmpty']) ? $settings['writeIfTargetIsNotEmpty'] : true;
}
/**
* @param ElementInterface $element
* @param $data
* @return void
*/
public function assignData(ElementInterface $element, $data): void
{
$getter = 'get' . ucfirst($this->fieldName);
if (!$this->writeIfTargetIsNotEmpty && $element->$getter()) {
return;
}
if (!$this->writeIfSourceIsEmpty && empty($data)) {
return;
}
$link = new \Pimcore\Model\DataObject\Data\Link();
$link->setPath($data);
$setter = 'set' . ucfirst($this->fieldName);
$element->$setter($link);
}
}
would you like to contribute a PR?