From d5515f73cd6cdf82d33ee15cc67b15a60e5c0277 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Wed, 15 Nov 2023 17:53:30 +0100 Subject: [PATCH 1/3] IBX-6491: Implemented strategy for resolving Content Type based on `FieldTypeDefinition` --- .../Resources/config/services/fieldtype.yaml | 6 ++ .../Resources/config/services/graphql.yaml | 1 + .../Mapper/FieldTypeToContentTypeStrategy.php | 40 +++++++++++++ ...ieldTypeToContentTypeStrategyInterface.php | 17 ++++++ .../Schema/MatrixFieldDefinitionMapper.php | 57 ++++++++++++------- 5 files changed, 99 insertions(+), 22 deletions(-) create mode 100644 src/lib/FieldType/Mapper/FieldTypeToContentTypeStrategy.php create mode 100644 src/lib/FieldType/Mapper/FieldTypeToContentTypeStrategyInterface.php diff --git a/src/bundle/Resources/config/services/fieldtype.yaml b/src/bundle/Resources/config/services/fieldtype.yaml index 39173db..f210d7c 100644 --- a/src/bundle/Resources/config/services/fieldtype.yaml +++ b/src/bundle/Resources/config/services/fieldtype.yaml @@ -29,3 +29,9 @@ services: Ibexa\FieldTypeMatrix\FieldType\Indexable: tags: - { name: ibexa.field_type.indexable, alias: '%ibexa.field_type.matrix.identifier%' } + + Ibexa\FieldTypeMatrix\FieldType\Mapper\FieldTypeToContentTypeStrategy: + arguments: + $contentTypeService: '@ibexa.api.service.content_type' + tags: + - { name: ibexa.graphql.field_type.matrix_mapper.content_type.strategy, priority: 10 } diff --git a/src/bundle/Resources/config/services/graphql.yaml b/src/bundle/Resources/config/services/graphql.yaml index 675e63d..b755d1a 100644 --- a/src/bundle/Resources/config/services/graphql.yaml +++ b/src/bundle/Resources/config/services/graphql.yaml @@ -8,6 +8,7 @@ services: decorates: Ibexa\Contracts\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\FieldDefinitionMapper arguments: $innerMapper: '@Ibexa\FieldTypeMatrix\GraphQL\Schema\MatrixFieldDefinitionMapper.inner' + $strategies: !tagged_iterator ibexa.graphql.field_type.matrix_mapper.content_type.strategy Ibexa\FieldTypeMatrix\GraphQL\Schema\NameHelper: ~ diff --git a/src/lib/FieldType/Mapper/FieldTypeToContentTypeStrategy.php b/src/lib/FieldType/Mapper/FieldTypeToContentTypeStrategy.php new file mode 100644 index 0000000..9592d06 --- /dev/null +++ b/src/lib/FieldType/Mapper/FieldTypeToContentTypeStrategy.php @@ -0,0 +1,40 @@ +contentTypeService = $contentTypeService; + } + + public function findContentTypeOf(FieldDefinition $fieldDefinition): ?ContentType + { + foreach ($this->contentTypeService->loadContentTypeGroups() as $group) { + foreach ($this->contentTypeService->loadContentTypes($group) as $type) { + $foundFieldDefinition = $type->getFieldDefinition($fieldDefinition->identifier); + if ($foundFieldDefinition === null) { + continue; + } + if ($foundFieldDefinition->id === $fieldDefinition->id) { + return $type; + } + } + } + + return null; + } +} diff --git a/src/lib/FieldType/Mapper/FieldTypeToContentTypeStrategyInterface.php b/src/lib/FieldType/Mapper/FieldTypeToContentTypeStrategyInterface.php new file mode 100644 index 0000000..9543e75 --- /dev/null +++ b/src/lib/FieldType/Mapper/FieldTypeToContentTypeStrategyInterface.php @@ -0,0 +1,17 @@ + */ + private iterable $strategies; + + /** + * @param iterable<\Ibexa\FieldTypeMatrix\FieldType\Mapper\FieldTypeToContentTypeStrategyInterface> $strategies + */ + public function __construct( + FieldDefinitionMapper $innerMapper, + NameHelper $nameHelper, + ContentTypeService $contentTypeService, + iterable $strategies + ) { parent::__construct($innerMapper); $this->nameHelper = $nameHelper; $this->contentTypeService = $contentTypeService; + + foreach ($strategies as $strategy) { + if ($strategy instanceof FieldTypeToContentTypeStrategyInterface) { + $this->strategies[] = $strategy; + } + } } public function mapToFieldDefinitionType(FieldDefinition $fieldDefinition): ?string @@ -46,9 +64,21 @@ public function mapToFieldValueType(FieldDefinition $fieldDefinition): ?string return parent::mapToFieldValueType($fieldDefinition); } - return sprintf( - '[%s]', - $this->nameHelper->matrixFieldDefinitionType($this->findContentTypeOf($fieldDefinition), $fieldDefinition) + foreach ($this->strategies as $strategy) { + $contentType = $strategy->findContentTypeOf($fieldDefinition); + if ($contentType === null) { + continue; + } + + return sprintf( + '[%s]', + $this->nameHelper->matrixFieldDefinitionType($contentType, $fieldDefinition) + ); + } + + throw new NotFoundException( + 'Could not find content type for field definition', + $fieldDefinition->identifier ); } @@ -75,23 +105,6 @@ public function mapToFieldValueResolver(FieldDefinition $fieldDefinition): ?stri $fieldDefinition->identifier ); } - - private function findContentTypeOf(FieldDefinition $fieldDefinition): ContentType - { - foreach ($this->contentTypeService->loadContentTypeGroups() as $group) { - foreach ($this->contentTypeService->loadContentTypes($group) as $type) { - $foundFieldDefinition = $type->getFieldDefinition($fieldDefinition->identifier); - if ($foundFieldDefinition === null) { - continue; - } - if ($foundFieldDefinition->id === $fieldDefinition->id) { - return $type; - } - } - } - - throw new \Exception('Could not find content type for field definition'); - } } class_alias(MatrixFieldDefinitionMapper::class, 'EzSystems\EzPlatformMatrixFieldtype\GraphQL\Schema\MatrixFieldDefinitionMapper'); From 17c8e432cd9520deafab5a46c33edecf8e1a5149 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Tue, 28 Nov 2023 15:21:09 +0100 Subject: [PATCH 2/3] IBX-6491: Applied review remarks --- src/bundle/Resources/config/services/fieldtype.yaml | 2 +- src/lib/GraphQL/Schema/MatrixFieldDefinitionMapper.php | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/bundle/Resources/config/services/fieldtype.yaml b/src/bundle/Resources/config/services/fieldtype.yaml index f210d7c..ac2eea2 100644 --- a/src/bundle/Resources/config/services/fieldtype.yaml +++ b/src/bundle/Resources/config/services/fieldtype.yaml @@ -34,4 +34,4 @@ services: arguments: $contentTypeService: '@ibexa.api.service.content_type' tags: - - { name: ibexa.graphql.field_type.matrix_mapper.content_type.strategy, priority: 10 } + - { name: ibexa.graphql.field_type.matrix_mapper.content_type.strategy, priority: -20 } diff --git a/src/lib/GraphQL/Schema/MatrixFieldDefinitionMapper.php b/src/lib/GraphQL/Schema/MatrixFieldDefinitionMapper.php index b76f6cc..ed8ba4e 100644 --- a/src/lib/GraphQL/Schema/MatrixFieldDefinitionMapper.php +++ b/src/lib/GraphQL/Schema/MatrixFieldDefinitionMapper.php @@ -38,14 +38,10 @@ public function __construct( iterable $strategies ) { parent::__construct($innerMapper); + $this->nameHelper = $nameHelper; $this->contentTypeService = $contentTypeService; - - foreach ($strategies as $strategy) { - if ($strategy instanceof FieldTypeToContentTypeStrategyInterface) { - $this->strategies[] = $strategy; - } - } + $this->strategies = $strategies; } public function mapToFieldDefinitionType(FieldDefinition $fieldDefinition): ?string From 2b93ba066de51976465abd5de03569b8a4e4ae56 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Tue, 28 Nov 2023 15:23:16 +0100 Subject: [PATCH 3/3] IBX-6491: CS --- src/lib/GraphQL/Schema/MatrixFieldDefinitionMapper.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/GraphQL/Schema/MatrixFieldDefinitionMapper.php b/src/lib/GraphQL/Schema/MatrixFieldDefinitionMapper.php index ed8ba4e..034d94d 100644 --- a/src/lib/GraphQL/Schema/MatrixFieldDefinitionMapper.php +++ b/src/lib/GraphQL/Schema/MatrixFieldDefinitionMapper.php @@ -13,7 +13,6 @@ use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition; use Ibexa\Contracts\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\FieldDefinitionMapper; use Ibexa\Core\Base\Exceptions\NotFoundException; -use Ibexa\FieldTypeMatrix\FieldType\Mapper\FieldTypeToContentTypeStrategyInterface; use Ibexa\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\DecoratingFieldDefinitionMapper; use Ibexa\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\FieldDefinitionInputMapper; @@ -38,7 +37,7 @@ public function __construct( iterable $strategies ) { parent::__construct($innerMapper); - + $this->nameHelper = $nameHelper; $this->contentTypeService = $contentTypeService; $this->strategies = $strategies;