From dfb88175ec761193b9f7837c15aa08b0f9e92437 Mon Sep 17 00:00:00 2001 From: Martin Eiber Date: Mon, 2 Dec 2024 12:37:10 +0100 Subject: [PATCH] [Data Object Editor] Add Endpoint to get objectbrick layout (#601) * Fix Return Type. * Get Layout Definition for Object Bricks. * Add translation. * Fix schema * Apply php-cs-fixer changes * Add return type * Apply php-cs-fixer changes * Fix line length. * Apply php-cs-fixer changes * Fix return Value. * Fix Return Value * Import namespace. * Apply php-cs-fixer changes --------- Co-authored-by: martineiber --- config/class.yaml | 8 +- .../FieldCollection/LayoutController.php | 2 +- .../ObjectBrick/LayoutController.php | 82 +++++++++++ .../FieldCollection/LayoutDefinitionEvent.php | 2 +- .../ObjectBrick/LayoutDefinitionEvent.php | 35 +++++ .../LayoutDefinitionHydrator.php | 2 +- .../LayoutDefinitionHydratorInterface.php | 2 +- .../ObjectBrick/LayoutDefinitionHydrator.php | 45 +++++++ .../LayoutDefinitionHydratorInterface.php | 28 ++++ .../LayoutDefinition.php | 3 +- .../Schema/ObjectBrick/LayoutDefinition.php | 127 ++++++++++++++++++ .../LayoutDefinitionService.php | 6 +- .../LayoutDefinitionServiceInterface.php | 3 + .../ObjectBrick/LayoutDefinitionService.php | 96 +++++++++++++ .../LayoutDefinitionServiceInterface.php | 34 +++++ translations/studio_api_docs.en.yaml | 4 + 16 files changed, 470 insertions(+), 9 deletions(-) create mode 100644 src/Class/Controller/ObjectBrick/LayoutController.php create mode 100644 src/Class/Event/ObjectBrick/LayoutDefinitionEvent.php create mode 100644 src/Class/Hydrator/ObjectBrick/LayoutDefinitionHydrator.php create mode 100644 src/Class/Hydrator/ObjectBrick/LayoutDefinitionHydratorInterface.php rename src/Class/Schema/{ => FieldCollection}/LayoutDefinition.php (96%) create mode 100644 src/Class/Schema/ObjectBrick/LayoutDefinition.php create mode 100644 src/Class/Service/ObjectBrick/LayoutDefinitionService.php create mode 100644 src/Class/Service/ObjectBrick/LayoutDefinitionServiceInterface.php diff --git a/config/class.yaml b/config/class.yaml index e818c5e5..3cd25c3c 100644 --- a/config/class.yaml +++ b/config/class.yaml @@ -17,6 +17,9 @@ services: Pimcore\Bundle\StudioBackendBundle\Class\Service\FieldCollection\LayoutDefinitionServiceInterface: class: Pimcore\Bundle\StudioBackendBundle\Class\Service\FieldCollection\LayoutDefinitionService + Pimcore\Bundle\StudioBackendBundle\Class\Service\ObjectBrick\LayoutDefinitionServiceInterface: + class: Pimcore\Bundle\StudioBackendBundle\Class\Service\ObjectBrick\LayoutDefinitionService + Pimcore\Bundle\StudioBackendBundle\Class\Repository\QuantityValueRepositoryInterface: class: Pimcore\Bundle\StudioBackendBundle\Class\Repository\QuantityValueRepository @@ -27,4 +30,7 @@ services: # Hydrators # Pimcore\Bundle\StudioBackendBundle\Class\Hydrator\FieldCollection\LayoutDefinitionHydratorInterface: - class: Pimcore\Bundle\StudioBackendBundle\Class\Hydrator\FieldCollection\LayoutDefinitionHydrator \ No newline at end of file + class: Pimcore\Bundle\StudioBackendBundle\Class\Hydrator\FieldCollection\LayoutDefinitionHydrator + + Pimcore\Bundle\StudioBackendBundle\Class\Hydrator\ObjectBrick\LayoutDefinitionHydratorInterface: + class: Pimcore\Bundle\StudioBackendBundle\Class\Hydrator\ObjectBrick\LayoutDefinitionHydrator \ No newline at end of file diff --git a/src/Class/Controller/FieldCollection/LayoutController.php b/src/Class/Controller/FieldCollection/LayoutController.php index 64bd5e2f..2bc9ef84 100644 --- a/src/Class/Controller/FieldCollection/LayoutController.php +++ b/src/Class/Controller/FieldCollection/LayoutController.php @@ -18,7 +18,7 @@ use Exception; use OpenApi\Attributes\Get; -use Pimcore\Bundle\StudioBackendBundle\Class\Schema\LayoutDefinition; +use Pimcore\Bundle\StudioBackendBundle\Class\Schema\FieldCollection\LayoutDefinition; use Pimcore\Bundle\StudioBackendBundle\Class\Service\FieldCollection\LayoutDefinitionServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; diff --git a/src/Class/Controller/ObjectBrick/LayoutController.php b/src/Class/Controller/ObjectBrick/LayoutController.php new file mode 100644 index 00000000..eb210515 --- /dev/null +++ b/src/Class/Controller/ObjectBrick/LayoutController.php @@ -0,0 +1,82 @@ +value], + )] + #[IdParameter(name: 'objectId', required: true)] + #[SuccessResponse( + description: 'class_object_brick_object_layout_success_response', + content: new CollectionJson(new GenericCollection(LayoutDefinition::class)) + )] + #[DefaultResponses([ + HttpResponseCodes::NOT_FOUND, + ])] + public function getObjectBrickLayoutForObject(int $objectId): JsonResponse + { + $items = $this->layoutDefinitionService->getLayoutDefinitionsForObject($objectId); + + return $this->getPaginatedCollection( + $this->serializer, + $items, + count($items), + ); + } +} diff --git a/src/Class/Event/FieldCollection/LayoutDefinitionEvent.php b/src/Class/Event/FieldCollection/LayoutDefinitionEvent.php index 66d41765..ecd63f5d 100644 --- a/src/Class/Event/FieldCollection/LayoutDefinitionEvent.php +++ b/src/Class/Event/FieldCollection/LayoutDefinitionEvent.php @@ -16,7 +16,7 @@ namespace Pimcore\Bundle\StudioBackendBundle\Class\Event\FieldCollection; -use Pimcore\Bundle\StudioBackendBundle\Class\Schema\LayoutDefinition; +use Pimcore\Bundle\StudioBackendBundle\Class\Schema\FieldCollection\LayoutDefinition; use Pimcore\Bundle\StudioBackendBundle\Event\AbstractPreResponseEvent; final class LayoutDefinitionEvent extends AbstractPreResponseEvent diff --git a/src/Class/Event/ObjectBrick/LayoutDefinitionEvent.php b/src/Class/Event/ObjectBrick/LayoutDefinitionEvent.php new file mode 100644 index 00000000..fe890dc0 --- /dev/null +++ b/src/Class/Event/ObjectBrick/LayoutDefinitionEvent.php @@ -0,0 +1,35 @@ +layoutDefinition); + } + + public function getLayoutDefinition(): LayoutDefinition + { + return $this->layoutDefinition; + } +} diff --git a/src/Class/Hydrator/FieldCollection/LayoutDefinitionHydrator.php b/src/Class/Hydrator/FieldCollection/LayoutDefinitionHydrator.php index ddafa45c..7d9dc940 100644 --- a/src/Class/Hydrator/FieldCollection/LayoutDefinitionHydrator.php +++ b/src/Class/Hydrator/FieldCollection/LayoutDefinitionHydrator.php @@ -16,7 +16,7 @@ namespace Pimcore\Bundle\StudioBackendBundle\Class\Hydrator\FieldCollection; -use Pimcore\Bundle\StudioBackendBundle\Class\Schema\LayoutDefinition; +use Pimcore\Bundle\StudioBackendBundle\Class\Schema\FieldCollection\LayoutDefinition; use Pimcore\Model\DataObject\Fieldcollection\Definition as FieldCollectionDefinition; /** diff --git a/src/Class/Hydrator/FieldCollection/LayoutDefinitionHydratorInterface.php b/src/Class/Hydrator/FieldCollection/LayoutDefinitionHydratorInterface.php index f0691955..45a25318 100644 --- a/src/Class/Hydrator/FieldCollection/LayoutDefinitionHydratorInterface.php +++ b/src/Class/Hydrator/FieldCollection/LayoutDefinitionHydratorInterface.php @@ -16,7 +16,7 @@ namespace Pimcore\Bundle\StudioBackendBundle\Class\Hydrator\FieldCollection; -use Pimcore\Bundle\StudioBackendBundle\Class\Schema\LayoutDefinition; +use Pimcore\Bundle\StudioBackendBundle\Class\Schema\FieldCollection\LayoutDefinition; use Pimcore\Model\DataObject\Fieldcollection\Definition as FieldCollectionDefinition; /** diff --git a/src/Class/Hydrator/ObjectBrick/LayoutDefinitionHydrator.php b/src/Class/Hydrator/ObjectBrick/LayoutDefinitionHydrator.php new file mode 100644 index 00000000..7de93ac7 --- /dev/null +++ b/src/Class/Hydrator/ObjectBrick/LayoutDefinitionHydrator.php @@ -0,0 +1,45 @@ +getLayoutDefinitions(); + + return new LayoutDefinition( + $data->getKey(), + $layout->getDatatype(), + $layout->getName(), + $layout->getType(), + $layout->getRegion(), + $layout->getTitle(), + $layout->getWidth(), + $layout->getHeight(), + $layout->getCollapsible(), + $layout->getCollapsed(), + $layout->getChildren(), + ); + } +} diff --git a/src/Class/Hydrator/ObjectBrick/LayoutDefinitionHydratorInterface.php b/src/Class/Hydrator/ObjectBrick/LayoutDefinitionHydratorInterface.php new file mode 100644 index 00000000..789b1e50 --- /dev/null +++ b/src/Class/Hydrator/ObjectBrick/LayoutDefinitionHydratorInterface.php @@ -0,0 +1,28 @@ +key; + } + + public function getName(): ?string + { + return $this->name; + } + + public function getType(): ?string + { + return $this->type; + } + + public function getRegion(): ?string + { + return $this->region; + } + + public function getTitle(): ?string + { + return $this->title; + } + + public function getWidth(): int + { + return $this->width; + } + + public function getHeight(): int + { + return $this->height; + } + + public function isCollapsible(): bool + { + return $this->collapsible; + } + + public function isCollapsed(): bool + { + return $this->collapsed; + } + + public function getDatatype(): string + { + return $this->datatype; + } + + public function getChildren(): array + { + return $this->children; + } +} diff --git a/src/Class/Service/FieldCollection/LayoutDefinitionService.php b/src/Class/Service/FieldCollection/LayoutDefinitionService.php index 7aa5b9ad..c382424b 100644 --- a/src/Class/Service/FieldCollection/LayoutDefinitionService.php +++ b/src/Class/Service/FieldCollection/LayoutDefinitionService.php @@ -22,9 +22,9 @@ use Pimcore\Bundle\StaticResolverBundle\Models\DataObject\FieldCollection\DefinitionResolverInterface; use Pimcore\Bundle\StudioBackendBundle\Class\Event\FieldCollection\LayoutDefinitionEvent; use Pimcore\Bundle\StudioBackendBundle\Class\Hydrator\FieldCollection\LayoutDefinitionHydratorInterface; -use Pimcore\Bundle\StudioBackendBundle\Class\Schema\LayoutDefinition; +use Pimcore\Bundle\StudioBackendBundle\Class\Schema\FieldCollection\LayoutDefinition; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; -use Pimcore\Model\DataObject; +use Pimcore\Model\DataObject\ClassDefinition\Data\Fieldcollections; use Pimcore\Model\DataObject\ClassDefinitionInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -91,7 +91,7 @@ private function getLayoutDefinitionByType(string $name): LayoutDefinition private function collectFieldCollectionTypes(ClassDefinitionInterface $classDefinition): void { foreach ($classDefinition->getFieldDefinitions() as $fieldDefinition) { - if ($fieldDefinition instanceof DataObject\ClassDefinition\Data\Fieldcollections) { + if ($fieldDefinition instanceof Fieldcollections) { $this->fieldCollectionTypes = [...$this->fieldCollectionTypes, ...$fieldDefinition->getAllowedTypes()]; } } diff --git a/src/Class/Service/FieldCollection/LayoutDefinitionServiceInterface.php b/src/Class/Service/FieldCollection/LayoutDefinitionServiceInterface.php index d8ea9c67..ccbf8424 100644 --- a/src/Class/Service/FieldCollection/LayoutDefinitionServiceInterface.php +++ b/src/Class/Service/FieldCollection/LayoutDefinitionServiceInterface.php @@ -17,6 +17,7 @@ namespace Pimcore\Bundle\StudioBackendBundle\Class\Service\FieldCollection; use Exception; +use Pimcore\Bundle\StudioBackendBundle\Class\Schema\FieldCollection\LayoutDefinition; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; /** @@ -26,6 +27,8 @@ interface LayoutDefinitionServiceInterface { /** * @throws NotFoundException|Exception + * + * @return LayoutDefinition[] */ public function getLayoutDefinitionsForObject(int $dataObjectId): array; } diff --git a/src/Class/Service/ObjectBrick/LayoutDefinitionService.php b/src/Class/Service/ObjectBrick/LayoutDefinitionService.php new file mode 100644 index 00000000..f1906354 --- /dev/null +++ b/src/Class/Service/ObjectBrick/LayoutDefinitionService.php @@ -0,0 +1,96 @@ +dataObjectResolver->getById($dataObjectId); + + $classDef = $this->classDefinitionResolver->getById($dataObject->getClassId()); + + $this->collectFieldCollectionTypes($classDef); + + $layoutDefinitions = []; + foreach ($this->objectBrickTypes as $type) { + $layoutDefinitions[] = $this->getLayoutDefinitionByType($type); + } + + return $layoutDefinitions; + + } + + /** + * @throws Exception + */ + private function getLayoutDefinitionByType(string $name): LayoutDefinition + { + $definition = $this->definitionResolver->getByKey($name); + + if (!$definition) { + throw new NotFoundException('Object Brick Definition', $name); + } + + $layoutDefinition = $this->layoutDefinitionHydrator->hydrate($definition); + + $this->eventDispatcher->dispatch( + new LayoutDefinitionEvent($layoutDefinition), + LayoutDefinitionEvent::EVENT_NAME + ); + + return $layoutDefinition; + } + + private function collectFieldCollectionTypes(ClassDefinitionInterface $classDefinition): void + { + foreach ($classDefinition->getFieldDefinitions() as $fieldDefinition) { + if ($fieldDefinition instanceof Objectbricks) { + $this->objectBrickTypes = [...$this->objectBrickTypes, ...$fieldDefinition->getAllowedTypes()]; + } + } + } +} diff --git a/src/Class/Service/ObjectBrick/LayoutDefinitionServiceInterface.php b/src/Class/Service/ObjectBrick/LayoutDefinitionServiceInterface.php new file mode 100644 index 00000000..7a03dc47 --- /dev/null +++ b/src/Class/Service/ObjectBrick/LayoutDefinitionServiceInterface.php @@ -0,0 +1,34 @@ +