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

Core/FieldType/RelationList/Type.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\RelationList;
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
27 class Type extends FieldType
28 {
29 const SELECTION_BROWSE = 0;
34 const SELECTION_DROPDOWN = 1;
35 const SELECTION_LIST_WITH_RADIO_BUTTONS = 2;
36 const SELECTION_LIST_WITH_CHECKBOXES = 3;
37 const SELECTION_MULTIPLE_SELECTION_LIST = 4;
38 const SELECTION_TEMPLATE_BASED_MULTIPLE = 5;
39 const SELECTION_TEMPLATE_BASED_SINGLE = 6;
40
41 protected $settingsSchema = [
42 'selectionMethod' => [
43 'type' => 'int',
44 'default' => self::SELECTION_BROWSE,
45 ],
46 'selectionDefaultLocation' => [
47 'type' => 'string',
48 'default' => null,
49 ],
50 'selectionContentTypes' => [
51 'type' => 'array',
52 'default' => [],
53 ],
54 ];
55
56 protected $validatorConfigurationSchema = [
57 'RelationListValueValidator' => [
58 'selectionLimit' => [
59 'type' => 'int',
60 'default' => 0,
61 ],
62 ],
63 ];
64
66 private $targetContentValidator;
67
68 public function __construct(TargetContentValidatorInterface $targetContentValidator)
69 {
70 $this->targetContentValidator = $targetContentValidator;
71 }
72
80 public function validateFieldSettings($fieldSettings)
81 {
82 $validationErrors = [];
83
84 foreach ($fieldSettings as $name => $value) {
85 if (!isset($this->settingsSchema[$name])) {
86 $validationErrors[] = new ValidationError(
87 "Setting '%setting%' is unknown",
88 null,
89 [
90 '%setting%' => $name,
91 ],
92 "[$name]"
93 );
94 continue;
95 }
96
97 switch ($name) {
98 case 'selectionMethod':
99 if (!$this->isValidSelectionMethod($value)) {
100 $validationErrors[] = new ValidationError(
101 "Setting '%setting%' must be one of %selection_browse%, %selection_dropdown%, %selection_list_with_radio_buttons%, %selection_list_with_checkboxes%, %selection_multiple_selection_list%, %selection_template_based_multiple%, %selection_template_based_single%",
102 null,
103 [
104 '%setting%' => $name,
105 '%selection_browse%' => self::SELECTION_BROWSE,
106 '%selection_dropdown%' => self::SELECTION_DROPDOWN,
107 '%selection_list_with_radio_buttons%' => self::SELECTION_LIST_WITH_RADIO_BUTTONS,
108 '%selection_list_with_checkboxes%' => self::SELECTION_LIST_WITH_CHECKBOXES,
109 '%selection_multiple_selection_list%' => self::SELECTION_MULTIPLE_SELECTION_LIST,
110 '%selection_template_based_multiple%' => self::SELECTION_TEMPLATE_BASED_MULTIPLE,
111 '%selection_template_based_single%' => self::SELECTION_TEMPLATE_BASED_SINGLE,
112 ],
113 "[$name]"
114 );
115 }
116 break;
117 case 'selectionDefaultLocation':
118 if (!is_int($value) && !is_string($value) && $value !== null) {
119 $validationErrors[] = new ValidationError(
120 "Setting '%setting%' value must be of either null, string or integer",
121 null,
122 [
123 '%setting%' => $name,
124 ],
125 "[$name]"
126 );
127 }
128 break;
129 case 'selectionContentTypes':
130 if (!is_array($value)) {
131 $validationErrors[] = new ValidationError(
132 "Setting '%setting%' value must be of array type",
133 null,
134 [
135 '%setting%' => $name,
136 ],
137 "[$name]"
138 );
139 }
140 break;
141 case 'selectionLimit':
142 if (!is_int($value)) {
143 $validationErrors[] = new ValidationError(
144 "Setting '%setting%' value must be of integer type",
145 null,
146 [
147 '%setting%' => $name,
148 ],
149 "[$name]"
150 );
151 }
152 if ($value < 0) {
153 $validationErrors[] = new ValidationError(
154 "Setting '%setting%' value cannot be lower than 0",
155 null,
156 [
157 '%setting%' => $name,
158 ],
159 "[$name]"
160 );
161 }
162 break;
163 }
164 }
165
166 return $validationErrors;
167 }
168
176 public function validateValidatorConfiguration($validatorConfiguration)
177 {
178 $validationErrors = [];
179
180 foreach ($validatorConfiguration as $validatorIdentifier => $constraints) {
181 if ($validatorIdentifier !== 'RelationListValueValidator') {
182 $validationErrors[] = new ValidationError(
183 "Validator '%validator%' is unknown",
184 null,
185 [
186 '%validator%' => $validatorIdentifier,
187 ],
188 "[$validatorIdentifier]"
189 );
190
191 continue;
192 }
193
194 foreach ($constraints as $name => $value) {
195 if ($name === 'selectionLimit') {
196 if (!is_int($value) && !ctype_digit($value)) {
197 $validationErrors[] = new ValidationError(
198 "Validator parameter '%parameter%' value must be an integer",
199 null,
200 [
201 '%parameter%' => $name,
202 ],
203 "[$validatorIdentifier][$name]"
204 );
205 }
206 if ($value < 0) {
207 $validationErrors[] = new ValidationError(
208 "Validator parameter '%parameter%' value must be equal to/greater than 0",
209 null,
210 [
211 '%parameter%' => $name,
212 ],
213 "[$validatorIdentifier][$name]"
214 );
215 }
216 } else {
217 $validationErrors[] = new ValidationError(
218 "Validator parameter '%parameter%' is unknown",
219 null,
220 [
221 '%parameter%' => $name,
222 ],
223 "[$validatorIdentifier][$name]"
224 );
225 }
226 }
227 }
228
229 return $validationErrors;
230 }
231
237 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue): array
238 {
239 $validationErrors = [];
240
241 if ($this->isEmptyValue($fieldValue)) {
242 return $validationErrors;
243 }
244
245 $validatorConfiguration = $fieldDefinition->getValidatorConfiguration();
246 $constraints = $validatorConfiguration['RelationListValueValidator'] ?? [];
247
248 $validationErrors = [];
249
250 if (isset($constraints['selectionLimit']) &&
251 $constraints['selectionLimit'] > 0 && count($fieldValue->destinationContentIds) > $constraints['selectionLimit']) {
252 $validationErrors[] = new ValidationError(
253 'The selected content items number cannot be higher than %limit%.',
254 null,
255 [
256 '%limit%' => $constraints['selectionLimit'],
257 ],
258 'destinationContentIds'
259 );
260 }
261
262 $allowedContentTypes = $fieldDefinition->getFieldSettings()['selectionContentTypes'] ?? [];
263
264 foreach ($fieldValue->destinationContentIds as $destinationContentId) {
265 $validationError = $this->targetContentValidator->validate((int) $destinationContentId, $allowedContentTypes);
266 if ($validationError !== null) {
267 $validationErrors[] = $validationError;
268 }
269 }
270
271 return $validationErrors;
272 }
273
279 public function getFieldTypeIdentifier()
280 {
281 return 'ezobjectrelationlist';
282 }
283
294 public function getName(SPIValue $value)
295 {
296 throw new \RuntimeException('Name generation provided via NameableField set via "ezpublish.fieldType.nameable" service tag');
297 }
298
305 public function getEmptyValue()
306 {
307 return new Value();
308 }
309
317 protected function createValueFromInput($inputValue)
318 {
319 // ContentInfo
320 if ($inputValue instanceof ContentInfo) {
321 $inputValue = new Value([$inputValue->id]);
322 } elseif (is_int($inputValue) || is_string($inputValue)) {
323 // content id
324 $inputValue = new Value([$inputValue]);
325 } elseif (is_array($inputValue)) {
326 // content id's
327 $inputValue = new Value($inputValue);
328 }
329
330 return $inputValue;
331 }
332
340 protected function checkValueStructure(BaseValue $value)
341 {
342 if (!is_array($value->destinationContentIds)) {
343 throw new InvalidArgumentType(
344 '$value->destinationContentIds',
345 'array',
346 $value->destinationContentIds
347 );
348 }
349
350 foreach ($value->destinationContentIds as $key => $destinationContentId) {
351 if (!is_int($destinationContentId) && !is_string($destinationContentId)) {
352 throw new InvalidArgumentType(
353 "\$value->destinationContentIds[$key]",
354 'string|int',
355 $destinationContentId
356 );
357 }
358 }
359 }
360
370 protected function getSortInfo(BaseValue $value)
371 {
372 return implode(',', $value->destinationContentIds);
373 }
374
382 public function fromHash($hash)
383 {
384 return new Value($hash['destinationContentIds']);
385 }
386
394 public function toHash(SPIValue $value)
395 {
396 return ['destinationContentIds' => $value->destinationContentIds];
397 }
398
404 public function isSearchable()
405 {
406 return true;
407 }
408
434 public function getRelations(SPIValue $value)
435 {
436 /* @var \eZ\Publish\Core\FieldType\RelationList\Value $value */
437 return [
438 Relation::FIELD => $value->destinationContentIds,
439 ];
440 }
441
449 private function isValidSelectionMethod($selectionMethod)
450 {
451 return in_array($selectionMethod, [
452 self::SELECTION_BROWSE,
453 self::SELECTION_DROPDOWN,
454 self::SELECTION_LIST_WITH_RADIO_BUTTONS,
455 self::SELECTION_LIST_WITH_CHECKBOXES,
456 self::SELECTION_MULTIPLE_SELECTION_LIST,
457 self::SELECTION_TEMPLATE_BASED_MULTIPLE,
458 self::SELECTION_TEMPLATE_BASED_SINGLE,
459 ], true);
460 }
461 }
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\ContentType\FieldDefinition\getValidatorConfiguration
getValidatorConfiguration()
This method returns the validator configuration of this 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\FieldType\isEmptyValue
isEmptyValue(SPIValue $value)
Returns if the given $value is considered empty by the field type.
Definition: Core/FieldType/FieldType.php:350
eZ\Publish\Core\FieldType\RelationList\Type
The RelationList field type.
Definition: Core/FieldType/RelationList/Type.php:28
eZ\Publish\Core\FieldType\RelationList\Type\SELECTION_LIST_WITH_CHECKBOXES
const SELECTION_LIST_WITH_CHECKBOXES
Definition: Core/FieldType/RelationList/Type.php:36
eZ\Publish\Core\FieldType\RelationList\Type\SELECTION_BROWSE
const SELECTION_BROWSE
Definition: Core/FieldType/RelationList/Type.php:29
eZ\Publish\Core\FieldType\RelationList\Type\isSearchable
isSearchable()
Returns whether the field type is searchable.
Definition: Core/FieldType/RelationList/Type.php:404
eZ\Publish\Core\FieldType\RelationList\Type\createValueFromInput
createValueFromInput($inputValue)
Inspects given $inputValue and potentially converts it into a dedicated value object.
Definition: Core/FieldType/RelationList/Type.php:317
eZ\Publish\Core\FieldType\RelationList\Type\toHash
toHash(SPIValue $value)
Converts a $Value to a hash.
Definition: Core/FieldType/RelationList/Type.php:394
eZ\Publish\Core\FieldType\RelationList\Type\SELECTION_TEMPLATE_BASED_SINGLE
const SELECTION_TEMPLATE_BASED_SINGLE
Definition: Core/FieldType/RelationList/Type.php:39
eZ\Publish\Core\FieldType\RelationList\Type\getEmptyValue
getEmptyValue()
Returns the fallback default value of field type when no such default value is provided in the field ...
Definition: Core/FieldType/RelationList/Type.php:305
eZ\Publish\Core\FieldType\RelationList\Type\getName
getName(SPIValue $value)
Returns the name of the given field value.
Definition: Core/FieldType/RelationList/Type.php:294
eZ\Publish\Core\FieldType\RelationList\Type\checkValueStructure
checkValueStructure(BaseValue $value)
Throws an exception if value structure is not of expected format.
Definition: Core/FieldType/RelationList/Type.php:340
eZ\Publish\Core\FieldType\RelationList\Type\$settingsSchema
$settingsSchema
Definition: Core/FieldType/RelationList/Type.php:41
eZ\Publish\Core\FieldType\RelationList\Type\SELECTION_MULTIPLE_SELECTION_LIST
const SELECTION_MULTIPLE_SELECTION_LIST
Definition: Core/FieldType/RelationList/Type.php:37
eZ\Publish\Core\FieldType\RelationList\Type\validateValidatorConfiguration
validateValidatorConfiguration($validatorConfiguration)
Validates the validatorConfiguration of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
Definition: Core/FieldType/RelationList/Type.php:176
eZ\Publish\Core\FieldType\RelationList\Type\$targetContentValidator
$targetContentValidator
Definition: Core/FieldType/RelationList/Type.php:66
eZ\Publish\Core\FieldType\RelationList\Type\getFieldTypeIdentifier
getFieldTypeIdentifier()
Returns the field type identifier for this field type.
Definition: Core/FieldType/RelationList/Type.php:279
eZ\Publish\Core\FieldType\RelationList\Type\SELECTION_TEMPLATE_BASED_MULTIPLE
const SELECTION_TEMPLATE_BASED_MULTIPLE
Definition: Core/FieldType/RelationList/Type.php:38
eZ\Publish\Core\FieldType\RelationList\Type\fromHash
fromHash($hash)
Converts an $hash to the Value defined by the field type.
Definition: Core/FieldType/RelationList/Type.php:382
eZ\Publish\Core\FieldType\RelationList\Type\getRelations
getRelations(SPIValue $value)
Returns relation data extracted from value.
Definition: Core/FieldType/RelationList/Type.php:434
eZ\Publish\Core\FieldType\RelationList\Type\SELECTION_LIST_WITH_RADIO_BUTTONS
const SELECTION_LIST_WITH_RADIO_BUTTONS
Definition: Core/FieldType/RelationList/Type.php:35
eZ\Publish\Core\FieldType\RelationList\Type\validateFieldSettings
validateFieldSettings($fieldSettings)
Definition: Core/FieldType/RelationList/Type.php:80
eZ\Publish\Core\FieldType\RelationList\Type\SELECTION_DROPDOWN
const SELECTION_DROPDOWN
Definition: Core/FieldType/RelationList/Type.php:34
eZ\Publish\Core\FieldType\RelationList\Type\$validatorConfigurationSchema
$validatorConfigurationSchema
Definition: Core/FieldType/RelationList/Type.php:56
eZ\Publish\Core\FieldType\RelationList\Type\getSortInfo
getSortInfo(BaseValue $value)
Returns information for FieldValue->$sortKey relevant to the field type.
Definition: Core/FieldType/RelationList/Type.php:370
eZ\Publish\Core\FieldType\RelationList\Type\__construct
__construct(TargetContentValidatorInterface $targetContentValidator)
Definition: Core/FieldType/RelationList/Type.php:68
eZ\Publish\Core\FieldType\RelationList\Type\isValidSelectionMethod
isValidSelectionMethod($selectionMethod)
Checks whether given selectionMethod is valid.
Definition: Core/FieldType/RelationList/Type.php:449
eZ\Publish\Core\FieldType\RelationList\Type\validate
validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
Validates a field based on the validators in the field definition.
Definition: Core/FieldType/RelationList/Type.php:237
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\RelationList
Definition: RelationList/NameableField.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.