-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented ImageAsset field support
ImageAsset fields will behave like a regular image.
- Loading branch information
Bertrand Dunogier
committed
Nov 5, 2018
1 parent
5e4811a
commit e50d761
Showing
6 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
DomainContent/FieldValueBuilder/ImageAssetFieldValueBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters