function EntityFieldManager::getFieldMap

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityFieldManager.php \Drupal\Core\Entity\EntityFieldManager::getFieldMap()
  2. 8.9.x core/lib/Drupal/Core/Entity/EntityFieldManager.php \Drupal\Core\Entity\EntityFieldManager::getFieldMap()
  3. 11.x core/lib/Drupal/Core/Entity/EntityFieldManager.php \Drupal\Core\Entity\EntityFieldManager::getFieldMap()
1 call to EntityFieldManager::getFieldMap()
EntityFieldManager::getFieldMapByFieldType in core/lib/Drupal/Core/Entity/EntityFieldManager.php

File

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

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),
                        ];
                    }
                }
            }
            // In the second step, the per-bundle fields are added, based on the
            // persistent bundle field map stored in a key value collection. This
            // data is managed in the
            // FieldDefinitionListener::onFieldDefinitionCreate() and
            // FieldDefinitionListener::onFieldDefinitionDelete() methods.
            // Rebuilding this information in the same way as base fields would not
            // scale, as the time to query would grow exponentially with more fields
            // and bundles. A cache would be deleted during cache clears, which is
            // the only time it is needed, so a key value collection is used.
            $bundle_field_maps = $this->keyValueFactory
                ->get('entity.definitions.bundle_field_map')
                ->getAll();
            foreach ($bundle_field_maps as $entity_type_id => $bundle_field_map) {
                foreach ($bundle_field_map as $field_name => $map_entry) {
                    if (!isset($this->fieldMap[$entity_type_id][$field_name])) {
                        $this->fieldMap[$entity_type_id][$field_name] = $map_entry;
                    }
                    else {
                        $this->fieldMap[$entity_type_id][$field_name]['bundles'] += $map_entry['bundles'];
                    }
                }
            }
            $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.