Mercurial > hg > cmmr2012-drupal-site
comparison core/lib/Drupal/Core/Field/FieldDefinition.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Core\Field; | |
4 | |
5 use Drupal\Core\Cache\UnchangingCacheableDependencyTrait; | |
6 use Drupal\Core\Entity\FieldableEntityInterface; | |
7 use Drupal\Core\Field\TypedData\FieldItemDataDefinition; | |
8 use Drupal\Core\TypedData\ListDataDefinition; | |
9 | |
10 /** | |
11 * A class for defining entity field definitions. | |
12 * | |
13 * A field definition in the context of a bundle field is different from a base | |
14 * field in that it may exist only for one or more bundles of an entity type. A | |
15 * bundle field definition may also override the definition of an existing base | |
16 * field definition on a per bundle basis. The bundle field definition is used | |
17 * for code driven overrides, while the | |
18 * \Drupal\Core\Field\Entity\BaseFieldOverride uses config to override the base | |
19 * field definition. | |
20 * | |
21 * Bundle fields can be defined in code using hook_entity_bundle_field_info() or | |
22 * via the | |
23 * \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions() method | |
24 * when defining an entity type. All bundle fields require an associated storage | |
25 * definition. A storage definition may have automatically been defined when | |
26 * overriding a base field or it may be manually provided via | |
27 * hook_entity_field_storage_info(). | |
28 * | |
29 * @see \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions() | |
30 * @see \Drupal\Core\Field\FieldDefinitionInterface | |
31 * @see \Drupal\Core\Field\FieldStorageDefinitionInterface | |
32 * @see hook_entity_bundle_field_info() | |
33 * @see hook_entity_field_storage_info() | |
34 */ | |
35 class FieldDefinition extends ListDataDefinition implements FieldDefinitionInterface { | |
36 | |
37 use UnchangingCacheableDependencyTrait; | |
38 use FieldInputValueNormalizerTrait; | |
39 | |
40 /** | |
41 * The associated field storage definition. | |
42 * | |
43 * @var \Drupal\Core\Field\FieldStorageDefinitionInterface | |
44 */ | |
45 protected $fieldStorageDefinition; | |
46 | |
47 /** | |
48 * Creates a new field definition. | |
49 * | |
50 * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storageDefinition | |
51 * The associated field storage definition. | |
52 * | |
53 * @return static | |
54 */ | |
55 public static function createFromFieldStorageDefinition(FieldStorageDefinitionInterface $storageDefinition) { | |
56 $field_definition = new static(); | |
57 $field_definition->setFieldStorageDefinition($storageDefinition); | |
58 return $field_definition; | |
59 } | |
60 | |
61 /** | |
62 * {@inheritdoc} | |
63 */ | |
64 public function getName() { | |
65 return $this->getFieldStorageDefinition()->getName(); | |
66 } | |
67 | |
68 /** | |
69 * {@inheritdoc} | |
70 */ | |
71 public function getType() { | |
72 return $this->getFieldStorageDefinition()->getType(); | |
73 } | |
74 | |
75 /** | |
76 * {@inheritdoc} | |
77 */ | |
78 public function getTargetEntityTypeId() { | |
79 return $this->getFieldStorageDefinition()->getTargetEntityTypeId(); | |
80 } | |
81 | |
82 /** | |
83 * Set the target bundle. | |
84 * | |
85 * @param string $bundle | |
86 * The target bundle. | |
87 * | |
88 * @return $this | |
89 */ | |
90 public function setTargetBundle($bundle) { | |
91 $this->definition['bundle'] = $bundle; | |
92 return $this; | |
93 } | |
94 | |
95 /** | |
96 * {@inheritdoc} | |
97 */ | |
98 public function getTargetBundle() { | |
99 return $this->definition['bundle']; | |
100 } | |
101 | |
102 /** | |
103 * Sets whether the display for the field can be configured. | |
104 * | |
105 * @param string $display_context | |
106 * The display context. Either 'view' or 'form'. | |
107 * @param bool $configurable | |
108 * Whether the display options can be configured (e.g., via the "Manage | |
109 * display" / "Manage form display" UI screens). If TRUE, the options | |
110 * specified via getDisplayOptions() act as defaults. | |
111 * | |
112 * @return $this | |
113 */ | |
114 public function setDisplayConfigurable($display_context, $configurable) { | |
115 // If no explicit display options have been specified, default to 'hidden'. | |
116 if (empty($this->definition['display'][$display_context])) { | |
117 $this->definition['display'][$display_context]['options'] = ['region' => 'hidden']; | |
118 } | |
119 $this->definition['display'][$display_context]['configurable'] = $configurable; | |
120 return $this; | |
121 } | |
122 | |
123 /** | |
124 * {@inheritdoc} | |
125 */ | |
126 public function isDisplayConfigurable($display_context) { | |
127 return isset($this->definition['display'][$display_context]['configurable']) ? $this->definition['display'][$display_context]['configurable'] : FALSE; | |
128 } | |
129 | |
130 /** | |
131 * Sets the display options for the field in forms or rendered entities. | |
132 * | |
133 * This enables generic rendering of the field with widgets / formatters, | |
134 * including automated support for "In place editing", and with optional | |
135 * configurability in the "Manage display" / "Manage form display" UI screens. | |
136 * | |
137 * Unless this method is called, the field remains invisible (or requires | |
138 * ad-hoc rendering logic). | |
139 * | |
140 * @param string $display_context | |
141 * The display context. Either 'view' or 'form'. | |
142 * @param array $options | |
143 * An array of display options. Refer to | |
144 * \Drupal\Core\Field\FieldDefinitionInterface::getDisplayOptions() for | |
145 * a list of supported keys. The options should include at least a 'weight', | |
146 * or specify 'type' = 'hidden'. The 'default_widget' / 'default_formatter' | |
147 * for the field type will be used if no 'type' is specified. | |
148 * | |
149 * @return $this | |
150 */ | |
151 public function setDisplayOptions($display_context, array $options) { | |
152 $this->definition['display'][$display_context]['options'] = $options; | |
153 return $this; | |
154 } | |
155 | |
156 /** | |
157 * {@inheritdoc} | |
158 */ | |
159 public function getDisplayOptions($display_context) { | |
160 return isset($this->definition['display'][$display_context]['options']) ? $this->definition['display'][$display_context]['options'] : NULL; | |
161 } | |
162 | |
163 /** | |
164 * {@inheritdoc} | |
165 */ | |
166 public function getDefaultValueLiteral() { | |
167 return isset($this->definition['default_value']) ? $this->definition['default_value'] : []; | |
168 } | |
169 | |
170 /** | |
171 * Set the default value callback for the field. | |
172 * | |
173 * @param string $callback | |
174 * The default value callback. | |
175 * | |
176 * @return $this | |
177 */ | |
178 public function setDefaultValueCallback($callback) { | |
179 if (isset($callback) && !is_string($callback)) { | |
180 throw new \InvalidArgumentException('Default value callback must be a string, like "function_name" or "ClassName::methodName"'); | |
181 } | |
182 $this->definition['default_value_callback'] = $callback; | |
183 return $this; | |
184 } | |
185 | |
186 /** | |
187 * {@inheritdoc} | |
188 */ | |
189 public function getDefaultValueCallback() { | |
190 return isset($this->definition['default_value_callback']) ? $this->definition['default_value_callback'] : NULL; | |
191 } | |
192 | |
193 /** | |
194 * Set a default value for the field. | |
195 * | |
196 * @param mixed $value | |
197 * The default value. | |
198 * | |
199 * @return $this | |
200 */ | |
201 public function setDefaultValue($value) { | |
202 $this->definition['default_value'] = $this->normalizeValue($value, $this->getFieldStorageDefinition()->getMainPropertyName()); | |
203 return $this; | |
204 } | |
205 | |
206 /** | |
207 * {@inheritdoc} | |
208 */ | |
209 public function getDefaultValue(FieldableEntityInterface $entity) { | |
210 // Allow custom default values function. | |
211 if ($callback = $this->getDefaultValueCallback()) { | |
212 $value = call_user_func($callback, $entity, $this); | |
213 } | |
214 else { | |
215 $value = $this->getDefaultValueLiteral(); | |
216 } | |
217 $value = $this->normalizeValue($value, $this->getFieldStorageDefinition()->getMainPropertyName()); | |
218 // Allow the field type to process default values. | |
219 $field_item_list_class = $this->getClass(); | |
220 return $field_item_list_class::processDefaultValue($value, $entity, $this); | |
221 } | |
222 | |
223 /** | |
224 * Sets whether the field is translatable. | |
225 * | |
226 * @param bool $translatable | |
227 * Whether the field is translatable. | |
228 * | |
229 * @return $this | |
230 */ | |
231 public function setTranslatable($translatable) { | |
232 $this->definition['translatable'] = $translatable; | |
233 return $this; | |
234 } | |
235 | |
236 /** | |
237 * {@inheritdoc} | |
238 */ | |
239 public function isTranslatable() { | |
240 return !empty($this->definition['translatable']) && $this->getFieldStorageDefinition()->isTranslatable(); | |
241 } | |
242 | |
243 /** | |
244 * Set the field storage definition. | |
245 * | |
246 * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storageDefinition | |
247 * The field storage definition associated with this field definition. | |
248 * | |
249 * @return $this | |
250 */ | |
251 public function setFieldStorageDefinition(FieldStorageDefinitionInterface $storageDefinition) { | |
252 $this->fieldStorageDefinition = $storageDefinition; | |
253 $this->itemDefinition = FieldItemDataDefinition::create($this); | |
254 // Create a definition for the items, and initialize it with the default | |
255 // settings for the field type. | |
256 $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); | |
257 $default_settings = $field_type_manager->getDefaultFieldSettings($storageDefinition->getType()); | |
258 $this->itemDefinition->setSettings($default_settings); | |
259 return $this; | |
260 } | |
261 | |
262 /** | |
263 * {@inheritdoc} | |
264 */ | |
265 public function getFieldStorageDefinition() { | |
266 return $this->fieldStorageDefinition; | |
267 } | |
268 | |
269 /** | |
270 * {@inheritdoc} | |
271 */ | |
272 public function getConfig($bundle) { | |
273 // @todo provide a FieldDefinitionOverride config entity in | |
274 // https://www.drupal.org/project/drupal/issues/2935978. | |
275 throw new \Exception('Field definitions do not currently have an override config entity.'); | |
276 } | |
277 | |
278 /** | |
279 * {@inheritdoc} | |
280 */ | |
281 public function getUniqueIdentifier() { | |
282 return $this->getTargetEntityTypeId() . '-' . $this->getTargetBundle() . '-' . $this->getName(); | |
283 } | |
284 | |
285 /** | |
286 * {@inheritdoc} | |
287 */ | |
288 public function getSetting($setting_name) { | |
289 if (array_key_exists($setting_name, $this->itemDefinition->getSettings())) { | |
290 return $this->itemDefinition->getSetting($setting_name); | |
291 } | |
292 else { | |
293 return $this->getFieldStorageDefinition()->getSetting($setting_name); | |
294 } | |
295 } | |
296 | |
297 /** | |
298 * {@inheritdoc} | |
299 */ | |
300 public function getSettings() { | |
301 return $this->getItemDefinition()->getSettings() + $this->getFieldStorageDefinition()->getSettings(); | |
302 } | |
303 | |
304 /** | |
305 * {@inheritdoc} | |
306 */ | |
307 public function setSetting($setting_name, $value) { | |
308 $this->getItemDefinition()->setSetting($setting_name, $value); | |
309 return $this; | |
310 } | |
311 | |
312 /** | |
313 * {@inheritdoc} | |
314 */ | |
315 public function setSettings(array $settings) { | |
316 // Assign settings individually, in order to keep the current values | |
317 // of settings not specified in $settings. | |
318 foreach ($settings as $setting_name => $setting) { | |
319 $this->getItemDefinition()->setSetting($setting_name, $setting); | |
320 } | |
321 return $this; | |
322 } | |
323 | |
324 } |