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
Labels
Comments
FredoVelcro
changed the title
[Feature]: ExternalImage setter
[Feature]: ExternalImage / Link setter
Jan 11, 2022
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? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The text was updated successfully, but these errors were encountered: