Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Field;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
6 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
7 use Drupal\Core\TypedData\DataDefinitionInterface;
|
Chris@0
|
8 use Drupal\Core\TypedData\Plugin\DataType\Map;
|
Chris@0
|
9 use Drupal\Core\TypedData\TypedDataInterface;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * An entity field item.
|
Chris@0
|
13 *
|
Chris@0
|
14 * Entity field items making use of this base class have to implement
|
Chris@0
|
15 * the static method propertyDefinitions().
|
Chris@0
|
16 *
|
Chris@0
|
17 * @see \Drupal\Core\Field\FieldItemInterface
|
Chris@0
|
18 * @ingroup field_types
|
Chris@0
|
19 */
|
Chris@0
|
20 abstract class FieldItemBase extends Map implements FieldItemInterface {
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * {@inheritdoc}
|
Chris@0
|
24 */
|
Chris@0
|
25 public static function defaultStorageSettings() {
|
Chris@0
|
26 return [];
|
Chris@0
|
27 }
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * {@inheritdoc}
|
Chris@0
|
31 */
|
Chris@0
|
32 public static function defaultFieldSettings() {
|
Chris@0
|
33 return [];
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * {@inheritdoc}
|
Chris@0
|
38 */
|
Chris@0
|
39 public static function mainPropertyName() {
|
Chris@0
|
40 return 'value';
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * {@inheritdoc}
|
Chris@0
|
45 */
|
Chris@0
|
46 public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
|
Chris@0
|
47 parent::__construct($definition, $name, $parent);
|
Chris@0
|
48 // Initialize computed properties by default, such that they get cloned
|
Chris@0
|
49 // with the whole item.
|
Chris@0
|
50 foreach ($this->definition->getPropertyDefinitions() as $name => $definition) {
|
Chris@0
|
51 if ($definition->isComputed()) {
|
Chris@0
|
52 $this->properties[$name] = \Drupal::typedDataManager()->getPropertyInstance($this, $name);
|
Chris@0
|
53 }
|
Chris@0
|
54 }
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * {@inheritdoc}
|
Chris@0
|
59 */
|
Chris@0
|
60 public function getEntity() {
|
Chris@0
|
61 return $this->getParent()->getEntity();
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * {@inheritdoc}
|
Chris@0
|
66 */
|
Chris@0
|
67 public function getLangcode() {
|
Chris@0
|
68 return $this->getParent()->getLangcode();
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * {@inheritdoc}
|
Chris@0
|
73 */
|
Chris@0
|
74 public function getFieldDefinition() {
|
Chris@0
|
75 return $this->definition->getFieldDefinition();
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * Returns the array of field settings.
|
Chris@0
|
80 *
|
Chris@0
|
81 * @return array
|
Chris@0
|
82 * The array of settings.
|
Chris@0
|
83 */
|
Chris@0
|
84 protected function getSettings() {
|
Chris@0
|
85 return $this->getFieldDefinition()->getSettings();
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 /**
|
Chris@0
|
89 * Returns the value of a field setting.
|
Chris@0
|
90 *
|
Chris@0
|
91 * @param string $setting_name
|
Chris@0
|
92 * The setting name.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @return mixed
|
Chris@0
|
95 * The setting value.
|
Chris@0
|
96 */
|
Chris@0
|
97 protected function getSetting($setting_name) {
|
Chris@0
|
98 return $this->getFieldDefinition()->getSetting($setting_name);
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * {@inheritdoc}
|
Chris@0
|
103 */
|
Chris@0
|
104 public function setValue($values, $notify = TRUE) {
|
Chris@0
|
105 // Treat the values as property value of the first property, if no array is
|
Chris@0
|
106 // given.
|
Chris@0
|
107 if (isset($values) && !is_array($values)) {
|
Chris@0
|
108 $keys = array_keys($this->definition->getPropertyDefinitions());
|
Chris@0
|
109 $values = [$keys[0] => $values];
|
Chris@0
|
110 }
|
Chris@0
|
111 parent::setValue($values, $notify);
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 /**
|
Chris@0
|
115 * {@inheritdoc}
|
Chris@0
|
116 *
|
Chris@0
|
117 * Different to the parent Map class, we avoid creating property objects as
|
Chris@0
|
118 * far as possible in order to optimize performance. Thus we just update
|
Chris@0
|
119 * $this->values if no property object has been created yet.
|
Chris@0
|
120 */
|
Chris@0
|
121 protected function writePropertyValue($property_name, $value) {
|
Chris@0
|
122 // For defined properties there is either a property object or a plain
|
Chris@0
|
123 // value that needs to be updated.
|
Chris@0
|
124 if (isset($this->properties[$property_name])) {
|
Chris@0
|
125 $this->properties[$property_name]->setValue($value, FALSE);
|
Chris@0
|
126 }
|
Chris@0
|
127 // Allow setting plain values for not-defined properties also.
|
Chris@0
|
128 else {
|
Chris@0
|
129 $this->values[$property_name] = $value;
|
Chris@0
|
130 }
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * {@inheritdoc}
|
Chris@0
|
135 */
|
Chris@0
|
136 public function __get($name) {
|
Chris@0
|
137 // There is either a property object or a plain value - possibly for a
|
Chris@0
|
138 // not-defined property. If we have a plain value, directly return it.
|
Chris@0
|
139 if (isset($this->properties[$name])) {
|
Chris@0
|
140 return $this->properties[$name]->getValue();
|
Chris@0
|
141 }
|
Chris@0
|
142 elseif (isset($this->values[$name])) {
|
Chris@0
|
143 return $this->values[$name];
|
Chris@0
|
144 }
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 /**
|
Chris@0
|
148 * {@inheritdoc}
|
Chris@0
|
149 */
|
Chris@0
|
150 public function __set($name, $value) {
|
Chris@0
|
151 // Support setting values via property objects, but take care in as the
|
Chris@0
|
152 // value of the 'entity' property is typed data also.
|
Chris@0
|
153 if ($value instanceof TypedDataInterface && !($value instanceof EntityInterface)) {
|
Chris@0
|
154 $value = $value->getValue();
|
Chris@0
|
155 }
|
Chris@0
|
156 $this->set($name, $value);
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 /**
|
Chris@0
|
160 * {@inheritdoc}
|
Chris@0
|
161 */
|
Chris@0
|
162 public function __isset($name) {
|
Chris@0
|
163 if (isset($this->properties[$name])) {
|
Chris@0
|
164 return $this->properties[$name]->getValue() !== NULL;
|
Chris@0
|
165 }
|
Chris@0
|
166 return isset($this->values[$name]);
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 /**
|
Chris@0
|
170 * {@inheritdoc}
|
Chris@0
|
171 */
|
Chris@0
|
172 public function __unset($name) {
|
Chris@0
|
173 if ($this->definition->getPropertyDefinition($name)) {
|
Chris@0
|
174 $this->set($name, NULL);
|
Chris@0
|
175 }
|
Chris@0
|
176 else {
|
Chris@0
|
177 // Explicitly unset the property in $this->values if a non-defined
|
Chris@0
|
178 // property is unset, such that its key is removed from $this->values.
|
Chris@0
|
179 unset($this->values[$name]);
|
Chris@0
|
180 }
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 /**
|
Chris@0
|
184 * {@inheritdoc}
|
Chris@0
|
185 */
|
Chris@0
|
186 public function view($display_options = []) {
|
Chris@0
|
187 $view_builder = \Drupal::entityManager()->getViewBuilder($this->getEntity()->getEntityTypeId());
|
Chris@0
|
188 return $view_builder->viewFieldItem($this, $display_options);
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 /**
|
Chris@0
|
192 * {@inheritdoc}
|
Chris@0
|
193 */
|
Chris@0
|
194 public function preSave() {}
|
Chris@0
|
195
|
Chris@0
|
196 /**
|
Chris@0
|
197 * {@inheritdoc}
|
Chris@0
|
198 */
|
Chris@0
|
199 public function postSave($update) {}
|
Chris@0
|
200
|
Chris@0
|
201 /**
|
Chris@0
|
202 * {@inheritdoc}
|
Chris@0
|
203 */
|
Chris@0
|
204 public function delete() {}
|
Chris@0
|
205
|
Chris@0
|
206 /**
|
Chris@0
|
207 * {@inheritdoc}
|
Chris@0
|
208 */
|
Chris@0
|
209 public static function generateSampleValue(FieldDefinitionInterface $field_definition) {}
|
Chris@0
|
210
|
Chris@0
|
211 /**
|
Chris@0
|
212 * {@inheritdoc}
|
Chris@0
|
213 */
|
Chris@0
|
214 public function deleteRevision() {}
|
Chris@0
|
215
|
Chris@0
|
216 /**
|
Chris@0
|
217 * {@inheritdoc}
|
Chris@0
|
218 */
|
Chris@0
|
219 public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
|
Chris@0
|
220 return [];
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@0
|
223 /**
|
Chris@0
|
224 * {@inheritdoc}
|
Chris@0
|
225 */
|
Chris@0
|
226 public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
227 return [];
|
Chris@0
|
228 }
|
Chris@0
|
229
|
Chris@0
|
230 /**
|
Chris@0
|
231 * {@inheritdoc}
|
Chris@0
|
232 */
|
Chris@0
|
233 public static function storageSettingsToConfigData(array $settings) {
|
Chris@0
|
234 return $settings;
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 /**
|
Chris@0
|
238 * {@inheritdoc}
|
Chris@0
|
239 */
|
Chris@0
|
240 public static function storageSettingsFromConfigData(array $settings) {
|
Chris@0
|
241 return $settings;
|
Chris@0
|
242 }
|
Chris@0
|
243
|
Chris@0
|
244 /**
|
Chris@0
|
245 * {@inheritdoc}
|
Chris@0
|
246 */
|
Chris@0
|
247 public static function fieldSettingsToConfigData(array $settings) {
|
Chris@0
|
248 return $settings;
|
Chris@0
|
249 }
|
Chris@0
|
250
|
Chris@0
|
251 /**
|
Chris@0
|
252 * {@inheritdoc}
|
Chris@0
|
253 */
|
Chris@0
|
254 public static function fieldSettingsFromConfigData(array $settings) {
|
Chris@0
|
255 return $settings;
|
Chris@0
|
256 }
|
Chris@0
|
257
|
Chris@0
|
258 /**
|
Chris@0
|
259 * {@inheritdoc}
|
Chris@0
|
260 */
|
Chris@0
|
261 public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
|
Chris@0
|
262 return [];
|
Chris@0
|
263 }
|
Chris@0
|
264
|
Chris@0
|
265 /**
|
Chris@0
|
266 * {@inheritdoc}
|
Chris@0
|
267 */
|
Chris@0
|
268 public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
|
Chris@0
|
269 return [];
|
Chris@0
|
270 }
|
Chris@0
|
271
|
Chris@0
|
272 /**
|
Chris@0
|
273 * {@inheritdoc}
|
Chris@0
|
274 */
|
Chris@0
|
275 public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) {
|
Chris@0
|
276 return FALSE;
|
Chris@0
|
277 }
|
Chris@0
|
278
|
Chris@0
|
279 }
|