function EntityFieldManager::getFieldDefinitions

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Entity/EntityFieldManager.php \Drupal\Core\Entity\EntityFieldManager::getFieldDefinitions()
  2. 10 core/lib/Drupal/Core/Entity/EntityFieldManager.php \Drupal\Core\Entity\EntityFieldManager::getFieldDefinitions()
  3. 9 core/lib/Drupal/Core/Entity/EntityFieldManager.php \Drupal\Core\Entity\EntityFieldManager::getFieldDefinitions()
  4. 8.9.x core/lib/Drupal/Core/Entity/EntityFieldManager.php \Drupal\Core\Entity\EntityFieldManager::getFieldDefinitions()

Gets the field definitions for a specific bundle.

Parameters

string $entity_type_id: The entity type ID. Only entity types that implement \Drupal\Core\Entity\FieldableEntityInterface are supported.

string $bundle: The bundle.

Return value

\Drupal\Core\Field\FieldDefinitionInterface[] The array of field definitions for the bundle, keyed by field name.

Overrides EntityFieldManagerInterface::getFieldDefinitions

1 call to EntityFieldManager::getFieldDefinitions()
EntityFieldManager::getFieldLabels in core/lib/Drupal/Core/Entity/EntityFieldManager.php
Returns the labels used for a field on an entity type.

File

core/lib/Drupal/Core/Entity/EntityFieldManager.php, line 354

Class

EntityFieldManager
Manages the discovery of entity fields.

Namespace

Drupal\Core\Entity

Code

public function getFieldDefinitions($entity_type_id, $bundle) {
  $langcode = $this->languageManager
    ->getCurrentLanguage()
    ->getId();
  if (!isset($this->fieldDefinitions[$entity_type_id][$bundle][$langcode])) {
    $base_field_definitions = $this->getBaseFieldDefinitions($entity_type_id);
    // Not prepared, try to load from cache.
    $cid = 'entity_bundle_field_definitions:' . $entity_type_id . ':' . $bundle . ':' . $langcode;
    if ($cache = $this->cacheGet($cid)) {
      $bundle_field_definitions = $cache->data;
      // Field definitions consist of the bundle specific overrides and the
      // base fields, merge them together. Use array_replace() to replace base
      // fields with by bundle overrides and keep them in order, append
      // additional by bundle fields.
      $this->fieldDefinitions[$entity_type_id][$bundle][$langcode] = array_replace($base_field_definitions, $bundle_field_definitions);
    }
    else {
      // In some cases, entity types can have dozens or hundreds of bundles,
      // so attempt to rebuild all of the bundle data in one go. We assume
      // that if field data is being requested for one bundle, that it will
      // also be requested for other bundles of the same entity later in the
      // request. For example when views field data is built.
      $bundles = $this->entityTypeBundleInfo
        ->getBundleInfo($entity_type_id);
      unset($bundles[$bundle]);
      $cache_data = [];
      // Always generate field data for the bundle that is passed in, even if
      // the bundle does not exist.
      // @todo remove support for non-existing bundles.
      // @see https://www.drupal.org/project/drupal/issues/3565232
      $cid = 'entity_bundle_field_definitions:' . $entity_type_id . ':' . $bundle . ':' . $langcode;
      $bundle_field_definitions = $this->buildBundleFieldDefinitions($entity_type_id, $bundle, $base_field_definitions);
      $cache_data[$cid] = [
        'data' => $bundle_field_definitions,
        'tags' => [
          'entity_types',
          'entity_field_info',
        ],
      ];
      $this->fieldDefinitions[$entity_type_id][$bundle][$langcode] = array_replace($base_field_definitions, $bundle_field_definitions);
      foreach ($bundles as $bundle_name => $bundle_info) {
        $cid = 'entity_bundle_field_definitions:' . $entity_type_id . ':' . $bundle_name . ':' . $langcode;
        // Rebuild the definitions and put it into the cache.
        $bundle_field_definitions = $this->buildBundleFieldDefinitions($entity_type_id, $bundle_name, $base_field_definitions);
        $cache_data[$cid] = [
          'data' => $bundle_field_definitions,
          'tags' => [
            'entity_types',
            'entity_field_info',
          ],
        ];
        $this->fieldDefinitions[$entity_type_id][$bundle_name][$langcode] = array_replace($base_field_definitions, $bundle_field_definitions);
      }
      if ($cache_data && $this->useCaches) {
        $this->cacheBackend
          ->setMultiple($cache_data);
      }
    }
  }
  return $this->fieldDefinitions[$entity_type_id][$bundle][$langcode];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.