Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Core/FieldType/ImageAsset/Type.php
Go to the documentation of this file.
1 <?php
2
7 declare(strict_types=1);
8
9 namespace eZ\Publish\Core\FieldType\ImageAsset;
10
11 use eZ\Publish\API\Repository\ContentService;
12 use eZ\Publish\API\Repository\ContentTypeService;
13 use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
14 use eZ\Publish\Core\FieldType\FieldType;
15 use eZ\Publish\API\Repository\Values\Content\ContentInfo;
16 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
17 use eZ\Publish\API\Repository\Values\Content\Relation;
18 use eZ\Publish\Core\FieldType\ValidationError;
19 use eZ\Publish\SPI\FieldType\Value as SPIValue;
20 use eZ\Publish\Core\FieldType\Value as BaseValue;
21
22 class Type extends FieldType
23 {
24 const FIELD_TYPE_IDENTIFIER = 'ezimageasset';
25
27 private $contentService;
28
30 private $contentTypeService;
31
33 private $assetMapper;
34
40 public function __construct(
41 ContentService $contentService,
42 ContentTypeService $contentTypeService,
43 AssetMapper $mapper
44 ) {
45 $this->contentService = $contentService;
46 $this->contentTypeService = $contentTypeService;
47 $this->assetMapper = $mapper;
48 }
49
58 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue): array
59 {
60 $errors = [];
61
62 if ($this->isEmptyValue($fieldValue)) {
63 return $errors;
64 }
65
66 $content = $this->contentService->loadContent($fieldValue->destinationContentId);
67
68 if (!$this->assetMapper->isAsset($content)) {
69 $currentContentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId);
70
71 $errors[] = new ValidationError(
72 'Content %type% is not a valid asset target',
73 null,
74 [
75 '%type%' => $currentContentType->identifier,
76 ],
77 'destinationContentId'
78 );
79 }
80
81 return $errors;
82 }
83
89 public function getFieldTypeIdentifier(): string
90 {
91 return self::FIELD_TYPE_IDENTIFIER;
92 }
93
104 public function getName(SPIValue $value): string
105 {
106 throw new \RuntimeException('Name generation provided via NameableField set via "ezpublish.fieldType.nameable" service tag');
107 }
108
115 public function getEmptyValue(): Value
116 {
117 return new Value();
118 }
119
127 public function isEmptyValue(SPIValue $value): bool
128 {
129 return null === $value->destinationContentId;
130 }
131
139 protected function createValueFromInput($inputValue)
140 {
141 if ($inputValue instanceof ContentInfo) {
142 $inputValue = new Value($inputValue->id);
143 } elseif (is_int($inputValue) || is_string($inputValue)) {
144 $inputValue = new Value($inputValue);
145 }
146
147 return $inputValue;
148 }
149
157 protected function checkValueStructure(BaseValue $value): void
158 {
159 if (!is_int($value->destinationContentId) && !is_string($value->destinationContentId)) {
160 throw new InvalidArgumentType(
161 '$value->destinationContentId',
162 'string|int',
163 $value->destinationContentId
164 );
165 }
166
167 if ($value->alternativeText !== null && !is_string($value->alternativeText)) {
168 throw new InvalidArgumentType(
169 '$value->alternativeText',
170 'string|null',
171 $value->alternativeText
172 );
173 }
174 }
175
184 protected function getSortInfo(BaseValue $value): bool
185 {
186 return false;
187 }
188
196 public function fromHash($hash): Value
197 {
198 if ($hash) {
199 return new Value($hash['destinationContentId'], $hash['alternativeText']);
200 }
201
202 return new Value();
203 }
204
212 public function toHash(SPIValue $value): array
213 {
214 return [
215 'destinationContentId' => $value->destinationContentId,
216 'alternativeText' => $value->alternativeText,
217 ];
218 }
219
245 public function getRelations(SPIValue $fieldValue): array
246 {
247 $relations = [];
248 if ($fieldValue->destinationContentId !== null) {
249 $relations[Relation::ASSET] = [$fieldValue->destinationContentId];
250 }
251
252 return $relations;
253 }
254
260 public function isSearchable(): bool
261 {
262 return true;
263 }
264 }
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition
Definition: API/Repository/Values/ContentType/FieldDefinition.php:31
eZ\Publish\API\Repository\Values\Content\ContentInfo
Definition: API/Repository/Values/Content/ContentInfo.php:31
eZ\Publish\API\Repository\Values\Content\Query\Criterion\Value
Struct that stores extra value information for a Criterion object.
Definition: API/Repository/Values/Content/Query/Criterion/Value.php:13
eZ\Publish\API\Repository\Values\Content\Relation
Definition: API/Repository/Values/Content/Relation.php:21
eZ\Publish\API\Repository\Values\Content\Relation\ASSET
const ASSET
Definition: API/Repository/Values/Content/Relation.php:55
eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
Invalid Argument Type Exception implementation.
Definition: InvalidArgumentType.php:17
eZ\Publish\Core\FieldType\FieldType
Base class for field types, the most basic storage unit of data inside eZ Publish.
Definition: Core/FieldType/FieldType.php:35
eZ\Publish\Core\FieldType\ImageAsset\AssetMapper
Definition: AssetMapper.php:21
eZ\Publish\Core\FieldType\ImageAsset\Type
Definition: Core/FieldType/ImageAsset/Type.php:23
eZ\Publish\Core\FieldType\ImageAsset\Type\getFieldTypeIdentifier
getFieldTypeIdentifier()
Returns the field type identifier for this field type.
Definition: Core/FieldType/ImageAsset/Type.php:89
eZ\Publish\Core\FieldType\ImageAsset\Type\isEmptyValue
isEmptyValue(SPIValue $value)
Returns if the given $value is considered empty by the field type.
Definition: Core/FieldType/ImageAsset/Type.php:127
eZ\Publish\Core\FieldType\ImageAsset\Type\checkValueStructure
checkValueStructure(BaseValue $value)
Throws an exception if value structure is not of expected format.
Definition: Core/FieldType/ImageAsset/Type.php:157
eZ\Publish\Core\FieldType\ImageAsset\Type\FIELD_TYPE_IDENTIFIER
const FIELD_TYPE_IDENTIFIER
Definition: Core/FieldType/ImageAsset/Type.php:24
eZ\Publish\Core\FieldType\ImageAsset\Type\getSortInfo
getSortInfo(BaseValue $value)
Returns information for FieldValue->$sortKey relevant to the field type.
Definition: Core/FieldType/ImageAsset/Type.php:184
eZ\Publish\Core\FieldType\ImageAsset\Type\validate
validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
Validates a field based on the validators in the field definition.
Definition: Core/FieldType/ImageAsset/Type.php:58
eZ\Publish\Core\FieldType\ImageAsset\Type\getRelations
getRelations(SPIValue $fieldValue)
Returns relation data extracted from value.
Definition: Core/FieldType/ImageAsset/Type.php:245
eZ\Publish\Core\FieldType\ImageAsset\Type\createValueFromInput
createValueFromInput($inputValue)
Inspects given $inputValue and potentially converts it into a dedicated value object.
Definition: Core/FieldType/ImageAsset/Type.php:139
eZ\Publish\Core\FieldType\ImageAsset\Type\$assetMapper
$assetMapper
Definition: Core/FieldType/ImageAsset/Type.php:33
eZ\Publish\Core\FieldType\ImageAsset\Type\isSearchable
isSearchable()
Returns whether the field type is searchable.
Definition: Core/FieldType/ImageAsset/Type.php:260
eZ\Publish\Core\FieldType\ImageAsset\Type\toHash
toHash(SPIValue $value)
Converts a $Value to a hash.
Definition: Core/FieldType/ImageAsset/Type.php:212
eZ\Publish\Core\FieldType\ImageAsset\Type\$contentService
$contentService
Definition: Core/FieldType/ImageAsset/Type.php:27
eZ\Publish\Core\FieldType\ImageAsset\Type\__construct
__construct(ContentService $contentService, ContentTypeService $contentTypeService, AssetMapper $mapper)
Definition: Core/FieldType/ImageAsset/Type.php:40
eZ\Publish\Core\FieldType\ImageAsset\Type\getName
getName(SPIValue $value)
Returns the name of the given field value.
Definition: Core/FieldType/ImageAsset/Type.php:104
eZ\Publish\Core\FieldType\ImageAsset\Type\getEmptyValue
getEmptyValue()
Returns the fallback default value of field type when no such default value is provided in the field ...
Definition: Core/FieldType/ImageAsset/Type.php:115
eZ\Publish\Core\FieldType\ImageAsset\Type\fromHash
fromHash($hash)
Converts an $hash to the Value defined by the field type.
Definition: Core/FieldType/ImageAsset/Type.php:196
eZ\Publish\Core\FieldType\ImageAsset\Type\$contentTypeService
$contentTypeService
Definition: Core/FieldType/ImageAsset/Type.php:30
eZ\Publish\Core\FieldType\ValidationError
Class for validation errors.
Definition: Core/FieldType/ValidationError.php:17
eZ\Publish\Core\FieldType\Value
Abstract class for all field value classes.
Definition: Core/FieldType/Value.php:17
eZ\Publish\API\Repository\ContentService
Definition: API/Repository/ContentService.php:27
eZ\Publish\API\Repository\ContentTypeService
Definition: API/Repository/ContentTypeService.php:25
eZ\Publish\SPI\FieldType\Value
Interface for field value classes.
Definition: SPI/FieldType/Value.php:13
eZ\Publish\Core\FieldType\ImageAsset
Definition: AssetMapper.php:9
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.