1.20.x field.info.inc _field_info_collate_fields()

Collates all information on existing fields and instances.

Return value

An associative array containing::

  • fields: Array of existing fields, keyed by field name. This element lists deleted and non-deleted fields, but not inactive ones. Each field has an additional element, 'bundles', which is an array of all non-deleted instances of that field.
  • instances: Array of existing instances, keyed by entity type, bundle name and field name. This element only lists non-deleted instances whose field is active.

See also

_field_info_collate_fields_reset()

Related topics

File

modules/field/field.info.inc, line 190
Field Info API, providing information about available fields and field types.

Code

function _field_info_collate_fields() {
  // Use the advanced backdrop_static() pattern, since this is called very often.
  static $backdrop_static_fast;

  if (!isset($backdrop_static_fast)) {
    $backdrop_static_fast['field_info_collate_fields'] = &backdrop_static(__FUNCTION__);
  }
  $info = &$backdrop_static_fast['field_info_collate_fields'];

  if (!isset($info)) {
    if ($cached = cache('field')->get('field_info_fields')) {
      $info = $cached->data;
    }
    else {
      $definitions = array(
        'fields' => _field_read_fields_cache(),
        'instances' => field_read_instances(),
      );

      // Populate 'fields' with all fields, keyed by field name.
      $info['fields'] = array();
      foreach ($definitions['fields'] as $field_name => $field) {
        $info['fields'][$field_name] = $definitions['fields'][$field_name] = _field_info_prepare_field($field);
      }

      // Populate 'instances'. Only non-deleted instances are considered.
      $info['instances'] = array();
      foreach (field_info_bundles() as $entity_type => $bundles) {
        foreach ($bundles as $bundle => $bundle_info) {
          $info['instances'][$entity_type][$bundle] = array();
        }
      }
      foreach ($definitions['instances'] as $instance) {
        $field = $info['fields'][$instance['field_name']];
        $instance = _field_info_prepare_instance($instance, $field);
        $info['instances'][$instance['entity_type']][$instance['bundle']][$instance['field_name']] = $instance;
        // Enrich field definitions with the list of bundles where they have
        // instances. NOTE: Deleted fields in $info['fields'] 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['entity_type']][] = $instance['bundle'];
      }

      // Populate 'extra_fields'.
      $extra = module_invoke_all('field_extra_fields');
      backdrop_alter('field_extra_fields', $extra);
      // Merge in saved settings.
      foreach ($extra as $entity_type => $bundles) {
        foreach ($bundles as $bundle => $extra_fields) {
          $extra_fields = _field_info_prepare_extra_fields($extra_fields, $entity_type, $bundle);
          $info['extra_fields'][$entity_type][$bundle] = $extra_fields;
        }
      }

      cache('field')->set('field_info_fields', $info);
    }
  }

  return $info;
}