annotate core/lib/Drupal/Core/Entity/EntityFieldManager.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Entity;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\Cache;
Chris@0 6 use Drupal\Core\Cache\CacheBackendInterface;
Chris@0 7 use Drupal\Core\Cache\UseCacheBackendTrait;
Chris@0 8 use Drupal\Core\Extension\ModuleHandlerInterface;
Chris@0 9 use Drupal\Core\Field\BaseFieldDefinition;
Chris@0 10 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
Chris@0 11 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 12 use Drupal\Core\StringTranslation\StringTranslationTrait;
Chris@0 13 use Drupal\Core\TypedData\TypedDataManagerInterface;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Manages the discovery of entity fields.
Chris@0 17 *
Chris@0 18 * This includes field definitions, base field definitions, and field storage
Chris@0 19 * definitions.
Chris@0 20 */
Chris@0 21 class EntityFieldManager implements EntityFieldManagerInterface {
Chris@0 22
Chris@0 23 use UseCacheBackendTrait;
Chris@0 24 use StringTranslationTrait;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Extra fields by bundle.
Chris@0 28 *
Chris@0 29 * @var array
Chris@0 30 */
Chris@0 31 protected $extraFields = [];
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Static cache of base field definitions.
Chris@0 35 *
Chris@0 36 * @var array
Chris@0 37 */
Chris@0 38 protected $baseFieldDefinitions;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Static cache of field definitions per bundle and entity type.
Chris@0 42 *
Chris@0 43 * @var array
Chris@0 44 */
Chris@0 45 protected $fieldDefinitions;
Chris@0 46
Chris@0 47 /**
Chris@0 48 * Static cache of field storage definitions per entity type.
Chris@0 49 *
Chris@0 50 * Elements of the array:
Chris@0 51 * - $entity_type_id: \Drupal\Core\Field\BaseFieldDefinition[]
Chris@0 52 *
Chris@0 53 * @var array
Chris@0 54 */
Chris@0 55 protected $fieldStorageDefinitions;
Chris@0 56
Chris@0 57 /**
Chris@0 58 * An array keyed by entity type. Each value is an array whose keys are
Chris@0 59 * field names and whose value is an array with two entries:
Chris@0 60 * - type: The field type.
Chris@0 61 * - bundles: The bundles in which the field appears.
Chris@0 62 *
Chris@12 63 * @var array
Chris@0 64 */
Chris@0 65 protected $fieldMap = [];
Chris@0 66
Chris@0 67 /**
Chris@0 68 * An array keyed by field type. Each value is an array whose key are entity
Chris@0 69 * types including arrays in the same form that $fieldMap.
Chris@0 70 *
Chris@0 71 * It helps access the mapping between types and fields by the field type.
Chris@0 72 *
Chris@0 73 * @var array
Chris@0 74 */
Chris@0 75 protected $fieldMapByFieldType = [];
Chris@0 76
Chris@0 77 /**
Chris@0 78 * The typed data manager.
Chris@0 79 *
Chris@0 80 * @var \Drupal\Core\TypedData\TypedDataManagerInterface
Chris@0 81 */
Chris@0 82 protected $typedDataManager;
Chris@0 83
Chris@0 84 /**
Chris@0 85 * The language manager.
Chris@0 86 *
Chris@0 87 * @var \Drupal\Core\Language\LanguageManagerInterface
Chris@0 88 */
Chris@0 89 protected $languageManager;
Chris@0 90
Chris@0 91 /**
Chris@0 92 * The key-value factory.
Chris@0 93 *
Chris@0 94 * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
Chris@0 95 */
Chris@0 96 protected $keyValueFactory;
Chris@0 97
Chris@0 98 /**
Chris@0 99 * The module handler.
Chris@0 100 *
Chris@0 101 * @var \Drupal\Core\Extension\ModuleHandlerInterface
Chris@0 102 */
Chris@0 103 protected $moduleHandler;
Chris@0 104
Chris@0 105 /**
Chris@0 106 * The entity type manager.
Chris@0 107 *
Chris@0 108 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 109 */
Chris@0 110 protected $entityTypeManager;
Chris@0 111
Chris@0 112 /**
Chris@0 113 * The entity type bundle info.
Chris@0 114 *
Chris@0 115 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
Chris@0 116 */
Chris@0 117 protected $entityTypeBundleInfo;
Chris@0 118
Chris@0 119 /**
Chris@0 120 * The entity display repository.
Chris@0 121 *
Chris@0 122 * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
Chris@0 123 */
Chris@0 124 protected $entityDisplayRepository;
Chris@0 125
Chris@0 126 /**
Chris@0 127 * Constructs a new EntityFieldManager.
Chris@0 128 *
Chris@0 129 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@0 130 * The entity type manager.
Chris@0 131 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
Chris@0 132 * The entity type bundle info.
Chris@0 133 * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
Chris@0 134 * The entity display repository.
Chris@0 135 * @param \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager
Chris@0 136 * The typed data manager.
Chris@0 137 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@0 138 * The language manager.
Chris@0 139 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
Chris@0 140 * The key-value factory.
Chris@0 141 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
Chris@0 142 * The module handler.
Chris@0 143 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
Chris@0 144 * The cache backend.
Chris@0 145 */
Chris@0 146 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) {
Chris@0 147 $this->entityTypeManager = $entity_type_manager;
Chris@0 148 $this->entityTypeBundleInfo = $entity_type_bundle_info;
Chris@0 149 $this->entityDisplayRepository = $entity_display_repository;
Chris@0 150
Chris@0 151 $this->typedDataManager = $typed_data_manager;
Chris@0 152 $this->languageManager = $language_manager;
Chris@0 153 $this->keyValueFactory = $key_value_factory;
Chris@0 154 $this->moduleHandler = $module_handler;
Chris@0 155 $this->cacheBackend = $cache_backend;
Chris@0 156 }
Chris@0 157
Chris@0 158 /**
Chris@0 159 * {@inheritdoc}
Chris@0 160 */
Chris@0 161 public function getBaseFieldDefinitions($entity_type_id) {
Chris@0 162 // Check the static cache.
Chris@0 163 if (!isset($this->baseFieldDefinitions[$entity_type_id])) {
Chris@0 164 // Not prepared, try to load from cache.
Chris@0 165 $cid = 'entity_base_field_definitions:' . $entity_type_id . ':' . $this->languageManager->getCurrentLanguage()->getId();
Chris@0 166 if ($cache = $this->cacheGet($cid)) {
Chris@0 167 $this->baseFieldDefinitions[$entity_type_id] = $cache->data;
Chris@0 168 }
Chris@0 169 else {
Chris@0 170 // Rebuild the definitions and put it into the cache.
Chris@0 171 $this->baseFieldDefinitions[$entity_type_id] = $this->buildBaseFieldDefinitions($entity_type_id);
Chris@0 172 $this->cacheSet($cid, $this->baseFieldDefinitions[$entity_type_id], Cache::PERMANENT, ['entity_types', 'entity_field_info']);
Chris@0 173 }
Chris@0 174 }
Chris@0 175 return $this->baseFieldDefinitions[$entity_type_id];
Chris@0 176 }
Chris@0 177
Chris@0 178 /**
Chris@0 179 * Builds base field definitions for an entity type.
Chris@0 180 *
Chris@0 181 * @param string $entity_type_id
Chris@0 182 * The entity type ID. Only entity types that implement
Chris@0 183 * \Drupal\Core\Entity\FieldableEntityInterface are supported.
Chris@0 184 *
Chris@0 185 * @return \Drupal\Core\Field\FieldDefinitionInterface[]
Chris@0 186 * An array of field definitions, keyed by field name.
Chris@0 187 *
Chris@0 188 * @throws \LogicException
Chris@0 189 * Thrown if a config entity type is given or if one of the entity keys is
Chris@0 190 * flagged as translatable.
Chris@0 191 */
Chris@0 192 protected function buildBaseFieldDefinitions($entity_type_id) {
Chris@14 193 /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */
Chris@0 194 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
Chris@0 195 $class = $entity_type->getClass();
Chris@14 196 /** @var string[] $keys */
Chris@0 197 $keys = array_filter($entity_type->getKeys());
Chris@0 198
Chris@0 199 // Fail with an exception for non-fieldable entity types.
Chris@0 200 if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
Chris@0 201 throw new \LogicException("Getting the base fields is not supported for entity type {$entity_type->getLabel()}.");
Chris@0 202 }
Chris@0 203
Chris@0 204 // Retrieve base field definitions.
Chris@0 205 /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $base_field_definitions */
Chris@0 206 $base_field_definitions = $class::baseFieldDefinitions($entity_type);
Chris@0 207
Chris@0 208 // Make sure translatable entity types are correctly defined.
Chris@0 209 if ($entity_type->isTranslatable()) {
Chris@0 210 // The langcode field should always be translatable if the entity type is.
Chris@0 211 if (isset($keys['langcode']) && isset($base_field_definitions[$keys['langcode']])) {
Chris@0 212 $base_field_definitions[$keys['langcode']]->setTranslatable(TRUE);
Chris@0 213 }
Chris@0 214 // A default_langcode field should always be defined.
Chris@0 215 if (!isset($base_field_definitions[$keys['default_langcode']])) {
Chris@0 216 $base_field_definitions[$keys['default_langcode']] = BaseFieldDefinition::create('boolean')
Chris@0 217 ->setLabel($this->t('Default translation'))
Chris@0 218 ->setDescription($this->t('A flag indicating whether this is the default translation.'))
Chris@0 219 ->setTranslatable(TRUE)
Chris@0 220 ->setRevisionable(TRUE)
Chris@0 221 ->setDefaultValue(TRUE);
Chris@0 222 }
Chris@0 223 }
Chris@0 224
Chris@0 225 // Make sure that revisionable entity types are correctly defined.
Chris@14 226 if ($entity_type->isRevisionable()) {
Chris@14 227 // Disable the BC layer to prevent a recursion, this only needs the
Chris@14 228 // revision_default key that is always set.
Chris@14 229 $field_name = $entity_type->getRevisionMetadataKeys(FALSE)['revision_default'];
Chris@14 230 $base_field_definitions[$field_name] = BaseFieldDefinition::create('boolean')
Chris@14 231 ->setLabel($this->t('Default revision'))
Chris@14 232 ->setDescription($this->t('A flag indicating whether this was a default revision when it was saved.'))
Chris@14 233 ->setStorageRequired(TRUE)
Chris@14 234 ->setInternal(TRUE)
Chris@14 235 ->setTranslatable(FALSE)
Chris@14 236 ->setRevisionable(TRUE);
Chris@14 237 }
Chris@14 238
Chris@14 239 // Make sure that revisionable and translatable entity types are correctly
Chris@14 240 // defined.
Chris@0 241 if ($entity_type->isRevisionable() && $entity_type->isTranslatable()) {
Chris@0 242 // The 'revision_translation_affected' field should always be defined.
Chris@0 243 // This field has been added unconditionally in Drupal 8.4.0 and it is
Chris@0 244 // overriding any pre-existing definition on purpose so that any
Chris@0 245 // differences are immediately available in the status report.
Chris@0 246 $base_field_definitions[$keys['revision_translation_affected']] = BaseFieldDefinition::create('boolean')
Chris@0 247 ->setLabel($this->t('Revision translation affected'))
Chris@0 248 ->setDescription($this->t('Indicates if the last edit of a translation belongs to current revision.'))
Chris@0 249 ->setReadOnly(TRUE)
Chris@0 250 ->setRevisionable(TRUE)
Chris@0 251 ->setTranslatable(TRUE);
Chris@0 252 }
Chris@0 253
Chris@0 254 // Assign base field definitions the entity type provider.
Chris@0 255 $provider = $entity_type->getProvider();
Chris@0 256 foreach ($base_field_definitions as $definition) {
Chris@0 257 // @todo Remove this check once FieldDefinitionInterface exposes a proper
Chris@0 258 // provider setter. See https://www.drupal.org/node/2225961.
Chris@0 259 if ($definition instanceof BaseFieldDefinition) {
Chris@0 260 $definition->setProvider($provider);
Chris@0 261 }
Chris@0 262 }
Chris@0 263
Chris@0 264 // Retrieve base field definitions from modules.
Chris@0 265 foreach ($this->moduleHandler->getImplementations('entity_base_field_info') as $module) {
Chris@0 266 $module_definitions = $this->moduleHandler->invoke($module, 'entity_base_field_info', [$entity_type]);
Chris@0 267 if (!empty($module_definitions)) {
Chris@0 268 // Ensure the provider key actually matches the name of the provider
Chris@0 269 // defining the field.
Chris@0 270 foreach ($module_definitions as $field_name => $definition) {
Chris@0 271 // @todo Remove this check once FieldDefinitionInterface exposes a
Chris@0 272 // proper provider setter. See https://www.drupal.org/node/2225961.
Chris@0 273 if ($definition instanceof BaseFieldDefinition && $definition->getProvider() == NULL) {
Chris@0 274 $definition->setProvider($module);
Chris@0 275 }
Chris@0 276 $base_field_definitions[$field_name] = $definition;
Chris@0 277 }
Chris@0 278 }
Chris@0 279 }
Chris@0 280
Chris@0 281 // Automatically set the field name, target entity type and bundle
Chris@0 282 // for non-configurable fields.
Chris@0 283 foreach ($base_field_definitions as $field_name => $base_field_definition) {
Chris@0 284 if ($base_field_definition instanceof BaseFieldDefinition) {
Chris@0 285 $base_field_definition->setName($field_name);
Chris@0 286 $base_field_definition->setTargetEntityTypeId($entity_type_id);
Chris@0 287 $base_field_definition->setTargetBundle(NULL);
Chris@0 288 }
Chris@0 289 }
Chris@0 290
Chris@0 291 // Invoke alter hook.
Chris@0 292 $this->moduleHandler->alter('entity_base_field_info', $base_field_definitions, $entity_type);
Chris@0 293
Chris@0 294 // Ensure defined entity keys are there and have proper revisionable and
Chris@0 295 // translatable values.
Chris@0 296 foreach (array_intersect_key($keys, array_flip(['id', 'revision', 'uuid', 'bundle'])) as $key => $field_name) {
Chris@0 297 if (!isset($base_field_definitions[$field_name])) {
Chris@0 298 throw new \LogicException("The $field_name field definition does not exist and it is used as $key entity key.");
Chris@0 299 }
Chris@0 300 if ($base_field_definitions[$field_name]->isRevisionable()) {
Chris@0 301 throw new \LogicException("The {$base_field_definitions[$field_name]->getLabel()} field cannot be revisionable as it is used as $key entity key.");
Chris@0 302 }
Chris@0 303 if ($base_field_definitions[$field_name]->isTranslatable()) {
Chris@0 304 throw new \LogicException("The {$base_field_definitions[$field_name]->getLabel()} field cannot be translatable as it is used as $key entity key.");
Chris@0 305 }
Chris@0 306 }
Chris@0 307
Chris@0 308 // Make sure translatable entity types define the "langcode" field properly.
Chris@0 309 if ($entity_type->isTranslatable() && (!isset($keys['langcode']) || !isset($base_field_definitions[$keys['langcode']]) || !$base_field_definitions[$keys['langcode']]->isTranslatable())) {
Chris@0 310 throw new \LogicException("The {$entity_type->getLabel()} entity type cannot be translatable as it does not define a translatable \"langcode\" field.");
Chris@0 311 }
Chris@0 312
Chris@0 313 return $base_field_definitions;
Chris@0 314 }
Chris@0 315
Chris@0 316 /**
Chris@0 317 * {@inheritdoc}
Chris@0 318 */
Chris@0 319 public function getFieldDefinitions($entity_type_id, $bundle) {
Chris@0 320 if (!isset($this->fieldDefinitions[$entity_type_id][$bundle])) {
Chris@0 321 $base_field_definitions = $this->getBaseFieldDefinitions($entity_type_id);
Chris@0 322 // Not prepared, try to load from cache.
Chris@0 323 $cid = 'entity_bundle_field_definitions:' . $entity_type_id . ':' . $bundle . ':' . $this->languageManager->getCurrentLanguage()->getId();
Chris@0 324 if ($cache = $this->cacheGet($cid)) {
Chris@0 325 $bundle_field_definitions = $cache->data;
Chris@0 326 }
Chris@0 327 else {
Chris@0 328 // Rebuild the definitions and put it into the cache.
Chris@0 329 $bundle_field_definitions = $this->buildBundleFieldDefinitions($entity_type_id, $bundle, $base_field_definitions);
Chris@0 330 $this->cacheSet($cid, $bundle_field_definitions, Cache::PERMANENT, ['entity_types', 'entity_field_info']);
Chris@0 331 }
Chris@0 332 // Field definitions consist of the bundle specific overrides and the
Chris@0 333 // base fields, merge them together. Use array_replace() to replace base
Chris@0 334 // fields with by bundle overrides and keep them in order, append
Chris@0 335 // additional by bundle fields.
Chris@0 336 $this->fieldDefinitions[$entity_type_id][$bundle] = array_replace($base_field_definitions, $bundle_field_definitions);
Chris@0 337 }
Chris@0 338 return $this->fieldDefinitions[$entity_type_id][$bundle];
Chris@0 339 }
Chris@0 340
Chris@0 341 /**
Chris@0 342 * Builds field definitions for a specific bundle within an entity type.
Chris@0 343 *
Chris@0 344 * @param string $entity_type_id
Chris@0 345 * The entity type ID. Only entity types that implement
Chris@0 346 * \Drupal\Core\Entity\FieldableEntityInterface are supported.
Chris@0 347 * @param string $bundle
Chris@0 348 * The bundle.
Chris@0 349 * @param \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions
Chris@0 350 * The list of base field definitions.
Chris@0 351 *
Chris@0 352 * @return \Drupal\Core\Field\FieldDefinitionInterface[]
Chris@0 353 * An array of bundle field definitions, keyed by field name. Does
Chris@0 354 * not include base fields.
Chris@0 355 */
Chris@0 356 protected function buildBundleFieldDefinitions($entity_type_id, $bundle, array $base_field_definitions) {
Chris@0 357 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
Chris@0 358 $class = $entity_type->getClass();
Chris@0 359
Chris@0 360 // Allow the entity class to provide bundle fields and bundle-specific
Chris@0 361 // overrides of base fields.
Chris@0 362 $bundle_field_definitions = $class::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions);
Chris@0 363
Chris@0 364 // Load base field overrides from configuration. These take precedence over
Chris@0 365 // base field overrides returned above.
Chris@0 366 $base_field_override_ids = array_map(function ($field_name) use ($entity_type_id, $bundle) {
Chris@0 367 return $entity_type_id . '.' . $bundle . '.' . $field_name;
Chris@0 368 }, array_keys($base_field_definitions));
Chris@0 369 $base_field_overrides = $this->entityTypeManager->getStorage('base_field_override')->loadMultiple($base_field_override_ids);
Chris@0 370 foreach ($base_field_overrides as $base_field_override) {
Chris@0 371 /** @var \Drupal\Core\Field\Entity\BaseFieldOverride $base_field_override */
Chris@0 372 $field_name = $base_field_override->getName();
Chris@0 373 $bundle_field_definitions[$field_name] = $base_field_override;
Chris@0 374 }
Chris@0 375
Chris@0 376 $provider = $entity_type->getProvider();
Chris@0 377 foreach ($bundle_field_definitions as $definition) {
Chris@0 378 // @todo Remove this check once FieldDefinitionInterface exposes a proper
Chris@0 379 // provider setter. See https://www.drupal.org/node/2225961.
Chris@0 380 if ($definition instanceof BaseFieldDefinition) {
Chris@0 381 $definition->setProvider($provider);
Chris@0 382 }
Chris@0 383 }
Chris@0 384
Chris@0 385 // Retrieve base field definitions from modules.
Chris@0 386 foreach ($this->moduleHandler->getImplementations('entity_bundle_field_info') as $module) {
Chris@0 387 $module_definitions = $this->moduleHandler->invoke($module, 'entity_bundle_field_info', [$entity_type, $bundle, $base_field_definitions]);
Chris@0 388 if (!empty($module_definitions)) {
Chris@0 389 // Ensure the provider key actually matches the name of the provider
Chris@0 390 // defining the field.
Chris@0 391 foreach ($module_definitions as $field_name => $definition) {
Chris@0 392 // @todo Remove this check once FieldDefinitionInterface exposes a
Chris@0 393 // proper provider setter. See https://www.drupal.org/node/2225961.
Chris@0 394 if ($definition instanceof BaseFieldDefinition) {
Chris@0 395 $definition->setProvider($module);
Chris@0 396 }
Chris@0 397 $bundle_field_definitions[$field_name] = $definition;
Chris@0 398 }
Chris@0 399 }
Chris@0 400 }
Chris@0 401
Chris@0 402 // Automatically set the field name, target entity type and bundle
Chris@0 403 // for non-configurable fields.
Chris@0 404 foreach ($bundle_field_definitions as $field_name => $field_definition) {
Chris@0 405 if ($field_definition instanceof BaseFieldDefinition) {
Chris@0 406 $field_definition->setName($field_name);
Chris@0 407 $field_definition->setTargetEntityTypeId($entity_type_id);
Chris@0 408 $field_definition->setTargetBundle($bundle);
Chris@0 409 }
Chris@0 410 }
Chris@0 411
Chris@0 412 // Invoke 'per bundle' alter hook.
Chris@0 413 $this->moduleHandler->alter('entity_bundle_field_info', $bundle_field_definitions, $entity_type, $bundle);
Chris@0 414
Chris@0 415 return $bundle_field_definitions;
Chris@0 416 }
Chris@0 417
Chris@0 418 /**
Chris@0 419 * {@inheritdoc}
Chris@0 420 */
Chris@0 421 public function getFieldStorageDefinitions($entity_type_id) {
Chris@0 422 if (!isset($this->fieldStorageDefinitions[$entity_type_id])) {
Chris@0 423 $this->fieldStorageDefinitions[$entity_type_id] = [];
Chris@0 424 // Add all non-computed base fields.
Chris@0 425 foreach ($this->getBaseFieldDefinitions($entity_type_id) as $field_name => $definition) {
Chris@0 426 if (!$definition->isComputed()) {
Chris@0 427 $this->fieldStorageDefinitions[$entity_type_id][$field_name] = $definition;
Chris@0 428 }
Chris@0 429 }
Chris@0 430 // Not prepared, try to load from cache.
Chris@0 431 $cid = 'entity_field_storage_definitions:' . $entity_type_id . ':' . $this->languageManager->getCurrentLanguage()->getId();
Chris@0 432 if ($cache = $this->cacheGet($cid)) {
Chris@0 433 $field_storage_definitions = $cache->data;
Chris@0 434 }
Chris@0 435 else {
Chris@0 436 // Rebuild the definitions and put it into the cache.
Chris@0 437 $field_storage_definitions = $this->buildFieldStorageDefinitions($entity_type_id);
Chris@0 438 $this->cacheSet($cid, $field_storage_definitions, Cache::PERMANENT, ['entity_types', 'entity_field_info']);
Chris@0 439 }
Chris@0 440 $this->fieldStorageDefinitions[$entity_type_id] += $field_storage_definitions;
Chris@0 441 }
Chris@0 442 return $this->fieldStorageDefinitions[$entity_type_id];
Chris@0 443 }
Chris@0 444
Chris@0 445 /**
Chris@0 446 * {@inheritdoc}
Chris@0 447 */
Chris@0 448 public function setFieldMap(array $field_map) {
Chris@0 449 $this->fieldMap = $field_map;
Chris@0 450 return $this;
Chris@0 451 }
Chris@0 452
Chris@0 453 /**
Chris@0 454 * {@inheritdoc}
Chris@0 455 */
Chris@0 456 public function getFieldMap() {
Chris@0 457 if (!$this->fieldMap) {
Chris@0 458 // Not prepared, try to load from cache.
Chris@0 459 $cid = 'entity_field_map';
Chris@0 460 if ($cache = $this->cacheGet($cid)) {
Chris@0 461 $this->fieldMap = $cache->data;
Chris@0 462 }
Chris@0 463 else {
Chris@0 464 // The field map is built in two steps. First, add all base fields, by
Chris@0 465 // looping over all fieldable entity types. They always exist for all
Chris@0 466 // bundles, and we do not expect to have so many different entity
Chris@0 467 // types for this to become a bottleneck.
Chris@0 468 foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
Chris@0 469 if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
Chris@0 470 $bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id));
Chris@0 471 foreach ($this->getBaseFieldDefinitions($entity_type_id) as $field_name => $base_field_definition) {
Chris@0 472 $this->fieldMap[$entity_type_id][$field_name] = [
Chris@0 473 'type' => $base_field_definition->getType(),
Chris@0 474 'bundles' => array_combine($bundles, $bundles),
Chris@0 475 ];
Chris@0 476 }
Chris@0 477 }
Chris@0 478 }
Chris@0 479
Chris@0 480 // In the second step, the per-bundle fields are added, based on the
Chris@0 481 // persistent bundle field map stored in a key value collection. This
Chris@0 482 // data is managed in the EntityManager::onFieldDefinitionCreate()
Chris@0 483 // and EntityManager::onFieldDefinitionDelete() methods. Rebuilding this
Chris@0 484 // information in the same way as base fields would not scale, as the
Chris@0 485 // time to query would grow exponentially with more fields and bundles.
Chris@0 486 // A cache would be deleted during cache clears, which is the only time
Chris@0 487 // it is needed, so a key value collection is used.
Chris@0 488 $bundle_field_maps = $this->keyValueFactory->get('entity.definitions.bundle_field_map')->getAll();
Chris@0 489 foreach ($bundle_field_maps as $entity_type_id => $bundle_field_map) {
Chris@0 490 foreach ($bundle_field_map as $field_name => $map_entry) {
Chris@0 491 if (!isset($this->fieldMap[$entity_type_id][$field_name])) {
Chris@0 492 $this->fieldMap[$entity_type_id][$field_name] = $map_entry;
Chris@0 493 }
Chris@0 494 else {
Chris@0 495 $this->fieldMap[$entity_type_id][$field_name]['bundles'] += $map_entry['bundles'];
Chris@0 496 }
Chris@0 497 }
Chris@0 498 }
Chris@0 499
Chris@17 500 $this->cacheSet($cid, $this->fieldMap, Cache::PERMANENT, ['entity_types', 'entity_field_info']);
Chris@0 501 }
Chris@0 502 }
Chris@0 503 return $this->fieldMap;
Chris@0 504 }
Chris@0 505
Chris@0 506 /**
Chris@0 507 * {@inheritdoc}
Chris@0 508 */
Chris@0 509 public function getFieldMapByFieldType($field_type) {
Chris@0 510 if (!isset($this->fieldMapByFieldType[$field_type])) {
Chris@0 511 $filtered_map = [];
Chris@0 512 $map = $this->getFieldMap();
Chris@0 513 foreach ($map as $entity_type => $fields) {
Chris@0 514 foreach ($fields as $field_name => $field_info) {
Chris@0 515 if ($field_info['type'] == $field_type) {
Chris@0 516 $filtered_map[$entity_type][$field_name] = $field_info;
Chris@0 517 }
Chris@0 518 }
Chris@0 519 }
Chris@0 520 $this->fieldMapByFieldType[$field_type] = $filtered_map;
Chris@0 521 }
Chris@0 522 return $this->fieldMapByFieldType[$field_type];
Chris@0 523 }
Chris@0 524
Chris@0 525 /**
Chris@0 526 * Builds field storage definitions for an entity type.
Chris@0 527 *
Chris@0 528 * @param string $entity_type_id
Chris@0 529 * The entity type ID. Only entity types that implement
Chris@0 530 * \Drupal\Core\Entity\FieldableEntityInterface are supported
Chris@0 531 *
Chris@0 532 * @return \Drupal\Core\Field\FieldStorageDefinitionInterface[]
Chris@0 533 * An array of field storage definitions, keyed by field name.
Chris@0 534 */
Chris@0 535 protected function buildFieldStorageDefinitions($entity_type_id) {
Chris@0 536 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
Chris@0 537 $field_definitions = [];
Chris@0 538
Chris@0 539 // Retrieve base field definitions from modules.
Chris@0 540 foreach ($this->moduleHandler->getImplementations('entity_field_storage_info') as $module) {
Chris@0 541 $module_definitions = $this->moduleHandler->invoke($module, 'entity_field_storage_info', [$entity_type]);
Chris@0 542 if (!empty($module_definitions)) {
Chris@0 543 // Ensure the provider key actually matches the name of the provider
Chris@0 544 // defining the field.
Chris@0 545 foreach ($module_definitions as $field_name => $definition) {
Chris@0 546 // @todo Remove this check once FieldDefinitionInterface exposes a
Chris@0 547 // proper provider setter. See https://www.drupal.org/node/2225961.
Chris@0 548 if ($definition instanceof BaseFieldDefinition) {
Chris@0 549 $definition->setProvider($module);
Chris@0 550 }
Chris@0 551 $field_definitions[$field_name] = $definition;
Chris@0 552 }
Chris@0 553 }
Chris@0 554 }
Chris@0 555
Chris@0 556 // Invoke alter hook.
Chris@0 557 $this->moduleHandler->alter('entity_field_storage_info', $field_definitions, $entity_type);
Chris@0 558
Chris@0 559 return $field_definitions;
Chris@0 560 }
Chris@0 561
Chris@0 562 /**
Chris@0 563 * {@inheritdoc}
Chris@0 564 */
Chris@0 565 public function clearCachedFieldDefinitions() {
Chris@0 566 $this->baseFieldDefinitions = [];
Chris@0 567 $this->fieldDefinitions = [];
Chris@0 568 $this->fieldStorageDefinitions = [];
Chris@0 569 $this->fieldMap = [];
Chris@0 570 $this->fieldMapByFieldType = [];
Chris@0 571 $this->entityDisplayRepository->clearDisplayModeInfo();
Chris@0 572 $this->extraFields = [];
Chris@0 573 Cache::invalidateTags(['entity_field_info']);
Chris@0 574 // The typed data manager statically caches prototype objects with injected
Chris@0 575 // definitions, clear those as well.
Chris@0 576 $this->typedDataManager->clearCachedDefinitions();
Chris@0 577 }
Chris@0 578
Chris@0 579 /**
Chris@0 580 * {@inheritdoc}
Chris@0 581 */
Chris@0 582 public function useCaches($use_caches = FALSE) {
Chris@0 583 $this->useCaches = $use_caches;
Chris@0 584 if (!$use_caches) {
Chris@0 585 $this->fieldDefinitions = [];
Chris@0 586 $this->baseFieldDefinitions = [];
Chris@0 587 $this->fieldStorageDefinitions = [];
Chris@0 588 }
Chris@0 589 }
Chris@0 590
Chris@0 591 /**
Chris@0 592 * {@inheritdoc}
Chris@0 593 */
Chris@0 594 public function getExtraFields($entity_type_id, $bundle) {
Chris@0 595 // Read from the "static" cache.
Chris@0 596 if (isset($this->extraFields[$entity_type_id][$bundle])) {
Chris@0 597 return $this->extraFields[$entity_type_id][$bundle];
Chris@0 598 }
Chris@0 599
Chris@0 600 // Read from the persistent cache. Since hook_entity_extra_field_info() and
Chris@0 601 // hook_entity_extra_field_info_alter() might contain t() calls, we cache
Chris@0 602 // per language.
Chris@0 603 $cache_id = 'entity_bundle_extra_fields:' . $entity_type_id . ':' . $bundle . ':' . $this->languageManager->getCurrentLanguage()->getId();
Chris@0 604 $cached = $this->cacheGet($cache_id);
Chris@0 605 if ($cached) {
Chris@0 606 $this->extraFields[$entity_type_id][$bundle] = $cached->data;
Chris@0 607 return $this->extraFields[$entity_type_id][$bundle];
Chris@0 608 }
Chris@0 609
Chris@0 610 $extra = $this->moduleHandler->invokeAll('entity_extra_field_info');
Chris@0 611 $this->moduleHandler->alter('entity_extra_field_info', $extra);
Chris@0 612 $info = isset($extra[$entity_type_id][$bundle]) ? $extra[$entity_type_id][$bundle] : [];
Chris@0 613 $info += [
Chris@0 614 'form' => [],
Chris@0 615 'display' => [],
Chris@0 616 ];
Chris@0 617
Chris@0 618 // Store in the 'static' and persistent caches.
Chris@0 619 $this->extraFields[$entity_type_id][$bundle] = $info;
Chris@0 620 $this->cacheSet($cache_id, $info, Cache::PERMANENT, [
Chris@0 621 'entity_field_info',
Chris@0 622 ]);
Chris@0 623
Chris@0 624 return $this->extraFields[$entity_type_id][$bundle];
Chris@0 625 }
Chris@0 626
Chris@0 627 }