diff --git a/src/Resolver/Location/FindParentStrategy.php b/src/Resolver/Location/FindParentStrategy.php index 58b78ea2..76d879b6 100644 --- a/src/Resolver/Location/FindParentStrategy.php +++ b/src/Resolver/Location/FindParentStrategy.php @@ -15,10 +15,12 @@ namespace Pimcore\Bundle\DataImporterBundle\Resolver\Location; +use Exception; 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\AbstractObject; use Pimcore\Model\DataObject\ClassDefinition; use Pimcore\Model\Element\ElementInterface; @@ -134,25 +136,28 @@ public function updateParent(ElementInterface $element, array $inputData): Eleme } if ($newParent) { - if ($newParent->getType() === DataObject::OBJECT_TYPE_VARIANT) { + if ($newParent->getType() === AbstractObject::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 - && $element instanceof DataObject\Concrete - && $element::class === $newParent::class - && $element->getClass()->getAllowVariants() - && !$element->hasChildren() - ) { - /** @var DataObject\Concrete $element */ - $element->setType(DataObject::OBJECT_TYPE_VARIANT); + try { + // Check if element should be saved as a variant. + if ( + $this->saveAsVariant + && $element instanceof DataObject\Concrete + && $element::class === $newParent::class + && !$element->hasChildren() + && $element->getClass()->getAllowVariants() + ) { + $element->setType(AbstractObject::OBJECT_TYPE_VARIANT); + } + + return $element->setParent($newParent); + } catch (Exception) { + // Exception might be thrown by $element->getClass(). } - - return $element->setParent($newParent); } return $element; diff --git a/src/Resolver/Resolver.php b/src/Resolver/Resolver.php index 17e9aee8..c4da3707 100644 --- a/src/Resolver/Resolver.php +++ b/src/Resolver/Resolver.php @@ -20,7 +20,7 @@ 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\DataObject\AbstractObject; use Pimcore\Model\Element\ElementInterface; class Resolver @@ -136,6 +136,9 @@ public function loadElementByIdentifier($identifier): ?ElementInterface return $this->getLoadingStrategy()->loadElementByIdentifier($identifier); } + /** + * @throws InvalidInputException + */ public function loadOrCreateAndPrepareElement(array $inputData, bool $createNew = true): ?ElementInterface { $element = $this->loadElement($inputData); @@ -155,7 +158,10 @@ public function loadOrCreateAndPrepareElement(array $inputData, bool $createNew $this->getLocationUpdateStrategy()->updateParent($element, $inputData); // The parent of a variant cannot be changed anymore. - if ($oldParentId !== $element->getParentId() && $element->getType() === DataObject::OBJECT_TYPE_VARIANT) { + if ( + $oldParentId !== $element->getParentId() + && $element->getType() === AbstractObject::OBJECT_TYPE_VARIANT + ) { throw new InvalidInputException( "Element with id `{$element->getId()}` is a variant and cannot change its parent anymore" );