Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.9' into 1.x
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Processing/ImportProcessingService.php
  • Loading branch information
mcop1 committed Dec 2, 2024
2 parents 8882b01 + 5a08c98 commit d9c7d8a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/Mapping/DataTarget/Direct.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,20 @@ protected function checkAssignData($newData, $valueContainer, $getter)
$fieldName = $fieldNameParts[2];
}

$fieldDefinition = $this->getFieldDefinition($valueContainer, $fieldName);
if ($this->writeIfTargetIsNotEmpty === false && !$fieldDefinition->isEmpty($currentData)) {
if ($this->fieldName === 'key') {
$currentDataIsEmpty = empty($currentData);
$newDataIsEmpty = empty($newData);
} else {
$fieldDefinition = $this->getFieldDefinition($valueContainer, $fieldName);
$currentDataIsEmpty = $fieldDefinition->isEmpty($currentData);
$newDataIsEmpty = $fieldDefinition->isEmpty($newData);
}

if ($this->writeIfTargetIsNotEmpty === false && !$currentDataIsEmpty) {
return false;
}

if ($this->writeIfSourceIsEmpty === false && $fieldDefinition->isEmpty($newData)) {
if ($this->writeIfSourceIsEmpty === false && $newDataIsEmpty) {
return false;
}

Expand Down
8 changes: 8 additions & 0 deletions src/Processing/ImportProcessingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ protected function processElement(string $configName, array $importDataRow, Reso
$event = new PreSaveEvent($configName, $importDataRow, $element);
$this->eventDispatcher->dispatch($event);

$this->checkKey($element);
$element
->setUserModification($userOwner)
->save();
Expand Down Expand Up @@ -393,4 +394,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));
}
}
}
17 changes: 12 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 @@ -42,6 +43,10 @@ public function setSubType(string $subType): void
$this->subType = $subType;
}

/**
* @throws InvalidConfigurationException
* @throws Exception
*/
public function createNewElement(): ElementInterface
{
$class = ClassDefinition::getById($this->subType);
Expand All @@ -52,12 +57,14 @@ public function createNewElement(): ElementInterface
$className = '\\Pimcore\\Model\\DataObject\\' . ucfirst($class->getName());
$element = $this->modelFactory->build($className);

if ($element instanceof ElementInterface) {
$element->setKey(uniqid('import-', true));

return $element;
if (!($element instanceof ElementInterface)) {
throw new InvalidConfigurationException(
"Object of class `{$this->subType}` could not be created."
);
}

throw new InvalidConfigurationException("Object of class `{$this->subType}` could not be created.");
$element->setKey(uniqid('import-', true));

return $element;
}
}
9 changes: 9 additions & 0 deletions src/Resolver/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ public function loadOrCreateAndPrepareElement(array $inputData, bool $createNew

if (empty($element)) {
$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 d9c7d8a

Please sign in to comment.