diff --git a/DomainContent/FieldValueBuilder/BaseFieldValueBuilder.php b/DomainContent/FieldValueBuilder/BaseFieldValueBuilder.php index 89611d8..ae4c95b 100644 --- a/DomainContent/FieldValueBuilder/BaseFieldValueBuilder.php +++ b/DomainContent/FieldValueBuilder/BaseFieldValueBuilder.php @@ -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', diff --git a/DomainContent/FieldValueBuilder/ImageAssetFieldValueBuilder.php b/DomainContent/FieldValueBuilder/ImageAssetFieldValueBuilder.php new file mode 100644 index 0000000..808c0b3 --- /dev/null +++ b/DomainContent/FieldValueBuilder/ImageAssetFieldValueBuilder.php @@ -0,0 +1,52 @@ +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]; + } +} \ No newline at end of file diff --git a/GraphQL/Resolver/DomainContentResolver.php b/GraphQL/Resolver/DomainContentResolver.php index 8ccf69a..eea8681 100644 --- a/GraphQL/Resolver/DomainContentResolver.php +++ b/GraphQL/Resolver/DomainContentResolver.php @@ -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; @@ -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"); } diff --git a/GraphQL/Resolver/ImageAssetFieldResolver.php b/GraphQL/Resolver/ImageAssetFieldResolver.php new file mode 100644 index 0000000..7923f30 --- /dev/null +++ b/GraphQL/Resolver/ImageAssetFieldResolver.php @@ -0,0 +1,55 @@ +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, + ]); + } +} \ No newline at end of file diff --git a/Resources/config/domain_content.yml b/Resources/config/domain_content.yml index 86a85f4..ae23070 100644 --- a/Resources/config/domain_content.yml +++ b/Resources/config/domain_content.yml @@ -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'} diff --git a/Resources/config/resolvers.yml b/Resources/config/resolvers.yml index 2aae9e8..1410a9d 100644 --- a/Resources/config/resolvers.yml +++ b/Resources/config/resolvers.yml @@ -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"} \ No newline at end of file