Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: ExternalImage / Link setter #153

Open
FredoVelcro opened this issue Jan 11, 2022 · 2 comments
Open

[Feature]: ExternalImage / Link setter #153

FredoVelcro opened this issue Jan 11, 2022 · 2 comments

Comments

@FredoVelcro
Copy link

FredoVelcro commented Jan 11, 2022

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 FredoVelcro changed the title [Feature]: ExternalImage setter [Feature]: ExternalImage / Link setter Jan 11, 2022
@FredoVelcro
Copy link
Author

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

@fashxp
Copy link
Member

fashxp commented Jan 14, 2022

would you like to contribute a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants