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

Core/FieldType/Selection/Type.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\Selection;
8
9 use eZ\Publish\Core\FieldType\FieldType;
10 use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
11 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
12 use eZ\Publish\Core\FieldType\ValidationError;
13 use eZ\Publish\SPI\FieldType\Value as SPIValue;
14 use eZ\Publish\Core\FieldType\Value as BaseValue;
15
21 class Type extends FieldType
22 {
31 protected $settingsSchema = [
32 'isMultiple' => [
33 'type' => 'bool',
34 'default' => false,
35 ],
36 'options' => [
37 'type' => 'hash',
38 'default' => [],
39 ],
40 'multilingualOptions' => [
41 'type' => 'hash',
42 'default' => [],
43 ],
44 ];
45
53 public function validateFieldSettings($fieldSettings)
54 {
55 $validationErrors = [];
56
57 foreach ($fieldSettings as $settingKey => $settingValue) {
58 switch ($settingKey) {
59 case 'isMultiple':
60 if (!is_bool($settingValue)) {
61 $validationErrors[] = new ValidationError(
62 "FieldType '%fieldType%' expects setting '%setting%' to be of type '%type%'",
63 null,
64 [
65 '%fieldType%' => $this->getFieldTypeIdentifier(),
66 '%setting%' => $settingKey,
67 '%type%' => 'bool',
68 ],
69 "[$settingKey]"
70 );
71 }
72 break;
73 case 'options':
74 if (!is_array($settingValue)) {
75 $validationErrors[] = new ValidationError(
76 "FieldType '%fieldType%' expects setting '%setting%' to be of type '%type%'",
77 null,
78 [
79 '%fieldType%' => $this->getFieldTypeIdentifier(),
80 '%setting%' => $settingKey,
81 '%type%' => 'hash',
82 ],
83 "[$settingKey]"
84 );
85 }
86 break;
87 case 'multilingualOptions':
88 if (!is_array($settingValue) && !is_array(reset($settingValue))) {
89 $validationErrors[] = new ValidationError(
90 "FieldType '%fieldType%' expects setting '%setting%' to be of type '%type%'",
91 null,
92 [
93 '%fieldType%' => $this->getFieldTypeIdentifier(),
94 '%setting%' => $settingKey,
95 '%type%' => 'hash',
96 ],
97 "[$settingKey]"
98 );
99 }
100 break;
101 default:
102 $validationErrors[] = new ValidationError(
103 "Setting '%setting%' is unknown",
104 null,
105 [
106 '%setting%' => $settingKey,
107 ],
108 "[$settingKey]"
109 );
110 }
111 }
112
113 return $validationErrors;
114 }
115
121 public function getFieldTypeIdentifier()
122 {
123 return 'ezselection';
124 }
125
136 public function getName(SPIValue $value)
137 {
138 throw new \RuntimeException('Name generation provided via NameableField set via "ezpublish.fieldType.nameable" service tag');
139 }
140
147 public function getEmptyValue()
148 {
149 return new Value();
150 }
151
159 protected function createValueFromInput($inputValue)
160 {
161 if (is_array($inputValue)) {
162 $inputValue = new Value($inputValue);
163 }
164
165 return $inputValue;
166 }
167
175 protected function checkValueStructure(BaseValue $value)
176 {
177 if (!is_array($value->selection)) {
178 throw new InvalidArgumentType(
179 '$value->selection',
180 'array',
181 $value->selection
182 );
183 }
184 }
185
198 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
199 {
200 $validationErrors = [];
201
202 if ($this->isEmptyValue($fieldValue)) {
203 return $validationErrors;
204 }
205
206 $fieldSettings = $fieldDefinition->getFieldSettings();
207
208 if ((!isset($fieldSettings['isMultiple']) || $fieldSettings['isMultiple'] === false)
209 && count($fieldValue->selection) > 1) {
210 $validationErrors[] = new ValidationError(
211 'Field definition does not allow multiple options to be selected.',
212 null,
213 [],
214 'selection'
215 );
216 }
217
218 foreach ($fieldValue->selection as $optionIndex) {
219 if (!isset($fieldSettings['options'][$optionIndex]) && empty($fieldSettings['multilingualOptions'])) {
220 $validationErrors[] = new ValidationError(
221 'Option with index %index% does not exist in the field definition.',
222 null,
223 [
224 '%index%' => $optionIndex,
225 ],
226 'selection'
227 );
228 }
229 }
230
231 //@todo: find a way to include selection language
232 if (isset($fieldSettings['multilingualOptions'])) {
233 $possibleOptionIndexesByLanguage = array_map(function ($languageOptionIndexes) {
234 return array_keys($languageOptionIndexes);
235 }, $fieldSettings['multilingualOptions']);
236
237 $possibleOptionIndexes = array_merge(...array_values($possibleOptionIndexesByLanguage));
238
239 foreach ($fieldValue->selection as $optionIndex) {
240 if (!in_array($optionIndex, $possibleOptionIndexes)) {
241 $validationErrors[] = new ValidationError(
242 'Option with index %index% does not exist in the field definition.',
243 null,
244 [
245 '%index%' => $optionIndex,
246 ],
247 'selection'
248 );
249 }
250 }
251 }
252
253 return $validationErrors;
254 }
255
263 protected function getSortInfo(BaseValue $value)
264 {
265 return implode('-', $value->selection);
266 }
267
275 public function fromHash($hash)
276 {
277 return new Value($hash);
278 }
279
287 public function toHash(SPIValue $value)
288 {
289 return $value->selection;
290 }
291
297 public function isSearchable()
298 {
299 return true;
300 }
301 }
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\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\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\Selection\Type
The Selection field type.
Definition: Core/FieldType/Selection/Type.php:22
eZ\Publish\Core\FieldType\Selection\Type\createValueFromInput
createValueFromInput($inputValue)
Inspects given $inputValue and potentially converts it into a dedicated value object.
Definition: Core/FieldType/Selection/Type.php:159
eZ\Publish\Core\FieldType\Selection\Type\getSortInfo
getSortInfo(BaseValue $value)
Returns information for FieldValue->$sortKey relevant to the field type.
Definition: Core/FieldType/Selection/Type.php:263
eZ\Publish\Core\FieldType\Selection\Type\validateFieldSettings
validateFieldSettings($fieldSettings)
Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
Definition: Core/FieldType/Selection/Type.php:53
eZ\Publish\Core\FieldType\Selection\Type\getFieldTypeIdentifier
getFieldTypeIdentifier()
Returns the field type identifier for this field type.
Definition: Core/FieldType/Selection/Type.php:121
eZ\Publish\Core\FieldType\Selection\Type\fromHash
fromHash($hash)
Converts an $hash to the Value defined by the field type.
Definition: Core/FieldType/Selection/Type.php:275
eZ\Publish\Core\FieldType\Selection\Type\isSearchable
isSearchable()
Returns whether the field type is searchable.
Definition: Core/FieldType/Selection/Type.php:297
eZ\Publish\Core\FieldType\Selection\Type\getEmptyValue
getEmptyValue()
Returns the fallback default value of field type when no such default value is provided in the field ...
Definition: Core/FieldType/Selection/Type.php:147
eZ\Publish\Core\FieldType\Selection\Type\$settingsSchema
$settingsSchema
Definition: Core/FieldType/Selection/Type.php:31
eZ\Publish\Core\FieldType\Selection\Type\getName
getName(SPIValue $value)
Returns the name of the given field value.
Definition: Core/FieldType/Selection/Type.php:136
eZ\Publish\Core\FieldType\Selection\Type\checkValueStructure
checkValueStructure(BaseValue $value)
Throws an exception if value structure is not of expected format.
Definition: Core/FieldType/Selection/Type.php:175
eZ\Publish\Core\FieldType\Selection\Type\validate
validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
Validates field value against 'isMultiple' and 'options' settings.
Definition: Core/FieldType/Selection/Type.php:198
eZ\Publish\Core\FieldType\Selection\Type\toHash
toHash(SPIValue $value)
Converts a $Value to a hash.
Definition: Core/FieldType/Selection/Type.php:287
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\SPI\FieldType\Value
Interface for field value classes.
Definition: SPI/FieldType/Value.php:13
eZ\Publish\Core\FieldType\Selection
Definition: Selection/NameableField.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.