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

Core/FieldType/Author/Type.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\Author;
8
9 use eZ\Publish\Core\FieldType\FieldType;
10 use eZ\Publish\Core\FieldType\Value as BaseValue;
11 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
12 use eZ\Publish\SPI\FieldType\Value as SPIValue;
13 use eZ\Publish\Core\FieldType\ValidationError;
14
21 class Type extends FieldType
22 {
27 const DEFAULT_VALUE_EMPTY = 0;
28
33 const DEFAULT_CURRENT_USER = 1;
34
35 protected $settingsSchema = [
36 'defaultAuthor' => [
37 'type' => 'choice',
38 'default' => self::DEFAULT_VALUE_EMPTY,
39 ],
40 ];
41
47 public function getFieldTypeIdentifier()
48 {
49 return 'ezauthor';
50 }
51
62 public function getName(SPIValue $value)
63 {
64 return isset($value->authors[0]) ? $value->authors[0]->name : '';
65 }
66
73 public function getEmptyValue()
74 {
75 return new Value();
76 }
77
85 protected function createValueFromInput($inputValue)
86 {
87 if (is_array($inputValue)) {
88 $inputValue = new Value($inputValue);
89 }
90
91 return $inputValue;
92 }
93
101 protected function checkValueStructure(BaseValue $value)
102 {
103 if (!$value->authors instanceof AuthorCollection) {
104 throw new InvalidArgumentType(
105 '$value->authors',
106 AuthorCollection::class,
107 $value->authors
108 );
109 }
110 }
111
115 protected function getSortInfo(BaseValue $value)
116 {
117 if (empty($value->authors)) {
118 return false;
119 }
120
121 $authors = [];
122 foreach ($value->authors as $author) {
123 $authors[] = $this->transformationProcessor->transformByGroup($author->name, 'lowercase');
124 }
125
126 sort($authors);
127
128 return implode(',', $authors);
129 }
130
138 public function fromHash($hash)
139 {
140 return new Value(
141 array_map(
142 function ($author) {
143 return new Author($author);
144 },
145 $hash
146 )
147 );
148 }
149
157 public function toHash(SPIValue $value)
158 {
159 return array_map(
160 function ($author) {
161 return (array)$author;
162 },
163 $value->authors->getArrayCopy()
164 );
165 }
166
172 public function isSearchable()
173 {
174 return true;
175 }
176
184 public function validateFieldSettings($fieldSettings)
185 {
186 $validationErrors = [];
187
188 foreach ($fieldSettings as $name => $value) {
189 $settingNameError = $this->validateSettingName($name);
190
191 if ($settingNameError instanceof ValidationError) {
192 $validationErrors[] = $settingNameError;
193 }
194
195 switch ($name) {
196 case 'defaultAuthor':
197 $settingValueError = $this->validateDefaultAuthorSetting($name, $value);
198 if ($settingValueError instanceof ValidationError) {
199 $validationErrors[] = $settingValueError;
200 }
201 break;
202 }
203 }
204
205 return $validationErrors;
206 }
207
215 private function validateSettingName($name)
216 {
217 if (!isset($this->settingsSchema[$name])) {
218 return new ValidationError(
219 "Setting '%setting%' is unknown",
220 null,
221 [
222 '%setting%' => $name,
223 ],
224 "[$name]"
225 );
226 }
227
228 return null;
229 }
230
239 private function validateDefaultAuthorSetting($name, $value)
240 {
241 $definedValues = [
242 self::DEFAULT_VALUE_EMPTY,
243 self::DEFAULT_CURRENT_USER,
244 ];
245
246 if (!in_array($value, $definedValues, true)) {
247 return new ValidationError(
248 "Setting '%setting%' has unknown default value",
249 null,
250 [
251 '%setting%' => $name,
252 ],
253 "[$name]"
254 );
255 }
256
257 return null;
258 }
259 }
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\Author\AuthorCollection
Author collection.
Definition: AuthorCollection.php:17
eZ\Publish\Core\FieldType\Author\Author
Value object for an author.
Definition: Author.php:15
eZ\Publish\Core\FieldType\Author\Type
Author field type.
Definition: Core/FieldType/Author/Type.php:22
eZ\Publish\Core\FieldType\Author\Type\DEFAULT_CURRENT_USER
const DEFAULT_CURRENT_USER
Flag which stands for prefilling Author FieldType with current user by default.
Definition: Core/FieldType/Author/Type.php:33
eZ\Publish\Core\FieldType\Author\Type\$settingsSchema
$settingsSchema
Definition: Core/FieldType/Author/Type.php:35
eZ\Publish\Core\FieldType\Author\Type\DEFAULT_VALUE_EMPTY
const DEFAULT_VALUE_EMPTY
Flag which stands for setting Author FieldType empty by default.
Definition: Core/FieldType/Author/Type.php:27
eZ\Publish\Core\FieldType\Author\Type\validateDefaultAuthorSetting
validateDefaultAuthorSetting($name, $value)
Validates if the defaultAuthor setting has one of the defined values.
Definition: Core/FieldType/Author/Type.php:239
eZ\Publish\Core\FieldType\Author\Type\getEmptyValue
getEmptyValue()
Returns the fallback default value of field type when no such default value is provided in the field ...
Definition: Core/FieldType/Author/Type.php:73
eZ\Publish\Core\FieldType\Author\Type\fromHash
fromHash($hash)
Converts an $hash to the Value defined by the field type.
Definition: Core/FieldType/Author/Type.php:138
eZ\Publish\Core\FieldType\Author\Type\createValueFromInput
createValueFromInput($inputValue)
Inspects given $inputValue and potentially converts it into a dedicated value object.
Definition: Core/FieldType/Author/Type.php:85
eZ\Publish\Core\FieldType\Author\Type\checkValueStructure
checkValueStructure(BaseValue $value)
Throws an exception if value structure is not of expected format.
Definition: Core/FieldType/Author/Type.php:101
eZ\Publish\Core\FieldType\Author\Type\toHash
toHash(SPIValue $value)
Converts a $Value to a hash.
Definition: Core/FieldType/Author/Type.php:157
eZ\Publish\Core\FieldType\Author\Type\getFieldTypeIdentifier
getFieldTypeIdentifier()
Returns the field type identifier for this field type.
Definition: Core/FieldType/Author/Type.php:47
eZ\Publish\Core\FieldType\Author\Type\validateFieldSettings
validateFieldSettings($fieldSettings)
Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
Definition: Core/FieldType/Author/Type.php:184
eZ\Publish\Core\FieldType\Author\Type\isSearchable
isSearchable()
Returns whether the field type is searchable.
Definition: Core/FieldType/Author/Type.php:172
eZ\Publish\Core\FieldType\Author\Type\validateSettingName
validateSettingName($name)
Validates the fieldSetting name.
Definition: Core/FieldType/Author/Type.php:215
eZ\Publish\Core\FieldType\Author\Type\getSortInfo
getSortInfo(BaseValue $value)
{}
Definition: Core/FieldType/Author/Type.php:115
eZ\Publish\Core\FieldType\Author\Type\getName
getName(SPIValue $value)
Returns the name of the given field value.
Definition: Core/FieldType/Author/Type.php:62
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\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\Author
Definition: Author.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.