summaryrefslogtreecommitdiff
path: root/modules/field/field.info.inc
blob: 0d895fa88110b7df3d3b8e8077321d889c83e5cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
<?php
// $Id$

/**
 * @file
 * Field Info API, providing information about available fields and field types.
 */

/**
 * @defgroup field_info Field Info API
 * @{
 * Obtain information about Field API configuration.
 *
 * The Field Info API exposes information about field types, fields,
 * instances, bundles, widget types, display formatters, behaviors,
 * and settings defined by or with the Field API.
 */

/**
 * Clear the field info cache without clearing the field data cache.
 *
 * This is useful when deleted fields or instances are purged.  We
 * need to remove the purged records, but no actual field data items
 * are affected.
 */
function _field_info_cache_clear() {
  _field_info_collate_types(TRUE);
  drupal_static_reset('field_build_modes');
  _field_info_collate_fields(TRUE);
}

/**
 * Collate all information on field types, widget types and related structures.
 *
 * @param $reset
 *   If TRUE, clear the cache. The information will be rebuilt from the database
 *   next time it is needed. Defaults to FALSE.
 * @return
 *   If $reset is TRUE, nothing.
 *   If $reset is FALSE, an array containing the following elements:
 *
 * field types: array of hook_field_info() results, keyed by field_type.
 *   * label, description, settings, instance_settings, default_widget,
 *     default_formatter, behaviors: from hook_field_info()
 *   * module: the module that exposes the field type
 *
 * widget types: array of hook_field_widget_info() results, keyed by
 * widget_type.
 *   * label, field types, settings, behaviors: from hook_field_widget_info()
 *   * module: module that exposes the widget type
 *
 * formatter types: array of hook_field_formatter_info() results, keyed by
 * formatter_type.
 *   * label, field types, behaviors: from hook_field_formatter_info()
 *   * module: module that exposes the formatter type

 * fieldable types: array of hook_entity_info() results, keyed by entity_type.
 *   * name, id key, revision key, bundle key, cacheable, bundles: from
 *     hook_entity_info()
 *   * module: module that exposes the entity type
 * @TODO use entity_get_info().
 */
function _field_info_collate_types($reset = FALSE) {
  static $info;

  if ($reset) {
    $info = NULL;
    cache_clear_all('field_info_types', 'cache_field');
    return;
  }

  if (!isset($info)) {
    if ($cached = cache_get('field_info_types', 'cache_field')) {
      $info = $cached->data;
    }
    else {
      $info = array(
        'field types' => array(),
        'widget types' => array(),
        'formatter types' => array(),
        'fieldable types' => array(),
      );

      // Populate field types.
      foreach (module_implements('field_info') as $module) {
        $field_types = (array) module_invoke($module, 'field_info');
        foreach ($field_types as $name => $field_info) {
          // Provide defaults.
          $field_info += array(
            'settings' => array(),
            'instance_settings' => array(),
          );
          $info['field types'][$name] = $field_info;
          $info['field types'][$name]['module'] = $module;
        }
      }
      drupal_alter('field_info', $info['field types']);

      // Populate widget types.
      foreach (module_implements('field_widget_info') as $module) {
        $widget_types = (array) module_invoke($module, 'field_widget_info');
        foreach ($widget_types as $name => $widget_info) {
          // Provide defaults.
          $widget_info += array(
            'settings' => array(),
          );
          $info['widget types'][$name] = $widget_info;
          $info['widget types'][$name]['module'] = $module;
        }
      }
      drupal_alter('field_widget_info', $info['widget types']);

      // Populate formatters.
      foreach (module_implements('field_formatter_info') as $module) {
        $formatter_types = (array) module_invoke($module, 'field_formatter_info');
        foreach ($formatter_types as $name => $formatter_info) {
          // Provide defaults.
          $formatter_info += array(
            'settings' => array(),
          );
          $info['formatter types'][$name] = $formatter_info;
          $info['formatter types'][$name]['module'] = $module;
        }
      }
      drupal_alter('field_formatter_info', $info['formatter types']);

      // Populate information about 'fieldable' entities.
      foreach (module_implements('entity_info') as $module) {
        $entities = (array) module_invoke($module, 'entity_info');
        foreach ($entities as $name => $entity_info) {
          if (!empty($entity_info['fieldable'])) {
            // Provide defaults.
            $entity_info += array(
              'cacheable' => TRUE,
              'translation_handlers' => array(),
              'bundles' => array(),
            );
            $entity_info['object keys'] += array(
              'revision' => '',
              'bundle' => '',
            );
            // If no bundle key provided, then we assume a single bundle, named
            // after the type of the object. Make sure the bundle created
            // has the human-readable name we need for bundle messages.
            if (empty($entity_info['object keys']['bundle']) && empty($entity_info['bundles'])) {
              $entity_info['bundles'] = array($name => array('label' => $entity_info['label']));
            }
            $info['fieldable types'][$name] = $entity_info;
            $info['fieldable types'][$name]['module'] = $module;
          }
        }
      }
      drupal_alter('fieldable_info', $info['fieldable types']);

      cache_set('field_info_types', $info, 'cache_field');
    }
  }

  return $info;
}

/**
 * Collate all information on existing fields and instances.
 *
 * @param $reset
 *   If TRUE, clear the cache. The information will be rebuilt from the
 *   database next time it is needed. Defaults to FALSE.
 * @return
 *   If $reset is TRUE, nothing.
 *   If $reset is FALSE, an array containing the following elements:
 *   - fields: Array of existing fields, keyed by field name. This entry only
 *     lists non-deleted fields. Each field has an additional element,
 *     'bundles', which is an array of all non-deleted instances to which the
 *     field is assigned.
 *   - fields_id: Array of existing fields, keyed by field id. This entry lists
 *     both deleted and non-deleted fields. The bundles element is the same as
 *     for 'fields'.
 *   - instances: Array of existing instances, keyed by bundle name and field
 *     name. This entry only lists non-deleted instances.
 */
function _field_info_collate_fields($reset = FALSE) {
  static $info;

  if ($reset) {
    $info = NULL;
    cache_clear_all('field_info_fields', 'cache_field');
    return;
  }

  if (!isset($info)) {
    if ($cached = cache_get('field_info_fields', 'cache_field')) {
      $definitions = $cached->data;
    }
    else {
      $definitions = array(
        'field_ids' => field_read_fields(array(), array('include_deleted' => 1)),
        'instances' => field_read_instances(),
      );
      cache_set('field_info_fields', $definitions, 'cache_field');
    }

    // Populate 'field_ids' with all fields.
    $info['field_ids'] = array();
    foreach ($definitions['field_ids'] as $key => $field) {
      $info['field_ids'][$key] = $definitions['field_ids'][$key] = _field_info_prepare_field($field);
    }

    // Populate 'fields' only with non-deleted fields.
    $info['field'] = array();
    foreach ($info['field_ids'] as $field) {
      if (!$field['deleted']) {
        $info['fields'][$field['field_name']] = $field;
      }
    }

    // Populate 'instances'. Only non-deleted instances are considered.
    $info['instances'] = array();
    foreach (field_info_bundles() as $bundle => $bundle_info) {
      $info['instances'][$bundle] = array();
    }
    foreach ($definitions['instances'] as $instance) {
      $field = $info['fields'][$instance['field_name']];
      $instance = _field_info_prepare_instance($instance, $field);
      $info['instances'][$instance['bundle']][$instance['field_name']] = $instance;
      // Enrich field definitions with the list of bundles where they have
      // instances. NOTE: Deleted fields in $info['field_ids'] are not
      // enriched because all of their instances are deleted, too, and
      // are thus not in $definitions['instances'].
      $info['fields'][$instance['field_name']]['bundles'][] = $instance['bundle'];
      $info['field_ids'][$instance['field_id']]['bundles'][] = $instance['bundle'];
    }
  }

  return $info;
}

 /**
 * Prepare a field definition for the current run-time context.
 *
 * Since the field was last saved or updated, new field settings can be
 * expected.
 *
 * @param $field
 *   The raw field structure as read from the database.
 */
function _field_info_prepare_field($field) {
  // Make sure all expected field settings are present.
  $field['settings'] += field_info_field_settings($field['type']);

  return $field;
}

/**
 * Prepare an instance definition for the current run-time context.
 *
 * Since the instance was last saved or updated, a number of things might have
 * changed: widgets or formatters disabled, new settings expected, new build
 * modes added...
 *
 * @param $instance
 *   The raw instance structure as read from the database.
 * @param $field
 *   The field structure for the instance.
 */
function _field_info_prepare_instance($instance, $field) {
  $field_type = field_info_field_types($field['type']);

  // Make sure all expected instance settings are present.
  $instance['settings'] += field_info_instance_settings($field['type']);

  // Set a default value for the instance.
  if (field_behaviors_widget('default value', $instance) == FIELD_BEHAVIOR_DEFAULT && !isset($instance['default_value'])) {
    $instance['default_value'] = NULL;
  }

  // Fallback to default widget if widget type is not available.
  if (!field_info_widget_types($instance['widget']['type'])) {
    $instance['widget']['type'] = $field_type['default_widget'];
  }
  // Make sure all expected widget settings are present.
  $instance['widget']['settings'] += field_info_widget_settings($instance['widget']['type']);

  foreach ($instance['display'] as $build_mode => $display) {
    if ($display['type'] != 'hidden') {
      // Fallback to default formatter if formatter type is not available.
      if (!field_info_formatter_types($instance['display'][$build_mode]['type'])) {
        $instance['display'][$build_mode]['type'] = $field_type['default_formatter'];
      }
      // Make sure all expected formatter settings are present.
      $instance['display'][$build_mode]['settings'] += field_info_formatter_settings($instance['display'][$build_mode]['type']);
    }
  }

  // Fallback to 'full' display settings for unspecified build modes.
  $obj_type = field_info_bundle_entity($instance['bundle']);
  foreach (field_build_modes($obj_type) as $build_mode => $label) {
    if (!isset($instance['display'][$build_mode])) {
      $instance['display'][$build_mode] = $instance['display']['full'];
    }
  }

  return $instance;
}

/**
 *  Helper function for determining the behavior of a widget
 *  with respect to a given operation.
 *
 *  @param $op
 *    The name of the operation.
 *    Currently supported: 'default value', 'multiple values'.
 *  @param $instance
 *    The field instance array.
 *  @return
 *    FIELD_BEHAVIOR_NONE    - do nothing for this operation.
 *    FIELD_BEHAVIOR_CUSTOM  - use the widget's callback function.
 *    FIELD_BEHAVIOR_DEFAULT - use field.module default behavior.
 */
function field_behaviors_widget($op, $instance) {
  $info = field_info_widget_types($instance['widget']['type']);
  return isset($info['behaviors'][$op]) ? $info['behaviors'][$op] : FIELD_BEHAVIOR_DEFAULT;
}

/**
 *  Helper function for determining the behavior of a formatter
 *  with respect to a given operation.
 *
 *  @param $op
 *    The name of the operation.
 *    Currently supported: 'multiple values'
 *  @param $display
 *    The $instance['display'][$build_mode] array.
 *  @return
 *    FIELD_BEHAVIOR_NONE    - do nothing for this operation.
 *    FIELD_BEHAVIOR_CUSTOM  - use the formatter's callback function.
 *    FIELD_BEHAVIOR_DEFAULT - use field module default behavior.
 */
function field_behaviors_formatter($op, $display) {
  $info = field_info_formatter_types($display['type']);
  return isset($info['behaviors'][$op]) ? $info['behaviors'][$op] : FIELD_BEHAVIOR_DEFAULT;
}

/**
 * Return hook_field_info() data.
 *
 * @param $field_type
 *   (optional) A field type name. If ommitted, all field types will be
 *   returned.
 * @return
 *   Either a field type description, as provided by hook_field_info(), or an
 *   array of all existing field types, keyed by field type name.
 */
function field_info_field_types($field_type = NULL) {
  $info = _field_info_collate_types();
  $field_types = $info['field types'];
  if ($field_type) {
    if (isset($field_types[$field_type])) {
      return $field_types[$field_type];
    }
  }
  else {
    return $field_types;
  }
}

/**
 * Return hook_field_widget_info() data.
 *
 * @param $widget_type
 *   (optional) A widget type name. If ommitted, all widget types will be
 *   returned.
 * @return
 *   Either a widget type description, as provided by
 *   hook_field_widget_info(), or an array of all existing widget
 *   types, keyed by widget type name.
 */
function field_info_widget_types($widget_type = NULL) {
  $info = _field_info_collate_types();
  $widget_types = $info['widget types'];
  if ($widget_type) {
    if (isset($widget_types[$widget_type])) {
      return $widget_types[$widget_type];
    }
  }
  else {
    return $widget_types;
  }
}

/**
 * Return hook_field_formatter_info() data.
 *
 * @param $formatter_type
 *   (optional) A formatter type name. If ommitted, all formatter types will be
 *   returned.
 * @return
 *   Either a formatter type description, as provided by hook_field_formatter_info(),
 *   or an array of all existing widget types, keyed by widget type name.
 */
function field_info_formatter_types($formatter_type = NULL) {
  $info = _field_info_collate_types();
  $formatter_types = $info['formatter types'];
  if ($formatter_type) {
    if (isset($formatter_types[$formatter_type])) {
      return $formatter_types[$formatter_type];
    }
  }
  else {
    return $formatter_types;
  }
}

/**
 * Return hook_fieldable_info() data.
 *
 * @param $obj_type
 *   (optional) A fieldable type name. If ommitted, all fieldable types will be
 *   returned.
 * @return
 *   Either a fieldable type description, as provided by hook_fieldable_info(),
 *   or an array of all existing fieldable types, keyed by fieldable type name.
 */
function field_info_fieldable_types($obj_type = NULL) {
  $info = _field_info_collate_types();
  $fieldable_types = $info['fieldable types'];
  if ($obj_type) {
    if (isset($fieldable_types[$obj_type])) {
      return $fieldable_types[$obj_type];
    }
  }
  else {
    return $fieldable_types;
  }
}

/**
 * Return an array of fieldable bundle names and labels, for an individual
 * object type or for all object types.
 */
function field_info_bundles($obj_type = NULL) {
  $info = _field_info_collate_types();
  $bundles = array();
  foreach ($info['fieldable types'] as $type => $fieldable_info) {
    if (empty($obj_type) || $obj_type == $type) {
      $bundles += $fieldable_info['bundles'];
    }
  }
  return $bundles;
}

/**
 * Identify the type of entity that created a bundle.
 * // TODO : might not be needed depending on how we solve
 * // the 'namespace bundle names' issue
 */
function field_info_bundle_entity($bundle) {
  $info = _field_info_collate_types();
  foreach ($info['fieldable types'] as $type => $fieldable_info) {
    if (isset($fieldable_info['bundles'][$bundle])) {
      return $type;
    }
  }
  return FALSE;
}

/**
 * Return array of all field data, keyed by field name.
 *
 * @return
 *   An array of Field objects. Each Field object has an additional
 *   property, bundles, which is an array of all the bundles to which
 *   this field belongs.
 */
function field_info_fields() {
  $info = _field_info_collate_fields();
  return $info['fields'];
}

/**
 * Return data about an individual field.
 *
 * @param $field_name
 *   The name of the field to retrieve. $field_name can only refer to a
 *   non-deleted field.
 * @return
 *   The named field object, or NULL. The Field object has an additional
 *   property, bundles, which is an array of all the bundles to which
 *   this field belongs.
 */
function field_info_field($field_name) {
  $info = _field_info_collate_fields();
  if (isset($info['fields'][$field_name])) {
    return $info['fields'][$field_name];
  }
}

/**
 * Return data about an individual field by its id.
 *
 * @param $field_id
 *   The id of the field to retrieve. $field_id can refer to a
 *   deleted field.
 * @return
 *   The named field object, or NULL. The Field object has an additional
 *   property, bundles, which is an array of all the bundles to which
 *   this field belongs.
 */
function field_info_field_by_id($field_id) {
  $info = _field_info_collate_fields();
  if (isset($info['field_ids'][$field_id])) {
    return $info['field_ids'][$field_id];
  }
}

/**
 * Return an array of instance data for a given bundle,
 * or for all known bundles, keyed by bundle name and field name.
 *
 * @param $bundle_name
 *   If set, return information on just this bundle.
 */
function field_info_instances($bundle_name = NULL) {
  $info = _field_info_collate_fields();
  if (!isset($bundle_name)) {
    return $info['instances'];
  }
  if (isset($info['instances'][$bundle_name])) {
    return $info['instances'][$bundle_name];
  }
  return array();
}

/**
 * Return an array of instance data for a specific field and bundle.
 */
function field_info_instance($field_name, $bundle_name) {
  $info = _field_info_collate_fields();
  if (isset($info['instances'][$bundle_name][$field_name])) {
    return $info['instances'][$bundle_name][$field_name];
  }
}

/**
 * Return a field type's default settings.
 *
 * @param $type
 *   A field type name.
 * @return
 *   The field type's default settings, as provided by hook_field_info(), or an
 *   empty array.
 */
function field_info_field_settings($type) {
  $info = field_info_field_types($type);
  return isset($info['settings']) ? $info['settings'] : array();
}

/**
 * Return a field type's default instance settings.
 *
 * @param $type
 *   A field type name.
 * @return
 *   The field type's default instance settings, as provided by
 *   hook_field_info(), or an empty array.
 */
function field_info_instance_settings($type) {
  $info = field_info_field_types($type);
  return isset($info['instance_settings']) ? $info['instance_settings'] : array();
}

/**
 * Return a field widget's default settings.
 *
 * @param $type
 *   A widget type name.
 * @return
 *   The field type's default settings, as provided by hook_field_info(), or an
 *   empty array.
 */
function field_info_widget_settings($type) {
  $info = field_info_widget_types($type);
  return isset($info['settings']) ? $info['settings'] : array();
}

/**
 * Return a field formatter's default settings.
 *
 * @param $type
 *   A field formatter type name.
 * @return
 *   The field formatter's default settings, as provided by
 *   hook_field_info(), or an empty array.
 */
function field_info_formatter_settings($type) {
  $info = field_info_formatter_types($type);
  return isset($info['settings']) ? $info['settings'] : array();
}

/**
 * @} End of "defgroup field_info"
 */