public function FieldInfo::getBundleInstances

Retrieves the instances for a bundle.

The function also populates the corresponding field definitions in the "static" cache.

Parameters

$entity_type: The entity type.

$bundle: The bundle name.

Return value

The array of instance definitions, keyed by field name.

File

drupal/core/modules/field/lib/Drupal/field/FieldInfo.php, line 385
Contains \Drupal\field\FieldInfo.

Class

FieldInfo
Provides field and instance definitions for the current runtime environment.

Namespace

Drupal\field

Code

public function getBundleInstances($entity_type, $bundle) {

  // Read from the "static" cache.
  if (isset($this->bundleInstances[$entity_type][$bundle])) {
    return $this->bundleInstances[$entity_type][$bundle];
  }
  if (isset($this->emptyBundles[$entity_type][$bundle])) {
    return array();
  }

  // Read from the persistent cache.
  if ($cached = $this->cacheBackend
    ->get("field_info:bundle:{$entity_type}:{$bundle}")) {
    $info = $cached->data;

    // Extract the field definitions and save them in the "static" cache.
    foreach ($info['fields'] as $field) {
      if (!isset($this->fieldsById[$field['uuid']])) {
        $this->fieldsById[$field['uuid']] = $field;
        if (!$field['deleted']) {
          $this->fieldIdsByName[$field['field_name']] = $field['uuid'];
        }
      }
    }
    unset($info['fields']);

    // Store the instance definitions in the "static" cache'. Empty (or
    // non-existent) bundles are stored separately, so that they do not
    // pollute the global list returned by getInstances().
    if ($info['instances']) {
      $this->bundleInstances[$entity_type][$bundle] = $info['instances'];
    }
    else {
      $this->emptyBundles[$entity_type][$bundle] = TRUE;
    }
    return $info['instances'];
  }

  // Cache miss: collect from the definitions.
  $instances = array();

  // Do not return anything for unknown entity types.
  if (entity_get_info($entity_type)) {

    // Collect names of fields and instances involved in the bundle, using the
    // field map. The field map is already filtered to active, non-deleted
    // fields and instances, so those are kept out of the persistent caches.
    $config_ids = array();
    foreach ($this
      ->getFieldMap() as $field_name => $field_data) {
      if (isset($field_data['bundles'][$entity_type]) && in_array($bundle, $field_data['bundles'][$entity_type])) {
        $config_ids[$field_name] = "{$entity_type}.{$bundle}.{$field_name}";
      }
    }

    // Load and prepare the corresponding fields and instances entities.
    if ($config_ids) {
      $loaded_fields = entity_load_multiple('field_entity', array_keys($config_ids));
      $loaded_instances = entity_load_multiple('field_instance', array_values($config_ids));
      foreach ($loaded_instances as $instance) {
        $field = $loaded_fields[$instance['field_name']];
        $instance = $this
          ->prepareInstance($instance, $field['type']);
        $instances[$field['field_name']] = $instance;

        // If the field is not in our global "static" list yet, add it.
        if (!isset($this->fieldsById[$field['uuid']])) {
          $field = $this
            ->prepareField($field);
          $this->fieldsById[$field['uuid']] = $field;
          $this->fieldIdsByName[$field['field_name']] = $field['uuid'];
        }
      }
    }
  }

  // Store in the 'static' cache'. Empty (or non-existent) bundles are stored
  // separately, so that they do not pollute the global list returned by
  // getInstances().
  if ($instances) {
    $this->bundleInstances[$entity_type][$bundle] = $instances;
  }
  else {
    $this->emptyBundles[$entity_type][$bundle] = TRUE;
  }

  // The persistent cache additionally contains the definitions of the fields
  // involved in the bundle.
  $cache = array(
    'instances' => $instances,
    'fields' => array(),
  );
  foreach ($instances as $instance) {
    $cache['fields'][] = $this->fieldsById[$instance['field_id']];
  }
  $this->cacheBackend
    ->set("field_info:bundle:{$entity_type}:{$bundle}", $cache, CacheBackendInterface::CACHE_PERMANENT, array(
    'field_info' => TRUE,
  ));
  return $instances;
}