public function FieldInfo::getInstances

Retrieves all active, non-deleted instances definitions.

Parameters

$entity_type: (optional) The entity type.

Return value

If $entity_type is not set, all instances keyed by entity type and bundle name. If $entity_type is set, all instances for that entity type, keyed by bundle name.

File

drupal/core/modules/field/lib/Drupal/field/FieldInfo.php, line 214

Class

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

Namespace

Drupal\field

Code

public function getInstances($entity_type = NULL) {

  // If the full list is not present in "static" cache yet.
  if (!$this->loadedAllInstances) {

    // Read from persistent cache.
    if ($cached = cache('field')
      ->get('field_info:instances')) {
      $this->bundleInstances = $cached->data;
    }
    else {

      // Collect and prepare instances.
      // We also need to populate the static field cache, since it will not
      // be set by subsequent getBundleInstances() calls.
      $this
        ->getFields();
      foreach (field_read_instances() as $instance) {
        $field = $this
          ->getField($instance['field_name']);
        $instance = $this
          ->prepareInstance($instance, $field['type']);
        $this->bundleInstances[$instance['entity_type']][$instance['bundle']][$instance['field_name']] = new FieldInstance($instance);
      }

      // Store in persistent cache.
      cache('field')
        ->set('field_info:instances', $this->bundleInstances, CacheBackendInterface::CACHE_PERMANENT, array(
        'field_info' => TRUE,
      ));
    }
    $this->loadedAllInstances = TRUE;
  }
  if (isset($entity_type)) {
    return isset($this->bundleInstances[$entity_type]) ? $this->bundleInstances[$entity_type] : array();
  }
  else {
    return $this->bundleInstances;
  }
}