Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / field / field.api.php
1 <?php
2
3 /**
4  * @file
5  * Field API documentation.
6  */
7
8 /**
9  * @addtogroup hooks
10  * @{
11  */
12
13 /**
14  * @defgroup field_types Field Types API
15  * @{
16  * Defines field, widget, display formatter, and storage types.
17  *
18  * In the Field API, each field has a type, which determines what kind of data
19  * (integer, string, date, etc.) the field can hold, which settings it provides,
20  * and so on. The data type(s) accepted by a field are defined in
21  * hook_field_schema().
22  *
23  * Field types are plugins annotated with class
24  * \Drupal\Core\Field\Annotation\FieldType, and implement plugin interface
25  * \Drupal\Core\Field\FieldItemInterface. Field Type plugins are managed by the
26  * \Drupal\Core\Field\FieldTypePluginManager class. Field type classes usually
27  * extend base class \Drupal\Core\Field\FieldItemBase. Field-type plugins need
28  * to be in the namespace \Drupal\{your_module}\Plugin\Field\FieldType. See the
29  * @link plugin_api Plugin API topic @endlink for more information on how to
30  * define plugins.
31  *
32  * The Field Types API also defines two kinds of pluggable handlers: widgets
33  * and formatters. @link field_widget Widgets @endlink specify how the field
34  * appears in edit forms, while @link field_formatter formatters @endlink
35  * specify how the field appears in displayed entities.
36  *
37  * See @link field Field API @endlink for information about the other parts of
38  * the Field API.
39  *
40  * @see field
41  * @see field_widget
42  * @see field_formatter
43  * @see plugin_api
44  */
45
46
47 /**
48  * Perform alterations on Field API field types.
49  *
50  * @param $info
51  *   Array of information on field types as collected by the "field type" plugin
52  *   manager.
53  */
54 function hook_field_info_alter(&$info) {
55   // Change the default widget for fields of type 'foo'.
56   if (isset($info['foo'])) {
57     $info['foo']['default widget'] = 'mymodule_widget';
58   }
59 }
60
61 /**
62  * Perform alterations on preconfigured field options.
63  *
64  * @param array $options
65  *   Array of options as returned from
66  *   \Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions().
67  * @param string $field_type
68  *   The field type plugin ID.
69  *
70  * @see \Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions()
71  */
72 function hook_field_ui_preconfigured_options_alter(array &$options, $field_type) {
73   // If the field is not an "entity_reference"-based field, bail out.
74   /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
75   $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
76   $class = $field_type_manager->getPluginClass($field_type);
77   if (!is_a($class, 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem', TRUE)) {
78     return;
79   }
80
81   // Set the default formatter for media in entity reference fields to be the
82   // "Rendered entity" formatter.
83   if (!empty($options['media'])) {
84     $options['media']['entity_view_display']['type'] = 'entity_reference_entity_view';
85   }
86 }
87
88 /**
89  * Forbid a field storage update from occurring.
90  *
91  * Any module may forbid any update for any reason. For example, the
92  * field's storage module might forbid an update if it would change
93  * the storage schema while data for the field exists. A field type
94  * module might forbid an update if it would change existing data's
95  * semantics, or if there are external dependencies on field settings
96  * that cannot be updated.
97  *
98  * To forbid the update from occurring, throw a
99  * \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException.
100  *
101  * @param \Drupal\field\FieldStorageConfigInterface $field_storage
102  *   The field storage as it will be post-update.
103  * @param \Drupal\field\FieldStorageConfigInterface $prior_field_storage
104  *   The field storage as it is pre-update.
105  *
106  * @see entity_crud
107  */
108 function hook_field_storage_config_update_forbid(\Drupal\field\FieldStorageConfigInterface $field_storage, \Drupal\field\FieldStorageConfigInterface $prior_field_storage) {
109   if ($field_storage->module == 'options' && $field_storage->hasData()) {
110     // Forbid any update that removes allowed values with actual data.
111     $allowed_values = $field_storage->getSetting('allowed_values');
112     $prior_allowed_values = $prior_field_storage->getSetting('allowed_values');
113     $lost_keys = array_keys(array_diff_key($prior_allowed_values, $allowed_values));
114     if (_options_values_in_use($field_storage->getTargetEntityTypeId(), $field_storage->getName(), $lost_keys)) {
115       throw new \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException(t('A list field (@field_name) with existing data cannot have its keys changed.', ['@field_name' => $field_storage->getName()]));
116     }
117   }
118 }
119
120 /**
121  * @} End of "defgroup field_types".
122  */
123
124 /**
125  * @defgroup field_widget Field Widget API
126  * @{
127  * Define Field API widget types.
128  *
129  * Field API widgets specify how fields are displayed in edit forms. Fields of a
130  * given @link field_types field type @endlink may be edited using more than one
131  * widget. In this case, the Field UI module allows the site builder to choose
132  * which widget to use.
133  *
134  * Widgets are Plugins managed by the
135  * \Drupal\Core\Field\WidgetPluginManager class. A widget is a plugin annotated
136  * with class \Drupal\Core\Field\Annotation\FieldWidget that implements
137  * \Drupal\Core\Field\WidgetInterface (in most cases, by
138  * subclassing \Drupal\Core\Field\WidgetBase). Widget plugins need to be in the
139  * namespace \Drupal\{your_module}\Plugin\Field\FieldWidget.
140  *
141  * Widgets are @link form_api Form API @endlink elements with additional
142  * processing capabilities. The methods of the WidgetInterface object are
143  * typically called by respective methods in the
144  * \Drupal\Core\Entity\Entity\EntityFormDisplay class.
145  *
146  * @see field
147  * @see field_types
148  * @see field_formatter
149  * @see plugin_api
150  */
151
152 /**
153  * Perform alterations on Field API widget types.
154  *
155  * @param array $info
156  *   An array of information on existing widget types, as collected by the
157  *   annotation discovery mechanism.
158  */
159 function hook_field_widget_info_alter(array &$info) {
160   // Let a new field type re-use an existing widget.
161   $info['options_select']['field_types'][] = 'my_field_type';
162 }
163
164 /**
165  * Alter forms for field widgets provided by other modules.
166  *
167  * This hook can only modify individual elements within a field widget and
168  * cannot alter the top level (parent element) for multi-value fields. In most
169  * cases, you should use hook_field_widget_multivalue_form_alter() instead and
170  * loop over the elements.
171  *
172  * @param $element
173  *   The field widget form element as constructed by
174  *   \Drupal\Core\Field\WidgetBaseInterface::form().
175  * @param $form_state
176  *   The current state of the form.
177  * @param $context
178  *   An associative array containing the following key-value pairs:
179  *   - form: The form structure to which widgets are being attached. This may be
180  *     a full form structure, or a sub-element of a larger form.
181  *   - widget: The widget plugin instance.
182  *   - items: The field values, as a
183  *     \Drupal\Core\Field\FieldItemListInterface object.
184  *   - delta: The order of this item in the array of subelements (0, 1, 2, etc).
185  *   - default: A boolean indicating whether the form is being shown as a dummy
186  *     form to set default values.
187  *
188  * @see \Drupal\Core\Field\WidgetBaseInterface::form()
189  * @see \Drupal\Core\Field\WidgetBase::formSingleElement()
190  * @see hook_field_widget_WIDGET_TYPE_form_alter()
191  * @see hook_field_widget_multivalue_form_alter()
192  */
193 function hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
194   // Add a css class to widget form elements for all fields of type mytype.
195   $field_definition = $context['items']->getFieldDefinition();
196   if ($field_definition->getType() == 'mytype') {
197     // Be sure not to overwrite existing attributes.
198     $element['#attributes']['class'][] = 'myclass';
199   }
200 }
201
202 /**
203  * Alter widget forms for a specific widget provided by another module.
204  *
205  * Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a
206  * specific widget form, rather than using hook_field_widget_form_alter() and
207  * checking the widget type.
208  *
209  * This hook can only modify individual elements within a field widget and
210  * cannot alter the top level (parent element) for multi-value fields. In most
211  * cases, you should use hook_field_widget_multivalue_WIDGET_TYPE_form_alter()
212  * instead and loop over the elements.
213  *
214  * @param $element
215  *   The field widget form element as constructed by
216  *   \Drupal\Core\Field\WidgetBaseInterface::form().
217  * @param $form_state
218  *   The current state of the form.
219  * @param $context
220  *   An associative array. See hook_field_widget_form_alter() for the structure
221  *   and content of the array.
222  *
223  * @see \Drupal\Core\Field\WidgetBaseInterface::form()
224  * @see \Drupal\Core\Field\WidgetBase::formSingleElement()
225  * @see hook_field_widget_form_alter()
226  * @see hook_field_widget_multivalue_WIDGET_TYPE_form_alter()
227  */
228 function hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
229   // Code here will only act on widgets of type WIDGET_TYPE.  For example,
230   // hook_field_widget_mymodule_autocomplete_form_alter() will only act on
231   // widgets of type 'mymodule_autocomplete'.
232   $element['#autocomplete_route_name'] = 'mymodule.autocomplete_route';
233 }
234
235 /**
236  * Alter forms for multi-value field widgets provided by other modules.
237  *
238  * To alter the individual elements within the widget, loop over
239  * \Drupal\Core\Render\Element::children($elements).
240  *
241  * @param array $elements
242  *   The field widget form elements as constructed by
243  *   \Drupal\Core\Field\WidgetBase::formMultipleElements().
244  * @param \Drupal\Core\Form\FormStateInterface $form_state
245  *   The current state of the form.
246  * @param array $context
247  *   An associative array containing the following key-value pairs:
248  *   - form: The form structure to which widgets are being attached. This may be
249  *     a full form structure, or a sub-element of a larger form.
250  *   - widget: The widget plugin instance.
251  *   - items: The field values, as a
252  *     \Drupal\Core\Field\FieldItemListInterface object.
253  *   - default: A boolean indicating whether the form is being shown as a dummy
254  *     form to set default values.
255  *
256  * @see \Drupal\Core\Field\WidgetBaseInterface::form()
257  * @see \Drupal\Core\Field\WidgetBase::formMultipleElements()
258  * @see hook_field_widget_multivalue_WIDGET_TYPE_form_alter()
259  */
260 function hook_field_widget_multivalue_form_alter(array &$elements, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
261   // Add a css class to widget form elements for all fields of type mytype.
262   $field_definition = $context['items']->getFieldDefinition();
263   if ($field_definition->getType() == 'mytype') {
264     // Be sure not to overwrite existing attributes.
265     $elements['#attributes']['class'][] = 'myclass';
266   }
267 }
268
269 /**
270  * Alter multi-value widget forms for a widget provided by another module.
271  *
272  * Modules can implement hook_field_widget_multivalue_WIDGET_TYPE_form_alter() to
273  * modify a specific widget form, rather than using
274  * hook_field_widget_form_alter() and checking the widget type.
275  *
276  * To alter the individual elements within the widget, loop over
277  * \Drupal\Core\Render\Element::children($elements).
278  *
279  * @param array $elements
280  *   The field widget form elements as constructed by
281  *   \Drupal\Core\Field\WidgetBase::formMultipleElements().
282  * @param \Drupal\Core\Form\FormStateInterface $form_state
283  *   The current state of the form.
284  * @param array $context
285  *   An associative array. See hook_field_widget_multivalue_form_alter() for
286  *   the structure and content of the array.
287  *
288  * @see \Drupal\Core\Field\WidgetBaseInterface::form()
289  * @see \Drupal\Core\Field\WidgetBase::formMultipleElements()
290  * @see hook_field_widget_multivalue_form_alter()
291  */
292 function hook_field_widget_multivalue_WIDGET_TYPE_form_alter(array &$elements, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
293   // Code here will only act on widgets of type WIDGET_TYPE. For example,
294   // hook_field_widget_multivalue_mymodule_autocomplete_form_alter() will only
295   // act on widgets of type 'mymodule_autocomplete'.
296   // Change the autcomplete route for each autocomplete element within the
297   // multivalue widget.
298   foreach (Element::children($elements) as $delta => $element) {
299     $elements[$delta]['#autocomplete_route_name'] = 'mymodule.autocomplete_route';
300   }
301 }
302
303 /**
304  * @} End of "defgroup field_widget".
305  */
306
307
308 /**
309  * @defgroup field_formatter Field Formatter API
310  * @{
311  * Define Field API formatter types.
312  *
313  * Field API formatters specify how fields are displayed when the entity to
314  * which the field is attached is displayed. Fields of a given
315  * @link field_types field type @endlink may be displayed using more than one
316  * formatter. In this case, the Field UI module allows the site builder to
317  * choose which formatter to use.
318  *
319  * Formatters are Plugins managed by the
320  * \Drupal\Core\Field\FormatterPluginManager class. A formatter is a plugin
321  * annotated with class \Drupal\Core\Field\Annotation\FieldFormatter that
322  * implements \Drupal\Core\Field\FormatterInterface (in most cases, by
323  * subclassing \Drupal\Core\Field\FormatterBase). Formatter plugins need to be
324  * in the namespace \Drupal\{your_module}\Plugin\Field\FieldFormatter.
325  *
326  * @see field
327  * @see field_types
328  * @see field_widget
329  * @see plugin_api
330  */
331
332 /**
333  * Perform alterations on Field API formatter types.
334  *
335  * @param array $info
336  *   An array of information on existing formatter types, as collected by the
337  *   annotation discovery mechanism.
338  */
339 function hook_field_formatter_info_alter(array &$info) {
340   // Let a new field type re-use an existing formatter.
341   $info['text_default']['field_types'][] = 'my_field_type';
342 }
343
344 /**
345  * @} End of "defgroup field_formatter".
346  */
347
348 /**
349  * Returns the maximum weight for the entity components handled by the module.
350  *
351  * Field API takes care of fields and 'extra_fields'. This hook is intended for
352  * third-party modules adding other entity components (e.g. field_group).
353  *
354  * @param string $entity_type
355  *   The type of entity; e.g. 'node' or 'user'.
356  * @param string $bundle
357  *   The bundle name.
358  * @param string $context
359  *   The context for which the maximum weight is requested. Either 'form' or
360  *   'display'.
361  * @param string $context_mode
362  *   The view or form mode name.
363  *
364  * @return int
365  *   The maximum weight of the entity's components, or NULL if no components
366  *   were found.
367  *
368  * @ingroup field_info
369  */
370 function hook_field_info_max_weight($entity_type, $bundle, $context, $context_mode) {
371   $weights = [];
372
373   foreach (my_module_entity_additions($entity_type, $bundle, $context, $context_mode) as $addition) {
374     $weights[] = $addition['weight'];
375   }
376
377   return $weights ? max($weights) : NULL;
378 }
379
380 /**
381  * @addtogroup field_purge
382  * @{
383  */
384
385 /**
386  * Acts when a field storage definition is being purged.
387  *
388  * In field_purge_field_storage(), after the storage definition has been removed
389  * from the system, the entity storage has purged stored field data, and the
390  * field definitions cache has been cleared, this hook is invoked on all modules
391  * to allow them to respond to the field storage being purged.
392  *
393  * @param $field_storage \Drupal\field\Entity\FieldStorageConfig
394  *   The field storage being purged.
395  */
396 function hook_field_purge_field_storage(\Drupal\field\Entity\FieldStorageConfig $field_storage) {
397   db_delete('my_module_field_storage_info')
398     ->condition('uuid', $field_storage->uuid())
399     ->execute();
400 }
401
402 /**
403  * Acts when a field is being purged.
404  *
405  * In field_purge_field(), after the field definition has been removed
406  * from the system, the entity storage has purged stored field data, and the
407  * field info cache has been cleared, this hook is invoked on all modules to
408  * allow them to respond to the field being purged.
409  *
410  * @param $field
411  *   The field being purged.
412  */
413 function hook_field_purge_field(\Drupal\field\Entity\FieldConfig $field) {
414   db_delete('my_module_field_info')
415     ->condition('id', $field->id())
416     ->execute();
417 }
418
419 /**
420  * @} End of "addtogroup field_purge".
421  */
422
423 /**
424  * @} End of "addtogroup hooks".
425  */