field.deprecated.inc 33.3 KB
Newer Older
1
2
3
4
5
6
7
8
<?php

/**
 * @file
 * This file contains functions marked as deprecated.
 */

use Drupal\Core\Entity\EntityInterface;
9
use Drupal\Core\Entity\ContentEntityInterface;
10
use Drupal\Core\Language\Language;
11
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
12
use Drupal\Core\Field\FieldDefinitionInterface;
13
14
15
16
17
18
19
20
21
22
23
24
25
use Drupal\field\Field;

/**
 * Returns information about field types.
 *
 * @param $field_type
 *   (optional) A field type name. If omitted, all field types will be returned.
 *
 * @return
 *   Either a field type definition, or an array of all existing field types,
 *   keyed by field type name.
 *
 * @deprecated as of Drupal 8.0. Use
26
 *   \Drupal::service('plugin.manager.field.field_type')->getDefinition()
27
 *   or
28
 *   \Drupal::service('plugin.manager.field.field_type')->getConfigurableDefinitions().
29
30
31
 */
function field_info_field_types($field_type = NULL) {
  if ($field_type) {
32
    return \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_type);
33
34
  }
  else {
35
    return \Drupal::service('plugin.manager.field.field_type')->getConfigurableDefinitions();
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  }
}

/**
 * Returns a field type's default settings.
 *
 * @param $type
 *   A field type name.
 *
 * @return
 *   The field type's default settings, or an empty array if type or settings
 *   are not defined.
 *
 * @deprecated as of Drupal 8.0. Use
50
 *   \Drupal::service('plugin.manager.field.field_type')->getDefaultSettings()
51
52
 */
function field_info_field_settings($type) {
53
  return \Drupal::service('plugin.manager.field.field_type')->getDefaultSettings($type);
54
55
56
57
58
59
60
61
62
63
64
65
66
}

/**
 * Returns a field type's default instance settings.
 *
 * @param $type
 *   A field type name.
 *
 * @return
 *   The field type's default instance settings, or an empty array if type or
 *   settings are not defined.
 *
 * @deprecated as of Drupal 8.0. Use
67
 *   \Drupal::service('plugin.manager.field.field_type')->getDefaultInstanceSettings()
68
69
 */
function field_info_instance_settings($type) {
70
  return \Drupal::service('plugin.manager.field.field_type')->getDefaultInstanceSettings($type);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
}

/**
 * Returns information about field widgets from AnnotatedClassDiscovery.
 *
 * @param string $widget_type
 *   (optional) A widget type name. If omitted, all widget types will be
 *   returned.
 *
 * @return array
 *   Either a single widget type description, as provided by class annotations,
 *   or an array of all existing widget types, keyed by widget type name.
 *
 * @deprecated as of Drupal 8.0. Use
85
 *   \Drupal::service('plugin.manager.field.widget')->getDefinition()
86
 *   or
87
 *   \Drupal::service('plugin.manager.field.widget')->getDefinitions()
88
89
90
 */
function field_info_widget_types($widget_type = NULL) {
  if ($widget_type) {
91
    return \Drupal::service('plugin.manager.field.widget')->getDefinition($widget_type);
92
93
  }
  else {
94
    return \Drupal::service('plugin.manager.field.widget')->getDefinitions();
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  }
}

/**
 * Returns a field widget's default settings.
 *
 * @param $type
 *   A widget type name.
 *
 * @return
 *   The widget type's default settings, as provided by
 *   hook_field_widget_info(), or an empty array if type or settings are
 *   undefined.
 *
 * @deprecated as of Drupal 8.0. Use
110
 *   \Drupal::service('plugin.manager.field.widget')->getDefaultSettings()
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
 */
function field_info_widget_settings($type) {
  return \Drupal::service('plugin.manager.field.widget')->getDefaultSettings($type);
}

/**
 * Returns information about field formatters from hook_field_formatter_info().
 *
 * @param string $formatter_type
 *   (optional) A formatter type name. If omitted, all formatter types will be
 *   returned.
 *
 * @return array
 *   Either a single formatter type description, as provided by class
 *   annotations, or an array of all existing formatter types, keyed by
 *   formatter type name.
 *
 * @deprecated as of Drupal 8.0. Use
129
 *   \Drupal::service('plugin.manager.field.formatter')->getDefinition()
130
 *   or
131
 *   \Drupal::service('plugin.manager.field.formatter')->getDefinitions()
132
133
134
 */
function field_info_formatter_types($formatter_type = NULL) {
  if ($formatter_type) {
135
    return \Drupal::service('plugin.manager.field.formatter')->getDefinition($formatter_type);
136
137
  }
  else {
138
    return \Drupal::service('plugin.manager.field.formatter')->getDefinitions();
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  }
}

/**
 * Returns a field formatter's default settings.
 *
 * @param $type
 *   A field formatter type name.
 *
 * @return
 *   The formatter type's default settings, as provided by
 *   hook_field_formatter_info(), or an empty array if type or settings are
 *   undefined.
 *
 * @deprecated as of Drupal 8.0. Use
154
 *   \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings()
155
156
157
158
159
160
161
162
 */
function field_info_formatter_settings($type) {
  return \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings($type);
}

/**
 * Returns a lightweight map of fields across bundles.
 *
163
 * The function only returns non-deleted fields.
164
165
 *
 * @return
166
167
 *   An array keyed by entity type. Each value is an array which keys are
 *   field names and value is an array with two entries:
168
 *   - type: The field type.
169
 *   - bundles: The bundles in which the field appears.
170
171
172
 * Example:
 * @code
 * array(
173
174
175
176
177
178
 *   'node' => array(
 *     'body' => array(
 *       'bundles' => array(
 *         'page', 'article'
 *       ),
 *       'type' => 'text_with_summary',
179
180
181
182
183
184
185
186
187
188
189
190
191
 *     ),
 *   ),
 * );
 * @endcode
 *
 * @deprecated as of Drupal 8.0. Use
 *   Field::fieldInfo()->getFieldMap().
 */
function field_info_field_map() {
  return Field::fieldInfo()->getFieldMap();
}

/**
192
 * Returns data about an individual field.
193
 *
194
195
 * @param $entity_type
 *   The entity type.
196
197
 * @param $field_name
 *   The name of the field to retrieve. $field_name can only refer to a
198
 *   non-deleted field. For deleted fields, use field_info_field_by_id().
199
200
201
202
203
204
205
206
207
208
209
210
 *
 * @return
 *   The field array, as returned by field_read_fields(), with an
 *   additional element 'bundles', whose value is an array of all the bundles
 *   this field belongs to keyed by entity type. NULL if the field was not
 *   found.
 *
 * @see field_info_field_by_id()

 * @deprecated as of Drupal 8.0. Use
 *   Field::fieldInfo()->getField($field_name).
 */
211
212
function field_info_field($entity_type, $field_name) {
  return Field::fieldInfo()->getField($entity_type, $field_name);
213
214
215
216
217
218
}

/**
 * Returns data about an individual field, given a field ID.
 *
 * @param $field_id
219
 *   The ID of the field to retrieve. $field_id can refer to a deleted field.
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
 *
 * @return
 *   The field array, as returned by field_read_fields(), with an additional
 *   element 'bundles', whose value is an array of all the bundles this field
 *   belongs to.
 *
 * @see field_info_field()
 *
 * @deprecated as of Drupal 8.0. Use
 *   Field::fieldInfo()->getFieldById($field_id).
 */
function field_info_field_by_id($field_id) {
  return Field::fieldInfo()->getFieldById($field_id);
}

/**
 * Returns the same data as field_info_field_by_id() for every field.
 *
 * Use of this function should be avoided when possible, since it loads and
 * statically caches a potentially large array of information.
 *
 * When iterating over the fields present in a given bundle after a call to
 * field_info_instances($entity_type, $bundle), it is recommended to use
 * field_info_field() on each individual field instead.
 *
 * @return
 *   An array, each key is a field ID and the values are field arrays as
 *   returned by field_read_fields(), with an additional element 'bundles',
 *   whose value is an array of all the bundle this field belongs to.
 *
 * @see field_info_field()
 * @see field_info_field_by_id()
 *
 * @deprecated as of Drupal 8.0. Use
 *   Field::fieldInfo()->getFields().
 */
function field_info_field_by_ids() {
  return Field::fieldInfo()->getFields();
}

/**
 * Retrieves information about field instances.
 *
 * Use of this function to retrieve instances across separate bundles (i.e.
 * when the $bundle parameter is NULL) should be avoided when possible, since
 * it loads and statically caches a potentially large array of information.
 * Use field_info_field_map() instead.
 *
 * When retrieving the instances of a specific bundle (i.e. when both
 * $entity_type and $bundle_name are provided), the function also populates a
 * static cache with the corresponding field definitions, allowing fast
 * retrieval of field_info_field() later in the request.
 *
 * @param $entity_type
 *   (optional) The entity type for which to return instances.
 * @param $bundle_name
 *   (optional) The bundle name for which to return instances. If $entity_type
 *   is NULL, the $bundle_name parameter is ignored.
 *
 * @return
 *   If $entity_type is not set, return all instances keyed by entity type and
 *   bundle name. If $entity_type is set, return all instances for that entity
 *   type, keyed by bundle name. If $entity_type and $bundle_name are set,
 *   return all instances for that bundle.
 *
 * @deprecated as of Drupal 8.0. Use
 *   Field::fieldInfo()->getInstances(),
 *   Field::fieldInfo()->getInstances($entity_type) or
 *   Field::fieldInfo()->getBundleInstances($entity_type, $bundle_name).
 */
function field_info_instances($entity_type = NULL, $bundle_name = NULL) {
  $cache = Field::fieldInfo();

  if (!isset($entity_type)) {
    return $cache->getInstances();
  }

  if (!isset($bundle_name)) {
    return $cache->getInstances($entity_type);
  }

  return $cache->getBundleInstances($entity_type, $bundle_name);
}

/**
 * Returns an array of instance data for a specific field and bundle.
 *
 * The function populates a static cache with all fields and instances used in
 * the bundle, allowing fast retrieval of field_info_field() or
 * field_info_instance() later in the request.
 *
 * @param $entity_type
 *   The entity type for the instance.
 * @param $field_name
 *   The field name for the instance.
 * @param $bundle_name
 *   The bundle name for the instance.
 *
 * @return
 *   An associative array of instance data for the specific field and bundle;
 *   NULL if the instance does not exist.
 *
 * @deprecated as of Drupal 8.0. Use
323
 *   Field::fieldInfo()->getInstance($entity_type, $bundle_name, $field_name).
324
325
326
327
328
329
330
331
332
333
334
335
336
 */
function field_info_instance($entity_type, $field_name, $bundle_name) {
  return Field::fieldInfo()->getInstance($entity_type, $bundle_name, $field_name);
}

/**
 * Reads a single field record directly from the database.
 *
 * Generally, you should use the field_info_field() instead.
 *
 * This function will not return deleted fields. Use field_read_fields() instead
 * for this purpose.
 *
337
338
 * @param $entity_type
 *   The entity type.
339
340
341
 * @param $field_name
 *   The field name to read.
 * @param array $include_additional
342
 *   Additional properties to match.
343
344
345
346
347
348
349
 *
 * @return
 *   A field definition array, or FALSE.
 *
 * @deprecated as of Drupal 8.0. Use
 *   entity_load('field_entity', 'field_name').
 */
350
351
function field_read_field($entity_type, $field_name, $include_additional = array()) {
  $fields = field_read_fields(array('entity_type' => $entity_type, 'name' => $field_name), $include_additional);
352
353
354
355
356
357
358
  return $fields ? current($fields) : FALSE;
}

/**
 * Reads in fields that match an array of conditions.
 *
 * @param array $conditions
359
360
 *   An array of conditions to match against. Keys are names of properties
 *   found in field configuration files, and values are conditions to match.
361
 * @param array $include_additional
362
363
364
 *   The default behavior of this function is to not return fields that have
 *   been deleted. Setting $include_additional['include_deleted'] to TRUE will
 *   override this behavior.
365
366
367
368
369
370
371
372
373
374
375
376
377
378
 *
 * @return
 *   An array of fields matching $params. If
 *   $include_additional['include_deleted'] is TRUE, the array is keyed by
 *   field ID, otherwise it is keyed by field name.
 *
 * @deprecated as of Drupal 8.0. Use
 *   entity_load_multiple_by_properties('field_entity', $conditions).
 */
function field_read_fields($conditions = array(), $include_additional = array()) {
  // Include deleted fields if specified either in the $include_additional or
  // the $conditions parameters.
  $include_deleted = (isset($include_additional['include_deleted']) && $include_additional['include_deleted']) || (isset($conditions['deleted']) && $conditions['deleted']);

379
  // Pass include_deleted to the $conditions array.
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
  $conditions['include_deleted'] = $include_deleted;

  return entity_load_multiple_by_properties('field_entity', $conditions);
}

/**
 * Reads a single instance record from the database.
 *
 * Generally, you should use field_info_instance() instead, as it provides
 * caching and allows other modules the opportunity to append additional
 * formatters, widgets, and other information.
 *
 * @param $entity_type
 *   The type of entity to which the field is bound.
 * @param $field_name
 *   The field name to read.
 * @param $bundle
 *   The bundle to which the field is bound.
 * @param array $include_additional
 *   The default behavior of this function is to not return an instance that has
400
401
 *   been deleted. Setting $include_additional['include_deleted'] to TRUE will
 *   override this behavior.
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
 *
 * @return
 *   An instance structure, or FALSE.
 *
 * @deprecated as of Drupal 8.0. Use
 *   entity_load('field_instance', 'field_name').
 */
function field_read_instance($entity_type, $field_name, $bundle, $include_additional = array()) {
  $instances = field_read_instances(array('entity_type' => $entity_type, 'field_name' => $field_name, 'bundle' => $bundle), $include_additional);
  return $instances ? current($instances) : FALSE;
}

/**
 * Reads in field instances that match an array of conditions.
 *
 * @param $param
 *   An array of properties to use in selecting a field instance. Keys are names
 *   of properties found in field instance configuration files, and values are
 *   conditions to match.
 * @param $include_additional
 *   The default behavior of this function is to not return field instances that
423
 *   have been marked deleted. Setting
424
425
426
427
428
429
430
431
432
433
434
435
436
 *   $include_additional['include_deleted'] to TRUE will override this behavior.
 *
 * @return
 *   An array of instances matching the arguments.
 *
 * @deprecated as of Drupal 8.0. Use
 *   entity_load_multiple_by_properties('field_instance', $conditions).
 */
function field_read_instances($conditions = array(), $include_additional = array()) {
  // Include deleted instances if specified either in the $include_additional
  // or the $conditions parameters.
  $include_deleted = (isset($include_additional['include_deleted']) && $include_additional['include_deleted']) || (isset($conditions['deleted']) && $conditions['deleted']);

437
  // Pass include_deleted to the $conditions array.
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
  $conditions['include_deleted'] = $include_deleted;

  return entity_load_multiple_by_properties('field_instance', $conditions);
}

/**
 * Adds form elements for all fields for an entity to a form structure.
 *
 * The form elements for the entity's fields are added by reference as direct
 * children in the $form parameter. This parameter can be a full form structure
 * (most common case for entity edit forms), or a sub-element of a larger form.
 *
 * By default, submitted field values appear at the top-level of
 * $form_state['values']. A different location within $form_state['values'] can
 * be specified by setting the '#parents' property on the incoming $form
 * parameter. Because of name clashes, two instances of the same field cannot
 * appear within the same $form element, or within the same '#parents' space.
 *
 * For each call to field_attach_form(), field values are processed by calling
 * field_attach_extract_form_values() on the same $form element.
 *
 * Sample resulting structure in $form:
 * @code
 *   '#parents' => The location of field values in $form_state['values'],
 *   '#entity_type' => The name of the entity type,
 *   '#bundle' => The name of the bundle,
 *   // One sub-array per field appearing in the entity, keyed by field name.
 *   // The structure of the array differs slightly depending on whether the
 *   // widget is 'single-value' (provides the input for one field value,
 *   // most common case), and will therefore be repeated as many times as
 *   // needed, or 'multiple-values' (one single widget allows the input of
 *   // several values, e.g checkboxes, select box...).
 *   'field_foo' => array(
471
472
473
 *     '#access' => TRUE if the current user has 'edit' grants for the field,
 *       FALSE if not.
 *     'widget' => array(
474
475
476
477
478
479
480
481
482
483
484
485
 *       '#field_name' => The name of the field,
 *       '#language' => $langcode,
 *       '#field_parents' => The 'parents' space for the field in the form,
 *          equal to the #parents property of the $form parameter received by
 *          field_attach_form(),
 *       '#required' => Whether or not the field is required,
 *       '#title' => The label of the field instance,
 *       '#description' => The description text for the field instance,
 *
 *       // Only for 'single' widgets:
 *       '#theme' => 'field_multiple_value_form',
 *       '#cardinality' => The field cardinality,
486
487
 *       '#cardinality_multiple => TRUE if the field can contain multiple items,
 *         FALSE otherwise.
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
 *       // One sub-array per copy of the widget, keyed by delta.
 *       0 => array(
 *         '#entity_type' => The name of the entity type,
 *         '#bundle' => The name of the bundle,
 *         '#field_name' => The name of the field,
 *         '#field_parents' => The 'parents' space for the field in the form,
 *            equal to the #parents property of the $form parameter received by
 *            field_attach_form(),
 *         '#title' => The title to be displayed by the widget,
 *         '#default_value' => The field value for delta 0,
 *         '#required' => Whether the widget should be marked required,
 *         '#delta' => 0,
 *         // The remaining elements in the sub-array depend on the widget.
 *         '#type' => The type of the widget,
 *         ...
 *       ),
 *       1 => array(
 *         ...
 *       ),
 *
 *       // Only for multiple widgets:
 *       '#entity_type' => The name of the entity type,
 *       '#bundle' => $instance['bundle'],
 *       // The remaining elements in the sub-array depend on the widget.
 *       '#type' => The type of the widget,
 *       ...
 *     ),
 *     ...
 *   ),
 * )
 * @endcode
 *
 * Additionally, some processing data is placed in $form_state, and can be
 * accessed by field_form_get_state() and field_form_set_state().
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity for which to load form elements, used to initialize
 *   default form values.
 * @param $form
 *   The form structure to fill in. This can be a full form structure, or a
 *   sub-element of a larger form. The #parents property can be set to control
 *   the location of submitted field values within $form_state['values']. If
 *   not specified, $form['#parents'] is set to an empty array, placing field
 *   values at the top-level of $form_state['values'].
 * @param $form_state
 *   An associative array containing the current state of the form.
 * @param $langcode
 *   The language the field values are going to be entered, if no language
 *   is provided the default site language will be used.
 * @param array $options
 *   An associative array of additional options. See field_invoke_method() for
 *   details.
 *
 * @deprecated as of Drupal 8.0. Use the entity system instead.
 *
 * @see field_form_get_state()
 * @see field_form_set_state()
 */
function field_attach_form(EntityInterface $entity, &$form, &$form_state, $langcode = NULL, array $options = array()) {
  // Set #parents to 'top-level' by default.
  $form += array('#parents' => array());

  // Get the entity_form_display object for this form.
  $form_display = $form_state['form_display'];

  $form += (array) field_invoke_method('form', _field_invoke_widget_target($form_display), $entity, $form, $form_state, $options);

  $form['#entity_type'] = $entity->entityType();
  $form['#bundle'] = $entity->bundle();

  // Let other modules make changes to the form.
  // Avoid \Drupal::moduleHandler()->invokeAll()
  // to let parameters be taken by reference.
561
  foreach (\Drupal::moduleHandler()->getImplementations('field_attach_form') as $module) {
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
    $function = $module . '_field_attach_form';
    $function($entity, $form, $form_state, $langcode);
  }
}

/**
 * Performs field validation against form-submitted field values.
 *
 * There are two levels of validation for fields in forms: widget validation and
 * and field validation.
 * - Widget validation steps are specific to a given widget's own form structure
 *   and UI metaphors. They are executed through FAPI's #element_validate
 *   property during normal form validation.
 * - Field validation steps are common to a given field type, independently of
 *   the specific widget being used in a given form. They are defined in the
 *   field type's implementation of hook_field_validate().
 *
 * This function performs field validation in the context of a form submission.
 * It converts field validation errors into form errors on the correct form
 * elements. Fieldable entity types should call this function during their own
 * form validation function.
 *
584
 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
585
586
587
588
589
590
591
592
593
594
595
596
597
 *   The entity being submitted. The actual field values will be read
 *   from $form_state['values'].
 * @param $form
 *   The form structure where field elements are attached to. This might be a
 *   full form structure, or a sub-element of a larger form.
 * @param $form_state
 *   An associative array containing the current state of the form.
 * @param array $options
 *   An associative array of additional options. See field_invoke_method() for
 *   details.
 *
 * @deprecated as of Drupal 8.0. Use the entity system instead.
 */
598
function field_attach_form_validate(ContentEntityInterface $entity, $form, &$form_state, array $options = array()) {
599
600
601
  $has_violations = FALSE;
  foreach ($entity as $field_name => $field) {
    $definition = $field->getDefinition();
602
    if ($definition->isConfigurable() && (empty($options['field_name']) || $options['field_name'] == $field_name)) {
603
604
605
606
607
      $field_violations = $field->validate();
      if (count($field_violations)) {
        $has_violations = TRUE;

        // Place violations in $form_state.
608
        $field_state = field_form_get_state($form['#parents'], $field_name, $form_state);
609
        $field_state['constraint_violations'] = $field_violations;
610
        field_form_set_state($form['#parents'], $field_name, $form_state, $field_state);
611
612
613
614
615
616
      }
    }
  }

  if ($has_violations) {
    // Map errors back to form elements.
617
618
    $form_display = $form_state['form_display'];
    field_invoke_method('flagErrors', _field_invoke_widget_target($form_display), $entity, $form, $form_state, $options);
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
  }
}

/**
 * Populates an entity object with values from a form submission.
 *
 * Currently, this accounts for drag-and-drop reordering of field values, and
 * filtering of empty values.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity being submitted. The actual field values will be read
 *   from $form_state['values'].
 * @param $form
 *   The form structure where field elements are attached to. This might be a
 *   full form structure, or a sub-element of a larger form.
 * @param $form_state
 *   An associative array containing the current state of the form.
 * @param array $options
 *   An associative array of additional options. See field_invoke_method() for
 *   details.
 *
 * @deprecated as of Drupal 8.0. Use the entity system instead.
 */
function field_attach_extract_form_values(EntityInterface $entity, $form, &$form_state, array $options = array()) {
  // Extract field values from submitted values.
  $form_display = $form_state['form_display'];
  field_invoke_method('extractFormValues', _field_invoke_widget_target($form_display), $entity, $form, $form_state, $options);

  // Let other modules act on submitting the entity.
  // Avoid \Drupal::moduleHandler()->invokeAll()
  // to let $form_state be taken by reference.
650
  foreach (\Drupal::moduleHandler()->getImplementations('field_attach_extract_form_values') as $module) {
651
652
653
654
655
656
657
658
659
    $function = $module . 'field_attach_extract_form_values';
    $function($entity, $form, $form_state);
  }
}

/**
 * Prepares field data prior to display.
 *
 * This function lets field types and formatters load additional data needed for
660
661
662
 * display that is not automatically loaded during entity loading. It accepts an
 * array of entities to allow query optimization when displaying lists of
 * entities.
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
 *
 * field_attach_prepare_view() and field_attach_view() are two halves of the
 * same operation. It is safe to call field_attach_prepare_view() multiple times
 * on the same entity before calling field_attach_view() on it, but calling any
 * Field API operation on an entity between passing that entity to these two
 * functions may yield incorrect results.
 *
 * @param $entity_type
 *   The type of entities in $entities; e.g. 'node' or 'user'.
 * @param array $entities
 *   An array of entities, keyed by entity ID.
 * @param array $displays
 *   An array of entity display objects, keyed by bundle name.
 * @param $langcode
 *   (Optional) The language the field values are to be shown in. If no language
 *   is provided the current language is used.
 *
 * @deprecated as of Drupal 8.0. Use the entity system instead.
 */
function field_attach_prepare_view($entity_type, array $entities, array $displays, $langcode = NULL) {
  // To ensure hooks are only run once per entity, only process items without
  // the _field_view_prepared flag.
  // @todo: resolve this more generally for both entity and field level hooks.
  $prepare = array();
  foreach ($entities as $id => $entity) {
    if (empty($entity->_field_view_prepared)) {
      // Add this entity to the items to be prepared.
      $prepare[$id] = $entity;

      // Mark this item as prepared.
      $entity->_field_view_prepared = TRUE;
    }
  }

  // Then let the formatters do their own specific massaging. For each
  // instance, call the prepareView() method on the formatter object handed by
  // the entity display.
700
701
  $target_function = function (FieldDefinitionInterface $field_definition, $bundle) use ($displays) {
    if (isset($displays[$bundle])) {
702
      return $displays[$bundle]->getRenderer($field_definition->getName());
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
    }
  };
  $null = NULL;
  field_invoke_method_multiple('prepareView', $target_function, $prepare, $null, $null);
}

/**
 * Returns a renderable array for the fields on an entity.
 *
 * Each field is displayed according to the display options specified in the
 * $display parameter for the given view mode.
 *
 * field_attach_prepare_view() and field_attach_view() are two halves of the
 * same operation. It is safe to call field_attach_prepare_view() multiple times
 * on the same entity before calling field_attach_view() on it, but calling any
 * Field API operation on an entity between passing that entity to these two
 * functions may yield incorrect results.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity with fields to render.
723
 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
724
725
726
727
728
729
 *   The entity display object.
 * @param $langcode
 *   The language the field values are to be shown in. If no language is
 *   provided the current language is used.
 * @param array $options
 *   An associative array of additional options. See field_invoke_method() for
730
 *   details.
731
732
733
734
735
736
 *
 * @return array
 *   A renderable array for the field values.
 *
 * @deprecated as of Drupal 8.0. Use the entity system instead.
 */
737
function field_attach_view(EntityInterface $entity, EntityViewDisplayInterface $display, $langcode = NULL, array $options = array()) {
738
  // For each field, call the view() method on the formatter object handed
739
  // by the entity display.
740
  $target_function = function (FieldDefinitionInterface $field_definition) use ($display) {
741
    return $display->getRenderer($field_definition->getName());
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
  };
  $null = NULL;
  $output = field_invoke_method('view', $target_function, $entity, $null, $null, $options);

  // Let other modules alter the renderable array.
  $view_mode = $display->originalMode;
  $context = array(
    'entity' => $entity,
    'view_mode' => $view_mode,
    'display_options' => $view_mode,
    'langcode' => $langcode,
  );
  drupal_alter('field_attach_view', $output, $context);

  // Reset the _field_view_prepared flag set in field_attach_prepare_view(),
  // in case the same entity is displayed with different settings later in
  // the request.
  unset($entity->_field_view_prepared);

  return $output;
}

/**
 * Returns the field items in the language they currently would be displayed.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity containing the data to be displayed.
 * @param $field_name
 *   The field to be displayed.
 * @param $langcode
 *   (optional) The language code $entity->{$field_name} has to be displayed in.
 *   Defaults to the current language.
 *
 * @return
 *   An array with available field items keyed by delta.
 *
 * @deprecated as of Drupal 8.0. Use
 *   $entity->getTranslation($langcode)->{$field_name}
 */
function field_get_items(EntityInterface $entity, $field_name, $langcode = NULL) {
  $langcode = field_language($entity, $field_name, $langcode);
  return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
}
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801

/**
 * Helper function to get the default value for a field on an entity.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity for the operation.
 * @param $field
 *   The field structure.
 * @param $instance
 *   The instance structure.
 * @param $langcode
 *   The field language to fill-in with the default value.
 *
 * @return array
 *   The default value for the field.
 *
 * @deprecated as of Drupal 8.0. Use
802
 *   $instance->getDefaultValue($entity)
803
804
 */
function field_get_default_value(EntityInterface $entity, $field, $instance, $langcode = NULL) {
805
  return $instance->getDefaultValue($entity);
806
}
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835

/**
 * Determines whether the user has access to a given field.
 *
 * @param string $op
 *   The operation to be performed. Possible values:
 *   - edit
 *   - view
 * @param \Drupal\field\FieldInterface $field
 *   The field on which the operation is to be performed.
 * @param $entity_type
 *   The type of $entity; for example, 'node' or 'user'.
 * @param $entity
 *   (optional) The entity for the operation.
 * @param $account
 *   (optional) The account to check, if not given use currently logged in user.
 *
 * @return
 *   TRUE if the operation is allowed; FALSE if the operation is denied.
 *
 * @deprecated as of Drupal 8.0. Use
 *   Drupal\Core\Entity\EntityAccessControllerInterface::fieldAccess()
 */
function field_access($op, FieldInterface $field, $entity_type, $entity = NULL, $account = NULL) {
  $access_controller = \Drupal::entityManager()->getAccessController($entity_type);

  $items = $entity ? $entity->get($field->id()) : NULL;
  return $access_controller->fieldAccess($op, $field, $account, $items);
}
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900

/**
 * Ensures that a given language code is valid.
 *
 * Checks whether the given language code is one of the enabled language codes.
 * Otherwise, it returns the current, global language code; or the site's
 * default language code, if the additional parameter $default is TRUE.
 *
 * @param $langcode
 *   The language code to validate.
 * @param $default
 *   Whether to return the default language code or the current language code in
 *   case $langcode is invalid.
 *
 * @return
 *   A valid language code.
 *
 * @deprecated This has been deprecated in favor of the Entity Field API.
 */
function field_valid_language($langcode, $default = TRUE) {
  $languages = field_content_languages();
  if (in_array($langcode, $languages)) {
    return $langcode;
  }
  return $default ? language_default()->id : language(Language::TYPE_CONTENT)->id;
}

/**
 * Returns the display language code for the fields attached to the given
 * entity.
 *
 * The actual language code for each given field is determined based on the
 * requested language code and the actual data available in the fields
 * themselves.
 * If there is no registered translation handler for the given entity type, the
 * display language code to be used is just Language::LANGCODE_NOT_SPECIFIED, as
 * no other language code is allowed by field_available_languages().
 *
 * If translation handlers are found, we let modules provide alternative display
 * language codes for fields not having the requested language code available.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity to be displayed.
 * @param $field_name
 *   (optional) The name of the field to be displayed. Defaults to NULL. If
 *   no value is specified, the display language codes for every field attached
 *   to the given entity will be returned.
 * @param $langcode
 *   (optional) The language code $entity has to be displayed in. Defaults to
 *   NULL. If no value is given the current language will be used.
 *
 * @return
 *   A language code if a field name is specified, an array of language codes
 *   keyed by field name otherwise.
 *
 * @see \Drupal\Core\Language\LanguageManager::getFallbackCandidates()
 * @see \Drupal\Core\Entity\EntityInterface::getFieldLangcode()
 *
 * @deprecated This has been deprecated in favor of the Entity Field API.
 */
function field_language(EntityInterface $entity, $field_name = NULL, $langcode = NULL) {
  $langcode = \Drupal::entityManager()->getTranslationFromContext($entity, $langcode)->language()->id;
  $translatable = field_has_translation_handler($entity->entityType());
  if (!isset($field_name)) {
    $display_langcodes = array();
901
902
    foreach ($entity->getPropertyDefinitions() as $name => $definition) {
      $display_langcodes[$name] = $translatable ? $langcode : Language::LANGCODE_NOT_SPECIFIED;
903
904
905
    }
    return $display_langcodes;
  }
906
  else {
907
908
909
    return $translatable ? $langcode : Language::LANGCODE_NOT_SPECIFIED;
  }
}