Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-6491: Implemented strategy for resolving Content Type based on FieldTypeDefinition #35

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/bundle/Resources/config/services/fieldtype.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: -20 }
1 change: 1 addition & 0 deletions src/bundle/Resources/config/services/graphql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ~

Expand Down
40 changes: 40 additions & 0 deletions src/lib/FieldType/Mapper/FieldTypeToContentTypeStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\FieldTypeMatrix\FieldType\Mapper;

use Ibexa\Contracts\Core\Repository\ContentTypeService;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;

final class FieldTypeToContentTypeStrategy implements FieldTypeToContentTypeStrategyInterface
{
private ContentTypeService $contentTypeService;

public function __construct(ContentTypeService $contentTypeService)
{
$this->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;
}
}
}
webhdx marked this conversation as resolved.
Show resolved Hide resolved

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\FieldTypeMatrix\FieldType\Mapper;

use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;

interface FieldTypeToContentTypeStrategyInterface
{
public function findContentTypeOf(FieldDefinition $fieldDefinition): ?ContentType;
}
52 changes: 30 additions & 22 deletions src/lib/GraphQL/Schema/MatrixFieldDefinitionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
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\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\DecoratingFieldDefinitionMapper;
use Ibexa\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\FieldDefinitionInputMapper;

Expand All @@ -23,11 +24,23 @@ class MatrixFieldDefinitionMapper extends DecoratingFieldDefinitionMapper implem
/** @var \Ibexa\Contracts\Core\Repository\ContentTypeService */
private $contentTypeService;

public function __construct(FieldDefinitionMapper $innerMapper, NameHelper $nameHelper, ContentTypeService $contentTypeService)
{
/* @var iterable<\Ibexa\FieldTypeMatrix\FieldType\Mapper\FieldTypeToContentTypeStrategyInterface> */
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;
$this->strategies = $strategies;
}

public function mapToFieldDefinitionType(FieldDefinition $fieldDefinition): ?string
Expand All @@ -46,9 +59,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
);
}

Expand All @@ -75,23 +100,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');
Loading