comparison core/lib/Drupal/Core/Field/FieldItemBase.php @ 0:4c8ae668cc8c

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