diff --git a/src/Processing/ImportProcessingService.php b/src/Processing/ImportProcessingService.php index 1e220f87..561bf345 100644 --- a/src/Processing/ImportProcessingService.php +++ b/src/Processing/ImportProcessingService.php @@ -227,6 +227,7 @@ protected function processElement(string $configName, array $importDataRow, Reso $event = new PreSaveEvent($configName, $importDataRow, $element); $this->eventDispatcher->dispatch($event); + $this->checkKey($element); $element->save(); $event = new PostSaveEvent($configName, $importDataRow, $element); @@ -390,4 +391,11 @@ public function cancelImportAndCleanupQueue(string $configName): void TmpStore::delete($infoEntryId); $this->queueService->cleanupQueueItems($configName); } + + private function checkKey(ElementInterface $element): void + { + if(empty($element->getKey())) { + $element->setKey(uniqid('import-', true)); + } + } } diff --git a/src/Resolver/Factory/DataObjectFactory.php b/src/Resolver/Factory/DataObjectFactory.php index 7d31775e..29fad639 100644 --- a/src/Resolver/Factory/DataObjectFactory.php +++ b/src/Resolver/Factory/DataObjectFactory.php @@ -15,6 +15,7 @@ namespace Pimcore\Bundle\DataImporterBundle\Resolver\Factory; +use Exception; use Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException; use Pimcore\Model\DataObject\ClassDefinition; use Pimcore\Model\Element\ElementInterface; @@ -44,9 +45,9 @@ public function setSubType(string $subType): void /** * @throws InvalidConfigurationException - * @throws \Exception + * @throws Exception */ - public function createNewElement(bool $assignUniqueIdAsKey = true): ElementInterface + public function createNewElement(): ElementInterface { $class = ClassDefinition::getById($this->subType); if (empty($class)) { @@ -62,9 +63,7 @@ public function createNewElement(bool $assignUniqueIdAsKey = true): ElementInter ); } - if($assignUniqueIdAsKey) { - $element->setKey(uniqid('import-', true)); - } + $element->setKey(uniqid('import-', true)); return $element; } diff --git a/src/Resolver/Factory/FactoryInterface.php b/src/Resolver/Factory/FactoryInterface.php index 13d654a5..596b4c74 100644 --- a/src/Resolver/Factory/FactoryInterface.php +++ b/src/Resolver/Factory/FactoryInterface.php @@ -31,5 +31,5 @@ public function setSubType(string $subType): void; * * @return ElementInterface */ - public function createNewElement(bool $assignUniqueIdAsKey = true): ElementInterface; + public function createNewElement(): ElementInterface; } diff --git a/src/Resolver/Resolver.php b/src/Resolver/Resolver.php index 0e603c89..03becd36 100644 --- a/src/Resolver/Resolver.php +++ b/src/Resolver/Resolver.php @@ -150,8 +150,15 @@ public function loadOrCreateAndPrepareElement(array $inputData, bool $createNew } if (empty($element)) { - $hasKeySet = array_key_exists('key', $inputData) && !empty($inputData['key']); - $element = $this->getElementFactory()->createNewElement($hasKeySet); + $element = $this->getElementFactory()->createNewElement(); + /** + * Reset key for new element, otherwise a key, that gets passed in the input data, would not be used. + * See checkKey method in ImportProcessingService, which adds the key again if necessary. + * @see \Pimcore\Bundle\DataImporterBundle\Processing\ImportProcessingService::checkKey + * + * Needs to be done here, because adding a parameter to createNewElement would be a bc break. + */ + $element->setKey(''); $this->getCreateLocationStrategy()->updateParent($element, $inputData); $justCreated = true; } else {