Skip to content

Commit

Permalink
Added parameter to uniqueid as key assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
mcop1 committed Oct 28, 2024
1 parent e2d277a commit f534ae2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/Resolver/Factory/DataObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public function setSubType(string $subType): void
$this->subType = $subType;
}

public function createNewElement(): ElementInterface
/**
* @throws InvalidConfigurationException
* @throws \Exception
*/
public function createNewElement(bool $assignUniqueIdAsKey = true): ElementInterface
{
$class = ClassDefinition::getById($this->subType);
if (empty($class)) {
Expand All @@ -52,12 +56,16 @@ 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));
if (!($element instanceof ElementInterface)) {
throw new InvalidConfigurationException(
"Object of class `{$this->subType}` could not be created."
);
}

return $element;
if($assignUniqueIdAsKey) {
$element->setKey(uniqid('import-', true));
}

throw new InvalidConfigurationException("Object of class `{$this->subType}` could not be created.");
return $element;
}
}
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(): ElementInterface;
public function createNewElement(bool $assignUniqueIdAsKey = true): ElementInterface;
}
3 changes: 2 additions & 1 deletion src/Resolver/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ public function loadOrCreateAndPrepareElement(array $inputData, bool $createNew
}

if (empty($element)) {
$element = $this->getElementFactory()->createNewElement();
$hasKeySet = array_key_exists('key', $inputData) && !empty($inputData['key']);
$element = $this->getElementFactory()->createNewElement($hasKeySet);
$this->getCreateLocationStrategy()->updateParent($element, $inputData);
$justCreated = true;
} else {
Expand Down

0 comments on commit f534ae2

Please sign in to comment.