From 5a33deb40cb0d59c9487e49e47a24a86f1b115b0 Mon Sep 17 00:00:00 2001 From: Elias Hiller Date: Thu, 9 Nov 2023 14:41:54 +0100 Subject: [PATCH] Added error handling when changing existing variants As the parent can be changed for variants this is now catched, as a variant cannot change its parent anymore. --- src/Resolver/Location/FindParentStrategy.php | 19 +++++++++++++------ src/Resolver/Resolver.php | 10 ++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/Resolver/Location/FindParentStrategy.php b/src/Resolver/Location/FindParentStrategy.php index 6b8316c0..58b78ea2 100644 --- a/src/Resolver/Location/FindParentStrategy.php +++ b/src/Resolver/Location/FindParentStrategy.php @@ -16,6 +16,7 @@ namespace Pimcore\Bundle\DataImporterBundle\Resolver\Location; use Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException; +use Pimcore\Bundle\DataImporterBundle\Exception\InvalidInputException; use Pimcore\Bundle\DataImporterBundle\Tool\DataObjectLoader; use Pimcore\Model\DataObject; use Pimcore\Model\DataObject\ClassDefinition; @@ -133,11 +134,19 @@ public function updateParent(ElementInterface $element, array $inputData): Eleme } if ($newParent) { + if ($newParent->getType() === DataObject::OBJECT_TYPE_VARIANT) { + throw new InvalidInputException( + 'The elements desired parent is a variant which cannot have any child elements' + ); + } + + // Check if element should be saved as a variant. if ( - $this->saveAsVariant && - $newParent instanceof DataObject\Concrete && - $newParent::class === $element::class && - $newParent->getClass()->getAllowVariants() + $this->saveAsVariant + && $element instanceof DataObject\Concrete + && $element::class === $newParent::class + && $element->getClass()->getAllowVariants() + && !$element->hasChildren() ) { /** @var DataObject\Concrete $element */ $element->setType(DataObject::OBJECT_TYPE_VARIANT); @@ -146,8 +155,6 @@ public function updateParent(ElementInterface $element, array $inputData): Eleme return $element->setParent($newParent); } - // Save the element as variant: The parent and element need to be of the same dataobject type. - return $element; } diff --git a/src/Resolver/Resolver.php b/src/Resolver/Resolver.php index 97af4256..17e9aee8 100644 --- a/src/Resolver/Resolver.php +++ b/src/Resolver/Resolver.php @@ -15,10 +15,12 @@ namespace Pimcore\Bundle\DataImporterBundle\Resolver; +use Pimcore\Bundle\DataImporterBundle\Exception\InvalidInputException; use Pimcore\Bundle\DataImporterBundle\Resolver\Factory\FactoryInterface; use Pimcore\Bundle\DataImporterBundle\Resolver\Load\LoadStrategyInterface; use Pimcore\Bundle\DataImporterBundle\Resolver\Location\LocationStrategyInterface; use Pimcore\Bundle\DataImporterBundle\Resolver\Publish\PublishStrategyInterface; +use Pimcore\Model\DataObject; use Pimcore\Model\Element\ElementInterface; class Resolver @@ -149,7 +151,15 @@ public function loadOrCreateAndPrepareElement(array $inputData, bool $createNew $this->getCreateLocationStrategy()->updateParent($element, $inputData); $justCreated = true; } else { + $oldParentId = $element->getParentId(); $this->getLocationUpdateStrategy()->updateParent($element, $inputData); + + // The parent of a variant cannot be changed anymore. + if ($oldParentId !== $element->getParentId() && $element->getType() === DataObject::OBJECT_TYPE_VARIANT) { + throw new InvalidInputException( + "Element with id `{$element->getId()}` is a variant and cannot change its parent anymore" + ); + } } $this->getPublishingStrategy()->updatePublishState($element, $justCreated, $inputData);