3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Cache\UseCacheBackendTrait;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Language\LanguageInterface;
9 use Drupal\Core\Language\LanguageManagerInterface;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
13 * Provides a repository for entity display objects (view modes and form modes).
15 class EntityDisplayRepository implements EntityDisplayRepositoryInterface {
17 use UseCacheBackendTrait;
18 use StringTranslationTrait;
21 * Static cache of display modes information.
25 protected $displayModeInfo = [];
28 * The language manager.
30 * @var \Drupal\Core\Language\LanguageManagerInterface
32 protected $languageManager;
35 * The entity type manager.
37 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
39 protected $entityTypeManager;
44 * @var \Drupal\Core\Extension\ModuleHandlerInterface
46 protected $moduleHandler;
49 * Constructs a new EntityDisplayRepository.
51 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
52 * The entity type manager.
53 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
55 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
57 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
58 * The language manager.
60 public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager) {
61 $this->entityTypeManager = $entity_type_manager;
62 $this->moduleHandler = $module_handler;
63 $this->cacheBackend = $cache_backend;
64 $this->languageManager = $language_manager;
70 public function getAllViewModes() {
71 return $this->getAllDisplayModesByEntityType('view_mode');
77 public function getViewModes($entity_type_id) {
78 return $this->getDisplayModesByEntityType('view_mode', $entity_type_id);
84 public function getAllFormModes() {
85 return $this->getAllDisplayModesByEntityType('form_mode');
91 public function getFormModes($entity_type_id) {
92 return $this->getDisplayModesByEntityType('form_mode', $entity_type_id);
96 * Gets the entity display mode info for all entity types.
98 * @param string $display_type
99 * The display type to be retrieved. It can be "view_mode" or "form_mode".
102 * The display mode info for all entity types.
104 protected function getAllDisplayModesByEntityType($display_type) {
105 if (!isset($this->displayModeInfo[$display_type])) {
106 $key = 'entity_' . $display_type . '_info';
107 $entity_type_id = 'entity_' . $display_type;
108 $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE)->getId();
109 if ($cache = $this->cacheGet("$key:$langcode")) {
110 $this->displayModeInfo[$display_type] = $cache->data;
113 $this->displayModeInfo[$display_type] = [];
114 foreach ($this->entityTypeManager->getStorage($entity_type_id)->loadMultiple() as $display_mode) {
115 list($display_mode_entity_type, $display_mode_name) = explode('.', $display_mode->id(), 2);
116 $this->displayModeInfo[$display_type][$display_mode_entity_type][$display_mode_name] = $display_mode->toArray();
118 $this->moduleHandler->alter($key, $this->displayModeInfo[$display_type]);
119 $this->cacheSet("$key:$langcode", $this->displayModeInfo[$display_type], CacheBackendInterface::CACHE_PERMANENT, ['entity_types', 'entity_field_info']);
123 return $this->displayModeInfo[$display_type];
127 * Gets the entity display mode info for a specific entity type.
129 * @param string $display_type
130 * The display type to be retrieved. It can be "view_mode" or "form_mode".
131 * @param string $entity_type_id
132 * The entity type whose display mode info should be returned.
135 * The display mode info for a specific entity type.
137 protected function getDisplayModesByEntityType($display_type, $entity_type_id) {
138 if (isset($this->displayModeInfo[$display_type][$entity_type_id])) {
139 return $this->displayModeInfo[$display_type][$entity_type_id];
142 $display_modes = $this->getAllDisplayModesByEntityType($display_type);
143 if (isset($display_modes[$entity_type_id])) {
144 return $display_modes[$entity_type_id];
153 public function getViewModeOptions($entity_type) {
154 return $this->getDisplayModeOptions('view_mode', $entity_type);
160 public function getFormModeOptions($entity_type_id) {
161 return $this->getDisplayModeOptions('form_mode', $entity_type_id);
167 public function getViewModeOptionsByBundle($entity_type_id, $bundle) {
168 return $this->getDisplayModeOptionsByBundle('view_mode', $entity_type_id, $bundle);
174 public function getFormModeOptionsByBundle($entity_type_id, $bundle) {
175 return $this->getDisplayModeOptionsByBundle('form_mode', $entity_type_id, $bundle);
179 * Gets an array of display mode options.
181 * @param string $display_type
182 * The display type to be retrieved. It can be "view_mode" or "form_mode".
183 * @param string $entity_type_id
184 * The entity type whose display mode options should be returned.
187 * An array of display mode labels, keyed by the display mode ID.
189 protected function getDisplayModeOptions($display_type, $entity_type_id) {
190 $options = ['default' => t('Default')];
191 foreach ($this->getDisplayModesByEntityType($display_type, $entity_type_id) as $mode => $settings) {
192 $options[$mode] = $settings['label'];
198 * Returns an array of enabled display mode options by bundle.
200 * @param $display_type
201 * The display type to be retrieved. It can be "view_mode" or "form_mode".
202 * @param string $entity_type_id
203 * The entity type whose display mode options should be returned.
204 * @param string $bundle
205 * The name of the bundle.
208 * An array of display mode labels, keyed by the display mode ID.
210 protected function getDisplayModeOptionsByBundle($display_type, $entity_type_id, $bundle) {
211 // Collect all the entity's display modes.
212 $options = $this->getDisplayModeOptions($display_type, $entity_type_id);
214 // Filter out modes for which the entity display is disabled
215 // (or non-existent).
217 // Get the list of available entity displays for the current bundle.
218 foreach (array_keys($options) as $mode) {
219 $load_ids[] = $entity_type_id . '.' . $bundle . '.' . $mode;
222 // Load the corresponding displays.
223 $displays = $this->entityTypeManager
224 ->getStorage($display_type == 'form_mode' ? 'entity_form_display' : 'entity_view_display')
225 ->loadMultiple($load_ids);
227 // Unset the display modes that are not active or do not exist.
228 foreach (array_keys($options) as $mode) {
229 $display_id = $entity_type_id . '.' . $bundle . '.' . $mode;
230 if (!isset($displays[$display_id]) || !$displays[$display_id]->status()) {
231 unset($options[$mode]);
241 public function clearDisplayModeInfo() {
242 $this->displayModeInfo = [];