EntityDisplayRepository.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Cache\CacheBackendInterface;
  4. use Drupal\Core\Cache\UseCacheBackendTrait;
  5. use Drupal\Core\Extension\ModuleHandlerInterface;
  6. use Drupal\Core\Language\LanguageInterface;
  7. use Drupal\Core\Language\LanguageManagerInterface;
  8. use Drupal\Core\StringTranslation\StringTranslationTrait;
  9. /**
  10. * Provides a repository for entity display objects (view modes and form modes).
  11. */
  12. class EntityDisplayRepository implements EntityDisplayRepositoryInterface {
  13. use UseCacheBackendTrait;
  14. use StringTranslationTrait;
  15. /**
  16. * Static cache of display modes information.
  17. *
  18. * @var array
  19. */
  20. protected $displayModeInfo = [];
  21. /**
  22. * The language manager.
  23. *
  24. * @var \Drupal\Core\Language\LanguageManagerInterface
  25. */
  26. protected $languageManager;
  27. /**
  28. * The entity type manager.
  29. *
  30. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  31. */
  32. protected $entityTypeManager;
  33. /**
  34. * The module handler.
  35. *
  36. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  37. */
  38. protected $moduleHandler;
  39. /**
  40. * Constructs a new EntityDisplayRepository.
  41. *
  42. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  43. * The entity type manager.
  44. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  45. * The module handler.
  46. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  47. * The cache backend.
  48. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  49. * The language manager.
  50. */
  51. public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager) {
  52. $this->entityTypeManager = $entity_type_manager;
  53. $this->moduleHandler = $module_handler;
  54. $this->cacheBackend = $cache_backend;
  55. $this->languageManager = $language_manager;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getAllViewModes() {
  61. return $this->getAllDisplayModesByEntityType('view_mode');
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getViewModes($entity_type_id) {
  67. return $this->getDisplayModesByEntityType('view_mode', $entity_type_id);
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getAllFormModes() {
  73. return $this->getAllDisplayModesByEntityType('form_mode');
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function getFormModes($entity_type_id) {
  79. return $this->getDisplayModesByEntityType('form_mode', $entity_type_id);
  80. }
  81. /**
  82. * Gets the entity display mode info for all entity types.
  83. *
  84. * @param string $display_type
  85. * The display type to be retrieved. It can be "view_mode" or "form_mode".
  86. *
  87. * @return array
  88. * The display mode info for all entity types.
  89. */
  90. protected function getAllDisplayModesByEntityType($display_type) {
  91. if (!isset($this->displayModeInfo[$display_type])) {
  92. $key = 'entity_' . $display_type . '_info';
  93. $entity_type_id = 'entity_' . $display_type;
  94. $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE)->getId();
  95. if ($cache = $this->cacheGet("$key:$langcode")) {
  96. $this->displayModeInfo[$display_type] = $cache->data;
  97. }
  98. else {
  99. $this->displayModeInfo[$display_type] = [];
  100. foreach ($this->entityTypeManager->getStorage($entity_type_id)->loadMultiple() as $display_mode) {
  101. list($display_mode_entity_type, $display_mode_name) = explode('.', $display_mode->id(), 2);
  102. $this->displayModeInfo[$display_type][$display_mode_entity_type][$display_mode_name] = $display_mode->toArray();
  103. }
  104. $this->moduleHandler->alter($key, $this->displayModeInfo[$display_type]);
  105. $this->cacheSet("$key:$langcode", $this->displayModeInfo[$display_type], CacheBackendInterface::CACHE_PERMANENT, ['entity_types', 'entity_field_info']);
  106. }
  107. }
  108. return $this->displayModeInfo[$display_type];
  109. }
  110. /**
  111. * Gets the entity display mode info for a specific entity type.
  112. *
  113. * @param string $display_type
  114. * The display type to be retrieved. It can be "view_mode" or "form_mode".
  115. * @param string $entity_type_id
  116. * The entity type whose display mode info should be returned.
  117. *
  118. * @return array
  119. * The display mode info for a specific entity type.
  120. */
  121. protected function getDisplayModesByEntityType($display_type, $entity_type_id) {
  122. if (isset($this->displayModeInfo[$display_type][$entity_type_id])) {
  123. return $this->displayModeInfo[$display_type][$entity_type_id];
  124. }
  125. else {
  126. $display_modes = $this->getAllDisplayModesByEntityType($display_type);
  127. if (isset($display_modes[$entity_type_id])) {
  128. return $display_modes[$entity_type_id];
  129. }
  130. }
  131. return [];
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function getViewModeOptions($entity_type) {
  137. return $this->getDisplayModeOptions('view_mode', $entity_type);
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function getFormModeOptions($entity_type_id) {
  143. return $this->getDisplayModeOptions('form_mode', $entity_type_id);
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function getViewModeOptionsByBundle($entity_type_id, $bundle) {
  149. return $this->getDisplayModeOptionsByBundle('view_mode', $entity_type_id, $bundle);
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getFormModeOptionsByBundle($entity_type_id, $bundle) {
  155. return $this->getDisplayModeOptionsByBundle('form_mode', $entity_type_id, $bundle);
  156. }
  157. /**
  158. * Gets an array of display mode options.
  159. *
  160. * @param string $display_type
  161. * The display type to be retrieved. It can be "view_mode" or "form_mode".
  162. * @param string $entity_type_id
  163. * The entity type whose display mode options should be returned.
  164. *
  165. * @return array
  166. * An array of display mode labels, keyed by the display mode ID.
  167. */
  168. protected function getDisplayModeOptions($display_type, $entity_type_id) {
  169. $options = ['default' => t('Default')];
  170. foreach ($this->getDisplayModesByEntityType($display_type, $entity_type_id) as $mode => $settings) {
  171. $options[$mode] = $settings['label'];
  172. }
  173. return $options;
  174. }
  175. /**
  176. * Returns an array of enabled display mode options by bundle.
  177. *
  178. * @param $display_type
  179. * The display type to be retrieved. It can be "view_mode" or "form_mode".
  180. * @param string $entity_type_id
  181. * The entity type whose display mode options should be returned.
  182. * @param string $bundle
  183. * The name of the bundle.
  184. *
  185. * @return array
  186. * An array of display mode labels, keyed by the display mode ID.
  187. */
  188. protected function getDisplayModeOptionsByBundle($display_type, $entity_type_id, $bundle) {
  189. // Collect all the entity's display modes.
  190. $options = $this->getDisplayModeOptions($display_type, $entity_type_id);
  191. // Filter out modes for which the entity display is disabled
  192. // (or non-existent).
  193. $load_ids = [];
  194. // Get the list of available entity displays for the current bundle.
  195. foreach (array_keys($options) as $mode) {
  196. $load_ids[] = $entity_type_id . '.' . $bundle . '.' . $mode;
  197. }
  198. // Load the corresponding displays.
  199. $displays = $this->entityTypeManager
  200. ->getStorage($display_type == 'form_mode' ? 'entity_form_display' : 'entity_view_display')
  201. ->loadMultiple($load_ids);
  202. // Unset the display modes that are not active or do not exist.
  203. foreach (array_keys($options) as $mode) {
  204. $display_id = $entity_type_id . '.' . $bundle . '.' . $mode;
  205. if (!isset($displays[$display_id]) || !$displays[$display_id]->status()) {
  206. unset($options[$mode]);
  207. }
  208. }
  209. return $options;
  210. }
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function clearDisplayModeInfo() {
  215. $this->displayModeInfo = [];
  216. return $this;
  217. }
  218. }