View source
<?php
namespace Drupal\Core\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\UseCacheBackendTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TypedData\TypedDataManagerInterface;
class EntityFieldManager implements EntityFieldManagerInterface {
use UseCacheBackendTrait;
use StringTranslationTrait;
protected $extraFields = [];
protected $baseFieldDefinitions;
protected $fieldDefinitions;
protected $fieldStorageDefinitions;
protected $activeFieldStorageDefinitions;
protected $fieldMap = [];
protected $fieldMapByFieldType = [];
protected $typedDataManager;
protected $languageManager;
protected $keyValueFactory;
protected $moduleHandler;
protected $entityTypeManager;
protected $entityTypeBundleInfo;
protected $entityDisplayRepository;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityDisplayRepositoryInterface $entity_display_repository, TypedDataManagerInterface $typed_data_manager, LanguageManagerInterface $language_manager, KeyValueFactoryInterface $key_value_factory, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityDisplayRepository = $entity_display_repository;
$this->typedDataManager = $typed_data_manager;
$this->languageManager = $language_manager;
$this->keyValueFactory = $key_value_factory;
$this->moduleHandler = $module_handler;
$this->cacheBackend = $cache_backend;
}
public function getBaseFieldDefinitions($entity_type_id) {
if (!isset($this->baseFieldDefinitions[$entity_type_id])) {
$cid = 'entity_base_field_definitions:' . $entity_type_id . ':' . $this->languageManager
->getCurrentLanguage()
->getId();
if ($cache = $this
->cacheGet($cid)) {
$this->baseFieldDefinitions[$entity_type_id] = $cache->data;
}
else {
$this->baseFieldDefinitions[$entity_type_id] = $this
->buildBaseFieldDefinitions($entity_type_id);
$this
->cacheSet($cid, $this->baseFieldDefinitions[$entity_type_id], Cache::PERMANENT, [
'entity_types',
'entity_field_info',
]);
}
}
return $this->baseFieldDefinitions[$entity_type_id];
}
protected function buildBaseFieldDefinitions($entity_type_id) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$class = $entity_type
->getClass();
$keys = array_filter($entity_type
->getKeys());
if (!$entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
throw new \LogicException("Getting the base fields is not supported for entity type {$entity_type->getLabel()}.");
}
$base_field_definitions = $class::baseFieldDefinitions($entity_type);
if ($entity_type
->isTranslatable()) {
if (isset($keys['langcode']) && isset($base_field_definitions[$keys['langcode']])) {
$base_field_definitions[$keys['langcode']]
->setTranslatable(TRUE);
}
if (!isset($base_field_definitions[$keys['default_langcode']])) {
$base_field_definitions[$keys['default_langcode']] = BaseFieldDefinition::create('boolean')
->setLabel($this
->t('Default translation'))
->setDescription($this
->t('A flag indicating whether this is the default translation.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDefaultValue(TRUE);
}
}
if ($entity_type
->isRevisionable()) {
$field_name = $entity_type
->getRevisionMetadataKeys(FALSE)['revision_default'];
$base_field_definitions[$field_name] = BaseFieldDefinition::create('boolean')
->setLabel($this
->t('Default revision'))
->setDescription($this
->t('A flag indicating whether this was a default revision when it was saved.'))
->setStorageRequired(TRUE)
->setInternal(TRUE)
->setTranslatable(FALSE)
->setRevisionable(TRUE);
}
if ($entity_type
->isRevisionable() && $entity_type
->isTranslatable()) {
$base_field_definitions[$keys['revision_translation_affected']] = BaseFieldDefinition::create('boolean')
->setLabel($this
->t('Revision translation affected'))
->setDescription($this
->t('Indicates if the last edit of a translation belongs to current revision.'))
->setReadOnly(TRUE)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
}
$provider = $entity_type
->getProvider();
foreach ($base_field_definitions as $definition) {
if ($definition instanceof BaseFieldDefinition) {
$definition
->setProvider($provider);
}
}
foreach ($this->moduleHandler
->getImplementations('entity_base_field_info') as $module) {
$module_definitions = $this->moduleHandler
->invoke($module, 'entity_base_field_info', [
$entity_type,
]);
if (!empty($module_definitions)) {
foreach ($module_definitions as $field_name => $definition) {
if ($definition instanceof BaseFieldDefinition && $definition
->getProvider() == NULL) {
$definition
->setProvider($module);
}
$base_field_definitions[$field_name] = $definition;
}
}
}
foreach ($base_field_definitions as $field_name => $base_field_definition) {
if ($base_field_definition instanceof BaseFieldDefinition) {
$base_field_definition
->setName($field_name);
$base_field_definition
->setTargetEntityTypeId($entity_type_id);
$base_field_definition
->setTargetBundle(NULL);
}
}
$this->moduleHandler
->alter('entity_base_field_info', $base_field_definitions, $entity_type);
foreach (array_intersect_key($keys, array_flip([
'id',
'revision',
'uuid',
'bundle',
])) as $key => $field_name) {
if (!isset($base_field_definitions[$field_name])) {
throw new \LogicException("The {$field_name} field definition does not exist and it is used as {$key} entity key.");
}
if ($base_field_definitions[$field_name]
->isRevisionable()) {
throw new \LogicException("The {$base_field_definitions[$field_name]->getLabel()} field cannot be revisionable as it is used as {$key} entity key.");
}
if ($base_field_definitions[$field_name]
->isTranslatable()) {
throw new \LogicException("The {$base_field_definitions[$field_name]->getLabel()} field cannot be translatable as it is used as {$key} entity key.");
}
}
if ($entity_type
->isTranslatable() && (!isset($keys['langcode']) || !isset($base_field_definitions[$keys['langcode']]) || !$base_field_definitions[$keys['langcode']]
->isTranslatable())) {
throw new \LogicException("The {$entity_type->getLabel()} entity type cannot be translatable as it does not define a translatable \"langcode\" field.");
}
return $base_field_definitions;
}
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);
$cid = 'entity_bundle_field_definitions:' . $entity_type_id . ':' . $bundle . ':' . $langcode;
if ($cache = $this
->cacheGet($cid)) {
$bundle_field_definitions = $cache->data;
}
else {
$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',
]);
}
$this->fieldDefinitions[$entity_type_id][$bundle][$langcode] = array_replace($base_field_definitions, $bundle_field_definitions);
}
return $this->fieldDefinitions[$entity_type_id][$bundle][$langcode];
}
protected function buildBundleFieldDefinitions($entity_type_id, $bundle, array $base_field_definitions) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$class = $entity_type
->getClass();
$bundle_field_definitions = $class::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions);
$base_field_override_ids = array_map(function ($field_name) use ($entity_type_id, $bundle) {
return $entity_type_id . '.' . $bundle . '.' . $field_name;
}, array_keys($base_field_definitions));
$base_field_overrides = $this->entityTypeManager
->getStorage('base_field_override')
->loadMultiple($base_field_override_ids);
foreach ($base_field_overrides as $base_field_override) {
$field_name = $base_field_override
->getName();
$bundle_field_definitions[$field_name] = $base_field_override;
}
$provider = $entity_type
->getProvider();
foreach ($bundle_field_definitions as $definition) {
if ($definition instanceof BaseFieldDefinition) {
$definition
->setProvider($provider);
}
}
foreach ($this->moduleHandler
->getImplementations('entity_bundle_field_info') as $module) {
$module_definitions = $this->moduleHandler
->invoke($module, 'entity_bundle_field_info', [
$entity_type,
$bundle,
$base_field_definitions,
]);
if (!empty($module_definitions)) {
foreach ($module_definitions as $field_name => $definition) {
if ($definition instanceof BaseFieldDefinition) {
$definition
->setProvider($module);
}
$bundle_field_definitions[$field_name] = $definition;
}
}
}
foreach ($bundle_field_definitions as $field_name => $field_definition) {
if ($field_definition instanceof BaseFieldDefinition) {
$field_definition
->setName($field_name);
$field_definition
->setTargetEntityTypeId($entity_type_id);
}
if ($field_definition instanceof BaseFieldDefinition || $field_definition instanceof FieldDefinition) {
$field_definition
->setTargetBundle($bundle);
}
}
$this->moduleHandler
->alter('entity_bundle_field_info', $bundle_field_definitions, $entity_type, $bundle);
return $bundle_field_definitions;
}
public function getFieldStorageDefinitions($entity_type_id) {
if (!isset($this->fieldStorageDefinitions[$entity_type_id])) {
$this->fieldStorageDefinitions[$entity_type_id] = [];
foreach ($this
->getBaseFieldDefinitions($entity_type_id) as $field_name => $definition) {
if (!$definition
->isComputed()) {
$this->fieldStorageDefinitions[$entity_type_id][$field_name] = $definition;
}
}
$cid = 'entity_field_storage_definitions:' . $entity_type_id . ':' . $this->languageManager
->getCurrentLanguage()
->getId();
if ($cache = $this
->cacheGet($cid)) {
$field_storage_definitions = $cache->data;
}
else {
$field_storage_definitions = $this
->buildFieldStorageDefinitions($entity_type_id);
$this
->cacheSet($cid, $field_storage_definitions, Cache::PERMANENT, [
'entity_types',
'entity_field_info',
]);
}
$this->fieldStorageDefinitions[$entity_type_id] += $field_storage_definitions;
}
return $this->fieldStorageDefinitions[$entity_type_id];
}
public function getActiveFieldStorageDefinitions($entity_type_id) {
if (!isset($this->activeFieldStorageDefinitions[$entity_type_id])) {
$this->activeFieldStorageDefinitions[$entity_type_id] = $this->keyValueFactory
->get('entity.definitions.installed')
->get($entity_type_id . '.field_storage_definitions', []);
}
return $this->activeFieldStorageDefinitions[$entity_type_id] ?: $this
->getFieldStorageDefinitions($entity_type_id);
}
public function setFieldMap(array $field_map) {
$this->fieldMap = $field_map;
return $this;
}
public function getFieldMap() {
if (!$this->fieldMap) {
$cid = 'entity_field_map';
if ($cache = $this
->cacheGet($cid)) {
$this->fieldMap = $cache->data;
}
else {
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),
];
}
}
}
$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;
}
public function getFieldMapByFieldType($field_type) {
if (!isset($this->fieldMapByFieldType[$field_type])) {
$filtered_map = [];
$map = $this
->getFieldMap();
foreach ($map as $entity_type => $fields) {
foreach ($fields as $field_name => $field_info) {
if ($field_info['type'] == $field_type) {
$filtered_map[$entity_type][$field_name] = $field_info;
}
}
}
$this->fieldMapByFieldType[$field_type] = $filtered_map;
}
return $this->fieldMapByFieldType[$field_type];
}
protected function buildFieldStorageDefinitions($entity_type_id) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$field_definitions = [];
foreach ($this->moduleHandler
->getImplementations('entity_field_storage_info') as $module) {
$module_definitions = $this->moduleHandler
->invoke($module, 'entity_field_storage_info', [
$entity_type,
]);
if (!empty($module_definitions)) {
foreach ($module_definitions as $field_name => $definition) {
if ($definition instanceof BaseFieldDefinition) {
$definition
->setProvider($module);
}
$field_definitions[$field_name] = $definition;
}
}
}
$this->moduleHandler
->alter('entity_field_storage_info', $field_definitions, $entity_type);
return $field_definitions;
}
public function clearCachedFieldDefinitions() {
$this->baseFieldDefinitions = [];
$this->fieldDefinitions = [];
$this->fieldStorageDefinitions = [];
$this->activeFieldStorageDefinitions = [];
$this->fieldMap = [];
$this->fieldMapByFieldType = [];
$this->entityDisplayRepository
->clearDisplayModeInfo();
$this->extraFields = [];
Cache::invalidateTags([
'entity_field_info',
]);
$this->typedDataManager
->clearCachedDefinitions();
}
public function useCaches($use_caches = FALSE) {
$this->useCaches = $use_caches;
if (!$use_caches) {
$this->fieldDefinitions = [];
$this->baseFieldDefinitions = [];
$this->fieldStorageDefinitions = [];
$this->activeFieldStorageDefinitions = [];
}
}
public function getExtraFields($entity_type_id, $bundle) {
if (isset($this->extraFields[$entity_type_id][$bundle])) {
return $this->extraFields[$entity_type_id][$bundle];
}
$cache_id = 'entity_bundle_extra_fields:' . $entity_type_id . ':' . $bundle . ':' . $this->languageManager
->getCurrentLanguage()
->getId();
$cached = $this
->cacheGet($cache_id);
if ($cached) {
$this->extraFields[$entity_type_id][$bundle] = $cached->data;
return $this->extraFields[$entity_type_id][$bundle];
}
$extra = $this->moduleHandler
->invokeAll('entity_extra_field_info');
$this->moduleHandler
->alter('entity_extra_field_info', $extra);
$info = isset($extra[$entity_type_id][$bundle]) ? $extra[$entity_type_id][$bundle] : [];
$info += [
'form' => [],
'display' => [],
];
$this->extraFields[$entity_type_id][$bundle] = $info;
$this
->cacheSet($cache_id, $info, Cache::PERMANENT, [
'entity_field_info',
]);
return $this->extraFields[$entity_type_id][$bundle];
}
}