EntityTypeBundleInfo.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Cache\Cache;
  4. use Drupal\Core\Cache\CacheBackendInterface;
  5. use Drupal\Core\Cache\UseCacheBackendTrait;
  6. use Drupal\Core\Extension\ModuleHandlerInterface;
  7. use Drupal\Core\Language\LanguageManagerInterface;
  8. use Drupal\Core\TypedData\TypedDataManagerInterface;
  9. /**
  10. * Provides discovery and retrieval of entity type bundles.
  11. */
  12. class EntityTypeBundleInfo implements EntityTypeBundleInfoInterface {
  13. use UseCacheBackendTrait;
  14. /**
  15. * Static cache of bundle information.
  16. *
  17. * @var array
  18. */
  19. protected $bundleInfo;
  20. /**
  21. * The language manager.
  22. *
  23. * @var \Drupal\Core\Language\LanguageManagerInterface
  24. */
  25. protected $languageManager;
  26. /**
  27. * The module handler.
  28. *
  29. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  30. */
  31. protected $moduleHandler;
  32. /**
  33. * The typed data manager.
  34. *
  35. * @var \Drupal\Core\TypedData\TypedDataManagerInterface
  36. */
  37. protected $typedDataManager;
  38. /**
  39. * The entity type manager.
  40. *
  41. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  42. */
  43. protected $entityTypeManager;
  44. /**
  45. * Constructs a new EntityTypeBundleInfo.
  46. *
  47. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  48. * The entity type manager.
  49. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  50. * The language manager.
  51. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  52. * The module handler.
  53. * @param \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager
  54. * The typed data manager.
  55. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  56. * The cache backend.
  57. */
  58. public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, TypedDataManagerInterface $typed_data_manager, CacheBackendInterface $cache_backend) {
  59. $this->entityTypeManager = $entity_type_manager;
  60. $this->languageManager = $language_manager;
  61. $this->moduleHandler = $module_handler;
  62. $this->typedDataManager = $typed_data_manager;
  63. $this->cacheBackend = $cache_backend;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function getBundleInfo($entity_type_id) {
  69. $bundle_info = $this->getAllBundleInfo();
  70. return isset($bundle_info[$entity_type_id]) ? $bundle_info[$entity_type_id] : [];
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function getAllBundleInfo() {
  76. if (empty($this->bundleInfo)) {
  77. $langcode = $this->languageManager->getCurrentLanguage()->getId();
  78. if ($cache = $this->cacheGet("entity_bundle_info:$langcode")) {
  79. $this->bundleInfo = $cache->data;
  80. }
  81. else {
  82. $this->bundleInfo = $this->moduleHandler->invokeAll('entity_bundle_info');
  83. foreach ($this->entityTypeManager->getDefinitions() as $type => $entity_type) {
  84. // First look for entity types that act as bundles for others, load them
  85. // and add them as bundles.
  86. if ($bundle_entity_type = $entity_type->getBundleEntityType()) {
  87. foreach ($this->entityTypeManager->getStorage($bundle_entity_type)->loadMultiple() as $entity) {
  88. $this->bundleInfo[$type][$entity->id()]['label'] = $entity->label();
  89. }
  90. }
  91. // If entity type bundles are not supported and
  92. // hook_entity_bundle_info() has not already set up bundle
  93. // information, use the entity type name and label.
  94. elseif (!isset($this->bundleInfo[$type])) {
  95. $this->bundleInfo[$type][$type]['label'] = $entity_type->getLabel();
  96. }
  97. }
  98. $this->moduleHandler->alter('entity_bundle_info', $this->bundleInfo);
  99. $this->cacheSet("entity_bundle_info:$langcode", $this->bundleInfo, Cache::PERMANENT, ['entity_types', 'entity_bundles']);
  100. }
  101. }
  102. return $this->bundleInfo;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function clearCachedBundles() {
  108. $this->bundleInfo = [];
  109. Cache::invalidateTags(['entity_bundles']);
  110. // Entity bundles are exposed as data types, clear that cache too.
  111. $this->typedDataManager->clearCachedDefinitions();
  112. }
  113. }