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

Core/FieldType/Relation/Type.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\Relation;
8
9 use eZ\Publish\API\Repository\Values\Content\ContentInfo;
10 use eZ\Publish\API\Repository\Values\Content\Relation;
11 use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
12 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
13 use eZ\Publish\Core\FieldType\FieldType;
14 use eZ\Publish\Core\FieldType\ValidationError;
15 use eZ\Publish\Core\FieldType\Value as BaseValue;
16 use eZ\Publish\SPI\FieldType\Value as SPIValue;
17 use Ibexa\Core\Repository\Validator\TargetContentValidatorInterface;
18
29 class Type extends FieldType
30 {
31 const SELECTION_BROWSE = 0;
32 const SELECTION_DROPDOWN = 1;
33
34 protected $settingsSchema = [
35 'selectionMethod' => [
36 'type' => 'int',
37 'default' => self::SELECTION_BROWSE,
38 ],
39 'selectionRoot' => [
40 'type' => 'string',
41 'default' => null,
42 ],
43 'selectionContentTypes' => [
44 'type' => 'array',
45 'default' => [],
46 ],
47 ];
48
50 private $targetContentValidator;
51
52 public function __construct(TargetContentValidatorInterface $targetContentValidator)
53 {
54 $this->targetContentValidator = $targetContentValidator;
55 }
56
64 public function validateFieldSettings($fieldSettings)
65 {
66 $validationErrors = [];
67
68 foreach ($fieldSettings as $name => $value) {
69 if (!isset($this->settingsSchema[$name])) {
70 $validationErrors[] = new ValidationError(
71 "Setting '%setting%' is unknown",
72 null,
73 [
74 '%setting%' => $name,
75 ],
76 "[$name]"
77 );
78 continue;
79 }
80
81 switch ($name) {
82 case 'selectionMethod':
83 if ($value !== self::SELECTION_BROWSE && $value !== self::SELECTION_DROPDOWN) {
84 $validationErrors[] = new ValidationError(
85 "Setting '%setting%' must be either %selection_browse% or %selection_dropdown%",
86 null,
87 [
88 '%setting%' => $name,
89 '%selection_browse%' => self::SELECTION_BROWSE,
90 '%selection_dropdown%' => self::SELECTION_DROPDOWN,
91 ],
92 "[$name]"
93 );
94 }
95 break;
96 case 'selectionRoot':
97 if (!is_int($value) && !is_string($value) && $value !== null) {
98 $validationErrors[] = new ValidationError(
99 "Setting '%setting%' value must be of either null, string or integer",
100 null,
101 [
102 '%setting%' => $name,
103 ],
104 "[$name]"
105 );
106 }
107 break;
108 case 'selectionContentTypes':
109 if (!is_array($value)) {
110 $validationErrors[] = new ValidationError(
111 "Setting '%setting%' value must be of array type",
112 null,
113 [
114 '%setting%' => $name,
115 ],
116 "[$name]"
117 );
118 }
119 break;
120 }
121 }
122
123 return $validationErrors;
124 }
125
131 public function getFieldTypeIdentifier()
132 {
133 return 'ezobjectrelation';
134 }
135
146 public function getName(SPIValue $value)
147 {
148 throw new \RuntimeException('Name generation provided via NameableField set via "ezpublish.fieldType.nameable" service tag');
149 }
150
156 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue): array
157 {
158 $validationErrors = [];
159
160 if ($this->isEmptyValue($fieldValue)) {
161 return $validationErrors;
162 }
163
164 $allowedContentTypes = $fieldDefinition->getFieldSettings()['selectionContentTypes'] ?? [];
165
166 $validationError = $this->targetContentValidator->validate(
167 (int) $fieldValue->destinationContentId,
168 $allowedContentTypes
169 );
170
171 return $validationError === null ? $validationErrors : [$validationError];
172 }
173
180 public function getEmptyValue()
181 {
182 return new Value();
183 }
184
192 public function isEmptyValue(SPIValue $value)
193 {
194 return $value->destinationContentId === null;
195 }
196
204 protected function createValueFromInput($inputValue)
205 {
206 // ContentInfo
207 if ($inputValue instanceof ContentInfo) {
208 $inputValue = new Value($inputValue->id);
209 } elseif (is_int($inputValue) || is_string($inputValue)) { // content id
210 $inputValue = new Value($inputValue);
211 }
212
213 return $inputValue;
214 }
215
223 protected function checkValueStructure(BaseValue $value)
224 {
225 if (!is_int($value->destinationContentId) && !is_string($value->destinationContentId)) {
226 throw new InvalidArgumentType(
227 '$value->destinationContentId',
228 'string|int',
229 $value->destinationContentId
230 );
231 }
232 }
233
242 protected function getSortInfo(BaseValue $value)
243 {
244 return (string)$value;
245 }
246
254 public function fromHash($hash)
255 {
256 if ($hash !== null) {
257 $destinationContentId = $hash['destinationContentId'];
258 if ($destinationContentId !== null) {
259 return new Value((int)$destinationContentId);
260 }
261 }
262
263 return $this->getEmptyValue();
264 }
265
273 public function toHash(SPIValue $value)
274 {
275 return ['destinationContentId' => $value->destinationContentId];
276 }
277
283 public function isSearchable()
284 {
285 return true;
286 }
287
313 public function getRelations(SPIValue $fieldValue)
314 {
315 $relations = [];
316 if ($fieldValue->destinationContentId !== null) {
317 $relations[Relation::FIELD] = [$fieldValue->destinationContentId];
318 }
319
320 return $relations;
321 }
322 }
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition
Definition: API/Repository/Values/ContentType/FieldDefinition.php:31
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition\getFieldSettings
getFieldSettings()
This method returns settings for the field definition supported by the field type.
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\FIELD
const FIELD
Definition: API/Repository/Values/Content/Relation.php:48
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\Relation\Type
The Relation field type.
Definition: Core/FieldType/Relation/Type.php:30
eZ\Publish\Core\FieldType\Relation\Type\toHash
toHash(SPIValue $value)
Converts a $Value to a hash.
Definition: Core/FieldType/Relation/Type.php:273
eZ\Publish\Core\FieldType\Relation\Type\getSortInfo
getSortInfo(BaseValue $value)
Returns information for FieldValue->$sortKey relevant to the field type.
Definition: Core/FieldType/Relation/Type.php:242
eZ\Publish\Core\FieldType\Relation\Type\getRelations
getRelations(SPIValue $fieldValue)
Returns relation data extracted from value.
Definition: Core/FieldType/Relation/Type.php:313
eZ\Publish\Core\FieldType\Relation\Type\createValueFromInput
createValueFromInput($inputValue)
Inspects given $inputValue and potentially converts it into a dedicated value object.
Definition: Core/FieldType/Relation/Type.php:204
eZ\Publish\Core\FieldType\Relation\Type\validate
validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
Validates a field based on the validators in the field definition.
Definition: Core/FieldType/Relation/Type.php:156
eZ\Publish\Core\FieldType\Relation\Type\checkValueStructure
checkValueStructure(BaseValue $value)
Throws an exception if value structure is not of expected format.
Definition: Core/FieldType/Relation/Type.php:223
eZ\Publish\Core\FieldType\Relation\Type\SELECTION_DROPDOWN
const SELECTION_DROPDOWN
Definition: Core/FieldType/Relation/Type.php:32
eZ\Publish\Core\FieldType\Relation\Type\isEmptyValue
isEmptyValue(SPIValue $value)
Returns if the given $value is considered empty by the field type.
Definition: Core/FieldType/Relation/Type.php:192
eZ\Publish\Core\FieldType\Relation\Type\isSearchable
isSearchable()
Returns whether the field type is searchable.
Definition: Core/FieldType/Relation/Type.php:283
eZ\Publish\Core\FieldType\Relation\Type\$settingsSchema
$settingsSchema
Definition: Core/FieldType/Relation/Type.php:34
eZ\Publish\Core\FieldType\Relation\Type\getEmptyValue
getEmptyValue()
Returns the fallback default value of field type when no such default value is provided in the field ...
Definition: Core/FieldType/Relation/Type.php:180
eZ\Publish\Core\FieldType\Relation\Type\SELECTION_BROWSE
const SELECTION_BROWSE
Definition: Core/FieldType/Relation/Type.php:31
eZ\Publish\Core\FieldType\Relation\Type\__construct
__construct(TargetContentValidatorInterface $targetContentValidator)
Definition: Core/FieldType/Relation/Type.php:52
eZ\Publish\Core\FieldType\Relation\Type\getName
getName(SPIValue $value)
Returns the name of the given field value.
Definition: Core/FieldType/Relation/Type.php:146
eZ\Publish\Core\FieldType\Relation\Type\fromHash
fromHash($hash)
Converts an $hash to the Value defined by the field type.
Definition: Core/FieldType/Relation/Type.php:254
eZ\Publish\Core\FieldType\Relation\Type\getFieldTypeIdentifier
getFieldTypeIdentifier()
Returns the field type identifier for this field type.
Definition: Core/FieldType/Relation/Type.php:131
eZ\Publish\Core\FieldType\Relation\Type\validateFieldSettings
validateFieldSettings($fieldSettings)
Definition: Core/FieldType/Relation/Type.php:64
eZ\Publish\Core\FieldType\Relation\Type\$targetContentValidator
$targetContentValidator
Definition: Core/FieldType/Relation/Type.php:50
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
Ibexa\Core\Repository\Validator\TargetContentValidatorInterface
Definition: TargetContentValidatorInterface.php:17
eZ\Publish\SPI\FieldType\Value
Interface for field value classes.
Definition: SPI/FieldType/Value.php:13
eZ\Publish\Core\FieldType\Relation
Definition: Relation/NameableField.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.