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-8006: Fixed matrix field resolving based on content's origin #43

Merged
merged 2 commits into from
Mar 28, 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
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
},
"autoload": {
"psr-4": {
"EzSystems\\EzPlatformMatrixFieldtypeBundle\\": "src/bundle/",
"EzSystems\\EzPlatformMatrixFieldtype\\": "src/lib/",
"Ibexa\\FieldTypeMatrix\\": "src/lib/",
"Ibexa\\Bundle\\FieldTypeMatrix\\": "src/bundle/"
"Ibexa\\Bundle\\FieldTypeMatrix\\": "src/bundle/",
"EzSystems\\EzPlatformMatrixFieldtypeBundle\\": "src/bundle/",
"EzSystems\\EzPlatformMatrixFieldtype\\": "src/lib/"
}
},
"autoload-dev": {
Expand Down Expand Up @@ -57,5 +57,8 @@
"branch-alias": {
"dev-main": "4.5.x-dev"
}
},
"config": {
"allow-plugins": false
}
}
8 changes: 7 additions & 1 deletion src/bundle/Resources/config/services/graphql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ services:
- { name: ibexa.graphql.field_type.input.handler, fieldtype: ezmatrix }

Ibexa\FieldTypeMatrix\GraphQL\FieldValueResolver:
arguments:
$strategies: !tagged_iterator ibexa.graphql.field_type.matrix_resolver.content.strategy
tags:
- { name: overblog_graphql.resolver, alias: "MatrixFieldValue", method: "resolveMatrixFieldValue" }

Ibexa\FieldTypeMatrix\GraphQL\Strategy\ItemContentResolvingStrategy:
tags:
- {name: overblog_graphql.resolver, alias: "MatrixFieldValue", method: "resolveMatrixFieldValue"}
- { name: ibexa.graphql.field_type.matrix_resolver.content.strategy, priority: -20 }
37 changes: 34 additions & 3 deletions src/lib/GraphQL/FieldValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,48 @@

namespace Ibexa\FieldTypeMatrix\GraphQL;

use Ibexa\Core\Base\Exceptions\BadStateException;
use Ibexa\FieldTypeMatrix\FieldType\Value\RowsCollection;
use Ibexa\GraphQL\Value\Item;

class FieldValueResolver
{
public function resolveMatrixFieldValue(Item $item, string $fieldDefIdentifier): RowsCollection
/** @var iterable<\Ibexa\FieldTypeMatrix\GraphQL\Strategy\ContentResolvingStrategyInterface> */
private iterable $strategies;

/**
* @param iterable<\Ibexa\FieldTypeMatrix\GraphQL\Strategy\ContentResolvingStrategyInterface> $strategies
*/
public function __construct(iterable $strategies)
{
$this->strategies = $strategies;
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
*/
public function resolveMatrixFieldValue(object $item, string $fieldDefIdentifier): RowsCollection
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
{
$silentRows = [];
$content = null;

foreach ($this->strategies as $strategy) {
if (!$strategy->supports($item)) {
continue;
}

$content = $strategy->resolveContent($item);
}

if ($content === null) {
throw new BadStateException(
'$item',
'GraphQL item cannot be resolved to a content.'
);
}

/** @var \Ibexa\FieldTypeMatrix\FieldType\Value\RowsCollection $rows $rows */
$rows = $item->getContent()->getFieldValue($fieldDefIdentifier)->getRows();
$rows = $content->getFieldValue($fieldDefIdentifier)->getRows();
foreach ($rows as $row) {
$silentRows[] = new SilentRow($row->getCells());
}
Expand Down
18 changes: 18 additions & 0 deletions src/lib/GraphQL/Strategy/ContentResolvingStrategyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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\GraphQL\Strategy;

use Ibexa\Contracts\Core\Repository\Values\Content\Content;

interface ContentResolvingStrategyInterface
{
public function resolveContent(object $item): Content;

public function supports(object $item): bool;
}
26 changes: 26 additions & 0 deletions src/lib/GraphQL/Strategy/ItemContentResolvingStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\GraphQL\Strategy;

use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\GraphQL\Value\Item;

final class ItemContentResolvingStrategy implements ContentResolvingStrategyInterface
{
public function resolveContent(object $item): Content
{
/** @var \Ibexa\GraphQL\Value\Item $item */
return $item->getContent();
}

public function supports(object $item): bool
{
return $item instanceof Item;
}
}
Loading