Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityDisplayRepository.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
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;
11
12 /**
13  * Provides a repository for entity display objects (view modes and form modes).
14  */
15 class EntityDisplayRepository implements EntityDisplayRepositoryInterface {
16
17   use UseCacheBackendTrait;
18   use StringTranslationTrait;
19
20   /**
21    * Static cache of display modes information.
22    *
23    * @var array
24    */
25   protected $displayModeInfo = [];
26
27   /**
28    * The language manager.
29    *
30    * @var \Drupal\Core\Language\LanguageManagerInterface
31    */
32   protected $languageManager;
33
34   /**
35    * The entity type manager.
36    *
37    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
38    */
39   protected $entityTypeManager;
40
41   /**
42    * The module handler.
43    *
44    * @var \Drupal\Core\Extension\ModuleHandlerInterface
45    */
46   protected $moduleHandler;
47
48   /**
49    * Constructs a new EntityDisplayRepository.
50    *
51    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
52    *   The entity type manager.
53    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
54    *   The module handler.
55    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
56    *   The cache backend.
57    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
58    *   The language manager.
59    */
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;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function getAllViewModes() {
71     return $this->getAllDisplayModesByEntityType('view_mode');
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function getViewModes($entity_type_id) {
78     return $this->getDisplayModesByEntityType('view_mode', $entity_type_id);
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function getAllFormModes() {
85     return $this->getAllDisplayModesByEntityType('form_mode');
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function getFormModes($entity_type_id) {
92     return $this->getDisplayModesByEntityType('form_mode', $entity_type_id);
93   }
94
95   /**
96    * Gets the entity display mode info for all entity types.
97    *
98    * @param string $display_type
99    *   The display type to be retrieved. It can be "view_mode" or "form_mode".
100    *
101    * @return array
102    *   The display mode info for all entity types.
103    */
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;
111       }
112       else {
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();
117         }
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']);
120       }
121     }
122
123     return $this->displayModeInfo[$display_type];
124   }
125
126   /**
127    * Gets the entity display mode info for a specific entity type.
128    *
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.
133    *
134    * @return array
135    *   The display mode info for a specific entity type.
136    */
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];
140     }
141     else {
142       $display_modes = $this->getAllDisplayModesByEntityType($display_type);
143       if (isset($display_modes[$entity_type_id])) {
144         return $display_modes[$entity_type_id];
145       }
146     }
147     return [];
148   }
149
150   /**
151    * {@inheritdoc}
152    */
153   public function getViewModeOptions($entity_type) {
154     return $this->getDisplayModeOptions('view_mode', $entity_type);
155   }
156
157   /**
158    * {@inheritdoc}
159    */
160   public function getFormModeOptions($entity_type_id) {
161     return $this->getDisplayModeOptions('form_mode', $entity_type_id);
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   public function getViewModeOptionsByBundle($entity_type_id, $bundle) {
168     return $this->getDisplayModeOptionsByBundle('view_mode', $entity_type_id, $bundle);
169   }
170
171   /**
172    * {@inheritdoc}
173    */
174   public function getFormModeOptionsByBundle($entity_type_id, $bundle) {
175     return $this->getDisplayModeOptionsByBundle('form_mode', $entity_type_id, $bundle);
176   }
177
178   /**
179    * Gets an array of display mode options.
180    *
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.
185    *
186    * @return array
187    *   An array of display mode labels, keyed by the display mode ID.
188    */
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'];
193     }
194     return $options;
195   }
196
197   /**
198    * Returns an array of enabled display mode options by bundle.
199    *
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.
206    *
207    * @return array
208    *   An array of display mode labels, keyed by the display mode ID.
209    */
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);
213
214     // Filter out modes for which the entity display is disabled
215     // (or non-existent).
216     $load_ids = [];
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;
220     }
221
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);
226
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]);
232       }
233     }
234
235     return $options;
236   }
237
238   /**
239    * {@inheritdoc}
240    */
241   public function clearDisplayModeInfo() {
242     $this->displayModeInfo = [];
243     return $this;
244   }
245
246 }