You are here

public function EntityFieldManager::getFieldDefinitions in Drupal 9

Same name and namespace in other branches
  1. 8 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

File

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

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;
    }
    else {

      // Rebuild the definitions and put it into the cache.
      $bundle_field_definitions = $this
        ->buildBundleFieldDefinitions($entity_type_id, $bundle, $base_field_definitions);
      $this
        ->cacheSet($cid, $bundle_field_definitions, Cache::PERMANENT, [
        'entity_types',
        'entity_field_info',
      ]);
    }

    // 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);
  }
  return $this->fieldDefinitions[$entity_type_id][$bundle][$langcode];
}