Skip to content

Commit

Permalink
Implemented ImageAsset field support
Browse files Browse the repository at this point in the history
ImageAsset fields will behave like a regular image.
  • Loading branch information
Bertrand Dunogier committed Nov 5, 2018
1 parent 5e4811a commit e50d761
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 2 deletions.
1 change: 1 addition & 0 deletions DomainContent/FieldValueBuilder/BaseFieldValueBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class BaseFieldValueBuilder implements FieldValueBuilder
'ezfloat' => ['Float', '@=resolver("DomainFieldValue", [value, "%s"]).value'],
'ezgmaplocation' => 'MapLocationFieldValue',
'ezimage' => 'ImageFieldValue',
'ezimageasset' => ['ImageFieldValue', '@=resolver("DomainImageAssetFieldValue", [value, "%s"]).value'],
'ezinteger' => ['Int', '@=resolver("DomainFieldValue", [value, "%s"]).value'],
'ezkeyword' => ['[String]', '@=resolver("DomainFieldValue", [value, "%s"]).values'],
'ezmedia' => 'MediaFieldValue',
Expand Down
52 changes: 52 additions & 0 deletions DomainContent/FieldValueBuilder/ImageAssetFieldValueBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace BD\EzPlatformGraphQLBundle\DomainContent\FieldValueBuilder;

use BD\EzPlatformGraphQLBundle\DomainContent\NameHelper;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;

class ImageAssetFieldValueBuilder implements FieldValueBuilder
{
/**
* @var NameHelper
*/
private $nameHelper;

/**
* @var ContentTypeService
*/
private $contentTypeService;

public function __construct(NameHelper $nameHelper, ContentTypeService $contentTypeService)
{
$this->nameHelper = $nameHelper;
$this->contentTypeService = $contentTypeService;
}

public function buildDefinition(FieldDefinition $fieldDefinition)
{
$settings = $fieldDefinition->getFieldSettings();
$constraints = $fieldDefinition->getValidatorConfiguration();

if (isset($settings['selectionContentTypes']) && count($settings['selectionContentTypes']) === 1) {
$contentType = $this->contentTypeService->loadContentTypeByIdentifier($settings['selectionContentTypes'][0]);
$type = $this->nameHelper->domainContentName($contentType);
} else {
$type = 'DomainContent';
}

$isMultiple = false;
if (isset($constraints['RelationListValueValidator']['selectionLimit']) && $constraints['RelationListValueValidator']['selectionLimit'] !== 1) {
$isMultiple = 'true';
$type = "[$type]";
}

$resolver = sprintf(
'@=resolver("DomainRelationFieldValue", [value, "%s", %s])',
$fieldDefinition->identifier,
$isMultiple
);

return ['type' => $type, 'resolve' => $resolver];
}
}
4 changes: 2 additions & 2 deletions GraphQL/Resolver/DomainContentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use BD\EzPlatformGraphQLBundle\GraphQL\InputMapper\SearchQueryMapper;
use BD\EzPlatformGraphQLBundle\GraphQL\Value\ContentFieldValue;
use eZ\Publish\Core\FieldType\RelationList\Value as RelationListValue;
use eZ\Publish\Core\FieldType;
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\SearchService;
Expand Down Expand Up @@ -139,7 +139,7 @@ public function resolveDomainRelationFieldValue($contentInfo, $fieldDefinitionId
// @todo check content type
$fieldValue = $content->getFieldValue($fieldDefinitionIdentifier);

if (!$fieldValue instanceof RelationListValue) {
if (!$fieldValue instanceof FieldType\RelationList\Value) {
throw new UserError("$fieldDefinitionIdentifier is not a RelationList field value");
}

Expand Down
55 changes: 55 additions & 0 deletions GraphQL/Resolver/ImageAssetFieldResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace BD\EzPlatformGraphQLBundle\GraphQL\Resolver;

use BD\EzPlatformGraphQLBundle\GraphQL\Value\ContentFieldValue;
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\Core\FieldType\ImageAsset\AssetMapper;
use eZ\Publish\Core\FieldType\ImageAsset\Value as ImageAssetValue;
use Overblog\GraphQLBundle\Error\UserError;

class ImageAssetFieldResolver
{
/**
* @var DomainContentResolver
*/
private $domainContentResolver;
/**
* @var ContentService
*/
private $contentService;
/**
* @var AssetMapper
*/
private $assetMapper;

public function __construct(ContentService $contentService, DomainContentResolver $domainContentResolver, AssetMapper $assetMapper)
{
$this->domainContentResolver = $domainContentResolver;
$this->contentService = $contentService;
$this->assetMapper = $assetMapper;
}

public function resolveDomainImageAssetFieldValue($contentInfo, $fieldDefinitionIdentifier)
{
$contentFieldValue = $this->domainContentResolver->resolveDomainFieldValue($contentInfo, $fieldDefinitionIdentifier);

if (!$contentFieldValue->value instanceof ImageAssetValue) {
throw new UserError("$fieldDefinitionIdentifier is not an image asset field");
}

$assetValue = $this->assetMapper->getAssetValue(
$this->contentService->loadContent($contentFieldValue->value->destinationContentId)
);

if (empty($assetValue->alternativeText)) {
$assetValue->alternativeText = $contentFieldValue->value->alternativeText;
}

return new ContentFieldValue([
'contentTypeId' => $contentFieldValue->contentTypeId,
'fieldDefIdentifier' => $contentFieldValue->fieldDefIdentifier,
'content' => $contentFieldValue->content,
'value' => $assetValue,
]);
}
}
4 changes: 4 additions & 0 deletions Resources/config/domain_content.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ services:
BD\EzPlatformGraphQLBundle\DomainContent\FieldValueBuilder\RelationListFieldValueBuilder:
tags:
- {name: ezplatform_graphql.field_value_builder, type: 'ezobjectrelationlist'}

#BD\EzPlatformGraphQLBundle\DomainContent\FieldValueBuilder\ImageAssetFieldValueBuilder:
# tags:
# - {name: ezplatform_graphql.field_value_builder, type: 'ezimageasset'}
3 changes: 3 additions & 0 deletions Resources/config/resolvers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,6 @@ services:
tags:
- { name: overblog_graphql.resolver, alias: "DateTimeFormat", method: "resolveDateToFormat"}

BD\EzPlatformGraphQLBundle\GraphQL\Resolver\ImageAssetFieldResolver:
tags:
- { name: overblog_graphql.resolver, alias: "DomainImageAssetFieldValue", method: "resolveDomainImageAssetFieldValue"}

0 comments on commit e50d761

Please sign in to comment.