annotate core/lib/Drupal/Core/Entity/EntityFieldManager.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
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@0 63 * @return 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@0 193 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
Chris@0 194 $class = $entity_type->getClass();
Chris@0 195 $keys = array_filter($entity_type->getKeys());
Chris@0 196
Chris@0 197 // Fail with an exception for non-fieldable entity types.
Chris@0 198 if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
Chris@0 199 throw new \LogicException("Getting the base fields is not supported for entity type {$entity_type->getLabel()}.");
Chris@0 200 }
Chris@0 201
Chris@0 202 // Retrieve base field definitions.
Chris@0 203 /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $base_field_definitions */
Chris@0 204 $base_field_definitions = $class::baseFieldDefinitions($entity_type);
Chris@0 205
Chris@0 206 // Make sure translatable entity types are correctly defined.
Chris@0 207 if ($entity_type->isTranslatable()) {
Chris@0 208 // The langcode field should always be translatable if the entity type is.
Chris@0 209 if (isset($keys['langcode']) && isset($base_field_definitions[$keys['langcode']])) {
Chris@0 210 $base_field_definitions[$keys['langcode']]->setTranslatable(TRUE);
Chris@0 211 }
Chris@0 212 // A default_langcode field should always be defined.
Chris@0 213 if (!isset($base_field_definitions[$keys['default_langcode']])) {
Chris@0 214 $base_field_definitions[$keys['default_langcode']] = BaseFieldDefinition::create('boolean')
Chris@0 215 ->setLabel($this->t('Default translation'))
Chris@0 216 ->setDescription($this->t('A flag indicating whether this is the default translation.'))
Chris@0 217 ->setTranslatable(TRUE)
Chris@0 218 ->setRevisionable(TRUE)
Chris@0 219 ->setDefaultValue(TRUE);
Chris@0 220 }
Chris@0 221 }
Chris@0 222
Chris@0 223 // Make sure that revisionable entity types are correctly defined.
Chris@0 224 if ($entity_type->isRevisionable() && $entity_type->isTranslatable()) {
Chris@0 225 // The 'revision_translation_affected' field should always be defined.
Chris@0 226 // This field has been added unconditionally in Drupal 8.4.0 and it is
Chris@0 227 // overriding any pre-existing definition on purpose so that any
Chris@0 228 // differences are immediately available in the status report.
Chris@0 229 $base_field_definitions[$keys['revision_translation_affected']] = BaseFieldDefinition::create('boolean')
Chris@0 230 ->setLabel($this->t('Revision translation affected'))
Chris@0 231 ->setDescription($this->t('Indicates if the last edit of a translation belongs to current revision.'))
Chris@0 232 ->setReadOnly(TRUE)
Chris@0 233 ->setRevisionable(TRUE)
Chris@0 234 ->setTranslatable(TRUE);
Chris@0 235 }
Chris@0 236
Chris@0 237 // Assign base field definitions the entity type provider.
Chris@0 238 $provider = $entity_type->getProvider();
Chris@0 239 foreach ($base_field_definitions as $definition) {
Chris@0 240 // @todo Remove this check once FieldDefinitionInterface exposes a proper
Chris@0 241 // provider setter. See https://www.drupal.org/node/2225961.
Chris@0 242 if ($definition instanceof BaseFieldDefinition) {
Chris@0 243 $definition->setProvider($provider);
Chris@0 244 }
Chris@0 245 }
Chris@0 246
Chris@0 247 // Retrieve base field definitions from modules.
Chris@0 248 foreach ($this->moduleHandler->getImplementations('entity_base_field_info') as $module) {
Chris@0 249 $module_definitions = $this->moduleHandler->invoke($module, 'entity_base_field_info', [$entity_type]);
Chris@0 250 if (!empty($module_definitions)) {
Chris@0 251 // Ensure the provider key actually matches the name of the provider
Chris@0 252 // defining the field.
Chris@0 253 foreach ($module_definitions as $field_name => $definition) {
Chris@0 254 // @todo Remove this check once FieldDefinitionInterface exposes a
Chris@0 255 // proper provider setter. See https://www.drupal.org/node/2225961.
Chris@0 256 if ($definition instanceof BaseFieldDefinition && $definition->getProvider() == NULL) {
Chris@0 257 $definition->setProvider($module);
Chris@0 258 }
Chris@0 259 $base_field_definitions[$field_name] = $definition;
Chris@0 260 }
Chris@0 261 }
Chris@0 262 }
Chris@0 263
Chris@0 264 // Automatically set the field name, target entity type and bundle
Chris@0 265 // for non-configurable fields.
Chris@0 266 foreach ($base_field_definitions as $field_name => $base_field_definition) {
Chris@0 267 if ($base_field_definition instanceof BaseFieldDefinition) {
Chris@0 268 $base_field_definition->setName($field_name);
Chris@0 269 $base_field_definition->setTargetEntityTypeId($entity_type_id);
Chris@0 270 $base_field_definition->setTargetBundle(NULL);
Chris@0 271 }
Chris@0 272 }
Chris@0 273
Chris@0 274 // Invoke alter hook.
Chris@0 275 $this->moduleHandler->alter('entity_base_field_info', $base_field_definitions, $entity_type);
Chris@0 276
Chris@0 277 // Ensure defined entity keys are there and have proper revisionable and
Chris@0 278 // translatable values.
Chris@0 279 foreach (array_intersect_key($keys, array_flip(['id', 'revision', 'uuid', 'bundle'])) as $key => $field_name) {
Chris@0 280 if (!isset($base_field_definitions[$field_name])) {
Chris@0 281 throw new \LogicException("The $field_name field definition does not exist and it is used as $key entity key.");
Chris@0 282 }
Chris@0 283 if ($base_field_definitions[$field_name]->isRevisionable()) {
Chris@0 284 throw new \LogicException("The {$base_field_definitions[$field_name]->getLabel()} field cannot be revisionable as it is used as $key entity key.");
Chris@0 285 }
Chris@0 286 if ($base_field_definitions[$field_name]->isTranslatable()) {
Chris@0 287 throw new \LogicException("The {$base_field_definitions[$field_name]->getLabel()} field cannot be translatable as it is used as $key entity key.");
Chris@0 288 }
Chris@0 289 }
Chris@0 290
Chris@0 291 // Make sure translatable entity types define the "langcode" field properly.
Chris@0 292 if ($entity_type->isTranslatable() && (!isset($keys['langcode']) || !isset($base_field_definitions[$keys['langcode']]) || !$base_field_definitions[$keys['langcode']]->isTranslatable())) {
Chris@0 293 throw new \LogicException("The {$entity_type->getLabel()} entity type cannot be translatable as it does not define a translatable \"langcode\" field.");
Chris@0 294 }
Chris@0 295
Chris@0 296 return $base_field_definitions;
Chris@0 297 }
Chris@0 298
Chris@0 299 /**
Chris@0 300 * {@inheritdoc}
Chris@0 301 */
Chris@0 302 public function getFieldDefinitions($entity_type_id, $bundle) {
Chris@0 303 if (!isset($this->fieldDefinitions[$entity_type_id][$bundle])) {
Chris@0 304 $base_field_definitions = $this->getBaseFieldDefinitions($entity_type_id);
Chris@0 305 // Not prepared, try to load from cache.
Chris@0 306 $cid = 'entity_bundle_field_definitions:' . $entity_type_id . ':' . $bundle . ':' . $this->languageManager->getCurrentLanguage()->getId();
Chris@0 307 if ($cache = $this->cacheGet($cid)) {
Chris@0 308 $bundle_field_definitions = $cache->data;
Chris@0 309 }
Chris@0 310 else {
Chris@0 311 // Rebuild the definitions and put it into the cache.
Chris@0 312 $bundle_field_definitions = $this->buildBundleFieldDefinitions($entity_type_id, $bundle, $base_field_definitions);
Chris@0 313 $this->cacheSet($cid, $bundle_field_definitions, Cache::PERMANENT, ['entity_types', 'entity_field_info']);
Chris@0 314 }
Chris@0 315 // Field definitions consist of the bundle specific overrides and the
Chris@0 316 // base fields, merge them together. Use array_replace() to replace base
Chris@0 317 // fields with by bundle overrides and keep them in order, append
Chris@0 318 // additional by bundle fields.
Chris@0 319 $this->fieldDefinitions[$entity_type_id][$bundle] = array_replace($base_field_definitions, $bundle_field_definitions);
Chris@0 320 }
Chris@0 321 return $this->fieldDefinitions[$entity_type_id][$bundle];
Chris@0 322 }
Chris@0 323
Chris@0 324 /**
Chris@0 325 * Builds field definitions for a specific bundle within an entity type.
Chris@0 326 *
Chris@0 327 * @param string $entity_type_id
Chris@0 328 * The entity type ID. Only entity types that implement
Chris@0 329 * \Drupal\Core\Entity\FieldableEntityInterface are supported.
Chris@0 330 * @param string $bundle
Chris@0 331 * The bundle.
Chris@0 332 * @param \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions
Chris@0 333 * The list of base field definitions.
Chris@0 334 *
Chris@0 335 * @return \Drupal\Core\Field\FieldDefinitionInterface[]
Chris@0 336 * An array of bundle field definitions, keyed by field name. Does
Chris@0 337 * not include base fields.
Chris@0 338 */
Chris@0 339 protected function buildBundleFieldDefinitions($entity_type_id, $bundle, array $base_field_definitions) {
Chris@0 340 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
Chris@0 341 $class = $entity_type->getClass();
Chris@0 342
Chris@0 343 // Allow the entity class to provide bundle fields and bundle-specific
Chris@0 344 // overrides of base fields.
Chris@0 345 $bundle_field_definitions = $class::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions);
Chris@0 346
Chris@0 347 // Load base field overrides from configuration. These take precedence over
Chris@0 348 // base field overrides returned above.
Chris@0 349 $base_field_override_ids = array_map(function ($field_name) use ($entity_type_id, $bundle) {
Chris@0 350 return $entity_type_id . '.' . $bundle . '.' . $field_name;
Chris@0 351 }, array_keys($base_field_definitions));
Chris@0 352 $base_field_overrides = $this->entityTypeManager->getStorage('base_field_override')->loadMultiple($base_field_override_ids);
Chris@0 353 foreach ($base_field_overrides as $base_field_override) {
Chris@0 354 /** @var \Drupal\Core\Field\Entity\BaseFieldOverride $base_field_override */
Chris@0 355 $field_name = $base_field_override->getName();
Chris@0 356 $bundle_field_definitions[$field_name] = $base_field_override;
Chris@0 357 }
Chris@0 358
Chris@0 359 $provider = $entity_type->getProvider();
Chris@0 360 foreach ($bundle_field_definitions as $definition) {
Chris@0 361 // @todo Remove this check once FieldDefinitionInterface exposes a proper
Chris@0 362 // provider setter. See https://www.drupal.org/node/2225961.
Chris@0 363 if ($definition instanceof BaseFieldDefinition) {
Chris@0 364 $definition->setProvider($provider);
Chris@0 365 }
Chris@0 366 }
Chris@0 367
Chris@0 368 // Retrieve base field definitions from modules.
Chris@0 369 foreach ($this->moduleHandler->getImplementations('entity_bundle_field_info') as $module) {
Chris@0 370 $module_definitions = $this->moduleHandler->invoke($module, 'entity_bundle_field_info', [$entity_type, $bundle, $base_field_definitions]);
Chris@0 371 if (!empty($module_definitions)) {
Chris@0 372 // Ensure the provider key actually matches the name of the provider
Chris@0 373 // defining the field.
Chris@0 374 foreach ($module_definitions as $field_name => $definition) {
Chris@0 375 // @todo Remove this check once FieldDefinitionInterface exposes a
Chris@0 376 // proper provider setter. See https://www.drupal.org/node/2225961.
Chris@0 377 if ($definition instanceof BaseFieldDefinition) {
Chris@0 378 $definition->setProvider($module);
Chris@0 379 }
Chris@0 380 $bundle_field_definitions[$field_name] = $definition;
Chris@0 381 }
Chris@0 382 }
Chris@0 383 }
Chris@0 384
Chris@0 385 // Automatically set the field name, target entity type and bundle
Chris@0 386 // for non-configurable fields.
Chris@0 387 foreach ($bundle_field_definitions as $field_name => $field_definition) {
Chris@0 388 if ($field_definition instanceof BaseFieldDefinition) {
Chris@0 389 $field_definition->setName($field_name);
Chris@0 390 $field_definition->setTargetEntityTypeId($entity_type_id);
Chris@0 391 $field_definition->setTargetBundle($bundle);
Chris@0 392 }
Chris@0 393 }
Chris@0 394
Chris@0 395 // Invoke 'per bundle' alter hook.
Chris@0 396 $this->moduleHandler->alter('entity_bundle_field_info', $bundle_field_definitions, $entity_type, $bundle);
Chris@0 397
Chris@0 398 return $bundle_field_definitions;
Chris@0 399 }
Chris@0 400
Chris@0 401 /**
Chris@0 402 * {@inheritdoc}
Chris@0 403 */
Chris@0 404 public function getFieldStorageDefinitions($entity_type_id) {
Chris@0 405 if (!isset($this->fieldStorageDefinitions[$entity_type_id])) {
Chris@0 406 $this->fieldStorageDefinitions[$entity_type_id] = [];
Chris@0 407 // Add all non-computed base fields.
Chris@0 408 foreach ($this->getBaseFieldDefinitions($entity_type_id) as $field_name => $definition) {
Chris@0 409 if (!$definition->isComputed()) {
Chris@0 410 $this->fieldStorageDefinitions[$entity_type_id][$field_name] = $definition;
Chris@0 411 }
Chris@0 412 }
Chris@0 413 // Not prepared, try to load from cache.
Chris@0 414 $cid = 'entity_field_storage_definitions:' . $entity_type_id . ':' . $this->languageManager->getCurrentLanguage()->getId();
Chris@0 415 if ($cache = $this->cacheGet($cid)) {
Chris@0 416 $field_storage_definitions = $cache->data;
Chris@0 417 }
Chris@0 418 else {
Chris@0 419 // Rebuild the definitions and put it into the cache.
Chris@0 420 $field_storage_definitions = $this->buildFieldStorageDefinitions($entity_type_id);
Chris@0 421 $this->cacheSet($cid, $field_storage_definitions, Cache::PERMANENT, ['entity_types', 'entity_field_info']);
Chris@0 422 }
Chris@0 423 $this->fieldStorageDefinitions[$entity_type_id] += $field_storage_definitions;
Chris@0 424 }
Chris@0 425 return $this->fieldStorageDefinitions[$entity_type_id];
Chris@0 426 }
Chris@0 427
Chris@0 428 /**
Chris@0 429 * {@inheritdoc}
Chris@0 430 */
Chris@0 431 public function setFieldMap(array $field_map) {
Chris@0 432 $this->fieldMap = $field_map;
Chris@0 433 return $this;
Chris@0 434 }
Chris@0 435
Chris@0 436 /**
Chris@0 437 * {@inheritdoc}
Chris@0 438 */
Chris@0 439 public function getFieldMap() {
Chris@0 440 if (!$this->fieldMap) {
Chris@0 441 // Not prepared, try to load from cache.
Chris@0 442 $cid = 'entity_field_map';
Chris@0 443 if ($cache = $this->cacheGet($cid)) {
Chris@0 444 $this->fieldMap = $cache->data;
Chris@0 445 }
Chris@0 446 else {
Chris@0 447 // The field map is built in two steps. First, add all base fields, by
Chris@0 448 // looping over all fieldable entity types. They always exist for all
Chris@0 449 // bundles, and we do not expect to have so many different entity
Chris@0 450 // types for this to become a bottleneck.
Chris@0 451 foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
Chris@0 452 if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
Chris@0 453 $bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id));
Chris@0 454 foreach ($this->getBaseFieldDefinitions($entity_type_id) as $field_name => $base_field_definition) {
Chris@0 455 $this->fieldMap[$entity_type_id][$field_name] = [
Chris@0 456 'type' => $base_field_definition->getType(),
Chris@0 457 'bundles' => array_combine($bundles, $bundles),
Chris@0 458 ];
Chris@0 459 }
Chris@0 460 }
Chris@0 461 }
Chris@0 462
Chris@0 463 // In the second step, the per-bundle fields are added, based on the
Chris@0 464 // persistent bundle field map stored in a key value collection. This
Chris@0 465 // data is managed in the EntityManager::onFieldDefinitionCreate()
Chris@0 466 // and EntityManager::onFieldDefinitionDelete() methods. Rebuilding this
Chris@0 467 // information in the same way as base fields would not scale, as the
Chris@0 468 // time to query would grow exponentially with more fields and bundles.
Chris@0 469 // A cache would be deleted during cache clears, which is the only time
Chris@0 470 // it is needed, so a key value collection is used.
Chris@0 471 $bundle_field_maps = $this->keyValueFactory->get('entity.definitions.bundle_field_map')->getAll();
Chris@0 472 foreach ($bundle_field_maps as $entity_type_id => $bundle_field_map) {
Chris@0 473 foreach ($bundle_field_map as $field_name => $map_entry) {
Chris@0 474 if (!isset($this->fieldMap[$entity_type_id][$field_name])) {
Chris@0 475 $this->fieldMap[$entity_type_id][$field_name] = $map_entry;
Chris@0 476 }
Chris@0 477 else {
Chris@0 478 $this->fieldMap[$entity_type_id][$field_name]['bundles'] += $map_entry['bundles'];
Chris@0 479 }
Chris@0 480 }
Chris@0 481 }
Chris@0 482
Chris@0 483 $this->cacheSet($cid, $this->fieldMap, Cache::PERMANENT, ['entity_types']);
Chris@0 484 }
Chris@0 485 }
Chris@0 486 return $this->fieldMap;
Chris@0 487 }
Chris@0 488
Chris@0 489 /**
Chris@0 490 * {@inheritdoc}
Chris@0 491 */
Chris@0 492 public function getFieldMapByFieldType($field_type) {
Chris@0 493 if (!isset($this->fieldMapByFieldType[$field_type])) {
Chris@0 494 $filtered_map = [];
Chris@0 495 $map = $this->getFieldMap();
Chris@0 496 foreach ($map as $entity_type => $fields) {
Chris@0 497 foreach ($fields as $field_name => $field_info) {
Chris@0 498 if ($field_info['type'] == $field_type) {
Chris@0 499 $filtered_map[$entity_type][$field_name] = $field_info;
Chris@0 500 }
Chris@0 501 }
Chris@0 502 }
Chris@0 503 $this->fieldMapByFieldType[$field_type] = $filtered_map;
Chris@0 504 }
Chris@0 505 return $this->fieldMapByFieldType[$field_type];
Chris@0 506 }
Chris@0 507
Chris@0 508 /**
Chris@0 509 * Builds field storage definitions for an entity type.
Chris@0 510 *
Chris@0 511 * @param string $entity_type_id
Chris@0 512 * The entity type ID. Only entity types that implement
Chris@0 513 * \Drupal\Core\Entity\FieldableEntityInterface are supported
Chris@0 514 *
Chris@0 515 * @return \Drupal\Core\Field\FieldStorageDefinitionInterface[]
Chris@0 516 * An array of field storage definitions, keyed by field name.
Chris@0 517 */
Chris@0 518 protected function buildFieldStorageDefinitions($entity_type_id) {
Chris@0 519 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
Chris@0 520 $field_definitions = [];
Chris@0 521
Chris@0 522 // Retrieve base field definitions from modules.
Chris@0 523 foreach ($this->moduleHandler->getImplementations('entity_field_storage_info') as $module) {
Chris@0 524 $module_definitions = $this->moduleHandler->invoke($module, 'entity_field_storage_info', [$entity_type]);
Chris@0 525 if (!empty($module_definitions)) {
Chris@0 526 // Ensure the provider key actually matches the name of the provider
Chris@0 527 // defining the field.
Chris@0 528 foreach ($module_definitions as $field_name => $definition) {
Chris@0 529 // @todo Remove this check once FieldDefinitionInterface exposes a
Chris@0 530 // proper provider setter. See https://www.drupal.org/node/2225961.
Chris@0 531 if ($definition instanceof BaseFieldDefinition) {
Chris@0 532 $definition->setProvider($module);
Chris@0 533 }
Chris@0 534 $field_definitions[$field_name] = $definition;
Chris@0 535 }
Chris@0 536 }
Chris@0 537 }
Chris@0 538
Chris@0 539 // Invoke alter hook.
Chris@0 540 $this->moduleHandler->alter('entity_field_storage_info', $field_definitions, $entity_type);
Chris@0 541
Chris@0 542 return $field_definitions;
Chris@0 543 }
Chris@0 544
Chris@0 545 /**
Chris@0 546 * {@inheritdoc}
Chris@0 547 */
Chris@0 548 public function clearCachedFieldDefinitions() {
Chris@0 549 $this->baseFieldDefinitions = [];
Chris@0 550 $this->fieldDefinitions = [];
Chris@0 551 $this->fieldStorageDefinitions = [];
Chris@0 552 $this->fieldMap = [];
Chris@0 553 $this->fieldMapByFieldType = [];
Chris@0 554 $this->entityDisplayRepository->clearDisplayModeInfo();
Chris@0 555 $this->extraFields = [];
Chris@0 556 Cache::invalidateTags(['entity_field_info']);
Chris@0 557 // The typed data manager statically caches prototype objects with injected
Chris@0 558 // definitions, clear those as well.
Chris@0 559 $this->typedDataManager->clearCachedDefinitions();
Chris@0 560 }
Chris@0 561
Chris@0 562 /**
Chris@0 563 * {@inheritdoc}
Chris@0 564 */
Chris@0 565 public function useCaches($use_caches = FALSE) {
Chris@0 566 $this->useCaches = $use_caches;
Chris@0 567 if (!$use_caches) {
Chris@0 568 $this->fieldDefinitions = [];
Chris@0 569 $this->baseFieldDefinitions = [];
Chris@0 570 $this->fieldStorageDefinitions = [];
Chris@0 571 }
Chris@0 572 }
Chris@0 573
Chris@0 574 /**
Chris@0 575 * {@inheritdoc}
Chris@0 576 */
Chris@0 577 public function getExtraFields($entity_type_id, $bundle) {
Chris@0 578 // Read from the "static" cache.
Chris@0 579 if (isset($this->extraFields[$entity_type_id][$bundle])) {
Chris@0 580 return $this->extraFields[$entity_type_id][$bundle];
Chris@0 581 }
Chris@0 582
Chris@0 583 // Read from the persistent cache. Since hook_entity_extra_field_info() and
Chris@0 584 // hook_entity_extra_field_info_alter() might contain t() calls, we cache
Chris@0 585 // per language.
Chris@0 586 $cache_id = 'entity_bundle_extra_fields:' . $entity_type_id . ':' . $bundle . ':' . $this->languageManager->getCurrentLanguage()->getId();
Chris@0 587 $cached = $this->cacheGet($cache_id);
Chris@0 588 if ($cached) {
Chris@0 589 $this->extraFields[$entity_type_id][$bundle] = $cached->data;
Chris@0 590 return $this->extraFields[$entity_type_id][$bundle];
Chris@0 591 }
Chris@0 592
Chris@0 593 $extra = $this->moduleHandler->invokeAll('entity_extra_field_info');
Chris@0 594 $this->moduleHandler->alter('entity_extra_field_info', $extra);
Chris@0 595 $info = isset($extra[$entity_type_id][$bundle]) ? $extra[$entity_type_id][$bundle] : [];
Chris@0 596 $info += [
Chris@0 597 'form' => [],
Chris@0 598 'display' => [],
Chris@0 599 ];
Chris@0 600
Chris@0 601 // Store in the 'static' and persistent caches.
Chris@0 602 $this->extraFields[$entity_type_id][$bundle] = $info;
Chris@0 603 $this->cacheSet($cache_id, $info, Cache::PERMANENT, [
Chris@0 604 'entity_field_info',
Chris@0 605 ]);
Chris@0 606
Chris@0 607 return $this->extraFields[$entity_type_id][$bundle];
Chris@0 608 }
Chris@0 609
Chris@0 610 }