public function EntityFieldManager::getFieldMap in Drupal 9
Same name and namespace in other branches
- 8 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
1 call 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.
File
- core/
lib/ Drupal/ Core/ Entity/ EntityFieldManager.php, line 498
Class
- EntityFieldManager
- Manages the discovery of entity fields.
Namespace
Drupal\Core\EntityCode
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;
}