Skip to content

Commit

Permalink
Allowing variants to be changed to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo-ele committed Apr 10, 2024
1 parent 70aeb60 commit 862977e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Resolver/Location/FindParentStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,26 +174,36 @@ private function setElementType(ElementInterface $element, DataObject | ElementI
|| $element::class !== $newParent::class
) {
throw new InvalidInputException(
'Only concrete objects of the same class can be saved as a variant.'
'Changing type not possible: Only concrete objects of the same class can be saved as a variant.'
);
}

if ($element->hasChildren()) {
throw new InvalidInputException(
'Only objects without any children can be saved as a variant.'
'Changing type not possible: Only objects without any children can be saved as a variant.'
);
}

if (!$this->getElementClassDefinition($element)?->getAllowVariants()) {
throw new InvalidInputException(
sprintf(
'Class `%s` is not configured to allow the creation of variants.',
'Changing type not possible: Class `%s` is not configured to allow the creation of variants.',
$this->getElementClassDefinition($element)?->getName(),
)
);
}

$element->setType(AbstractObject::OBJECT_TYPE_VARIANT);
} elseif (
!$this->saveAsVariant && $element->getType() !== AbstractObject::OBJECT_TYPE_OBJECT
) {
if ($newParent->getType() === AbstractObject::OBJECT_TYPE_VARIANT) {
throw new InvalidInputException(
'Changing type not possible: An object cannot be a child of a variant.'
);
}

$element->setType(AbstractObject::OBJECT_TYPE_OBJECT);
}
}

Expand Down

0 comments on commit 862977e

Please sign in to comment.