function EntityFieldManager::getFieldMap

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

Gets a lightweight map of fields across bundles.

Return value

array An array keyed by entity type. Each value is an array which keys are field names and value is an array with two entries:

  • type: The field type.
  • bundles: An associative array of the bundles in which the field appears, where the keys and values are both the bundle's machine name.

Overrides EntityFieldManagerInterface::getFieldMap

2 calls to EntityFieldManager::getFieldMap()
EntityFieldManager::getFieldMapByFieldType in core/lib/Drupal/Core/Entity/EntityFieldManager.php
Gets a lightweight map of fields across bundles filtered by field type.
EntityFieldManager::preWarm in core/lib/Drupal/Core/Entity/EntityFieldManager.php
Build any cache item or items that this service relies on.

File

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

Class

EntityFieldManager
Manages the discovery of entity fields.

Namespace

Drupal\Core\Entity

Code

public function getFieldMap() {
  if (!$this->fieldMap) {
    // Not prepared, try to load from cache.
    $cid = 'entity_field_map';
    if ($cache = $this->cacheGet($cid)) {
      $this->fieldMap = $cache->data;
    }
    else {
      // The field map is built in two steps. First, add all base fields, by
      // looping over all fieldable entity types. They always exist for all
      // bundles, and we do not expect to have so many different entity
      // types for this to become a bottleneck.
      foreach ($this->entityTypeManager
        ->getDefinitions() as $entity_type_id => $entity_type) {
        if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
          $bundles = array_keys($this->entityTypeBundleInfo
            ->getBundleInfo($entity_type_id));
          foreach ($this->getBaseFieldDefinitions($entity_type_id) as $field_name => $base_field_definition) {
            $this->fieldMap[$entity_type_id][$field_name] = [
              'type' => $base_field_definition->getType(),
              'bundles' => array_combine($bundles, $bundles),
            ];
          }
          foreach ($bundles as $bundle) {
            $fields = $this->getFieldDefinitions($entity_type_id, $bundle);
            foreach ($fields as $field_name => $field_definition) {
              $this->fieldMap[$entity_type_id][$field_name]['type'] = $field_definition->getType();
              $this->fieldMap[$entity_type_id][$field_name]['bundles'][$bundle] = $bundle;
            }
          }
        }
      }
      $this->cacheSet($cid, $this->fieldMap, Cache::PERMANENT, [
        'entity_types',
        'entity_field_info',
      ]);
    }
  }
  return $this->fieldMap;
}

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