data-importer icon indicating copy to clipboard operation
data-importer copied to clipboard

[Feature]: ExternalImage / Link setter

Open FredoVelcro opened this issue 3 years ago • 2 comments

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

FredoVelcro avatar Jan 11 '22 15:01 FredoVelcro

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);
    }
}

FredoVelcro avatar Jan 13 '22 16:01 FredoVelcro

would you like to contribute a PR?

fashxp avatar Jan 14 '22 08:01 fashxp