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 343

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 = cache('field')
    ->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['id']])) {
        $this->fieldsById[$field['id']] = $field;
        if (!$field['deleted']) {
          $this->fieldIdsByName[$field['field_name']] = $field['id'];
        }
      }
    }
    unset($info['fields']);

    // Save in the "static" cache.
    $this->bundleInstances[$entity_type][$bundle] = $info['instances'];
    return $info['instances'];
  }

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

  // Collect the fields in the bundle.
  $params = array(
    'entity_type' => $entity_type,
    'bundle' => $bundle,
  );
  $fields = field_read_fields($params);

  // This iterates on non-deleted instances, so deleted fields are kept out of
  // the persistent caches.
  foreach (field_read_instances($params) as $instance) {
    $field = $fields[$instance['field_name']];
    $instance = $this
      ->prepareInstance($instance, $field['type']);
    $instances[$field['field_name']] = new FieldInstance($instance);

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

  // 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']];
  }
  cache('field')
    ->set("field_info:bundle:{$entity_type}:{$bundle}", $cache, CacheBackendInterface::CACHE_PERMANENT, array(
    'field_info' => TRUE,
  ));
  return $instances;
}