Skip to content

Commit

Permalink
Avoid bc break
Browse files Browse the repository at this point in the history
  • Loading branch information
mcop1 committed Oct 28, 2024
1 parent f534ae2 commit 846313a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/Processing/ImportProcessingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
}
}
9 changes: 4 additions & 5 deletions src/Resolver/Factory/DataObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Resolver/Factory/FactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ public function setSubType(string $subType): void;
*
* @return ElementInterface
*/
public function createNewElement(bool $assignUniqueIdAsKey = true): ElementInterface;
public function createNewElement(): ElementInterface;
}
11 changes: 9 additions & 2 deletions src/Resolver/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 846313a

Please sign in to comment.