View source
<?php
namespace Drupal\Core\Entity;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\UseCacheBackendTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class EntityDisplayRepository implements EntityDisplayRepositoryInterface {
use UseCacheBackendTrait;
use StringTranslationTrait;
protected $displayModeInfo = [];
protected $languageManager;
protected $entityTypeManager;
protected $moduleHandler;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->cacheBackend = $cache_backend;
$this->languageManager = $language_manager;
}
public function getAllViewModes() {
return $this
->getAllDisplayModesByEntityType('view_mode');
}
public function getViewModes($entity_type_id) {
return $this
->getDisplayModesByEntityType('view_mode', $entity_type_id);
}
public function getAllFormModes() {
return $this
->getAllDisplayModesByEntityType('form_mode');
}
public function getFormModes($entity_type_id) {
return $this
->getDisplayModesByEntityType('form_mode', $entity_type_id);
}
protected function getAllDisplayModesByEntityType($display_type) {
if (!isset($this->displayModeInfo[$display_type])) {
$key = 'entity_' . $display_type . '_info';
$entity_type_id = 'entity_' . $display_type;
$langcode = $this->languageManager
->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE)
->getId();
if ($cache = $this
->cacheGet("{$key}:{$langcode}")) {
$this->displayModeInfo[$display_type] = $cache->data;
}
else {
$this->displayModeInfo[$display_type] = [];
foreach ($this->entityTypeManager
->getStorage($entity_type_id)
->loadMultiple() as $display_mode) {
list($display_mode_entity_type, $display_mode_name) = explode('.', $display_mode
->id(), 2);
$this->displayModeInfo[$display_type][$display_mode_entity_type][$display_mode_name] = $display_mode
->toArray();
}
$this->moduleHandler
->alter($key, $this->displayModeInfo[$display_type]);
$this
->cacheSet("{$key}:{$langcode}", $this->displayModeInfo[$display_type], CacheBackendInterface::CACHE_PERMANENT, [
'entity_types',
'entity_field_info',
]);
}
}
return $this->displayModeInfo[$display_type];
}
protected function getDisplayModesByEntityType($display_type, $entity_type_id) {
if (isset($this->displayModeInfo[$display_type][$entity_type_id])) {
return $this->displayModeInfo[$display_type][$entity_type_id];
}
else {
$display_modes = $this
->getAllDisplayModesByEntityType($display_type);
if (isset($display_modes[$entity_type_id])) {
return $display_modes[$entity_type_id];
}
}
return [];
}
public function getViewModeOptions($entity_type) {
return $this
->getDisplayModeOptions('view_mode', $entity_type);
}
public function getFormModeOptions($entity_type_id) {
return $this
->getDisplayModeOptions('form_mode', $entity_type_id);
}
public function getViewModeOptionsByBundle($entity_type_id, $bundle) {
return $this
->getDisplayModeOptionsByBundle('view_mode', $entity_type_id, $bundle);
}
public function getFormModeOptionsByBundle($entity_type_id, $bundle) {
return $this
->getDisplayModeOptionsByBundle('form_mode', $entity_type_id, $bundle);
}
protected function getDisplayModeOptions($display_type, $entity_type_id) {
$options = [
'default' => t('Default'),
];
foreach ($this
->getDisplayModesByEntityType($display_type, $entity_type_id) as $mode => $settings) {
$options[$mode] = $settings['label'];
}
return $options;
}
protected function getDisplayModeOptionsByBundle($display_type, $entity_type_id, $bundle) {
$options = $this
->getDisplayModeOptions($display_type, $entity_type_id);
$load_ids = [];
foreach (array_keys($options) as $mode) {
$load_ids[] = $entity_type_id . '.' . $bundle . '.' . $mode;
}
$displays = $this->entityTypeManager
->getStorage($display_type == 'form_mode' ? 'entity_form_display' : 'entity_view_display')
->loadMultiple($load_ids);
foreach (array_keys($options) as $mode) {
$display_id = $entity_type_id . '.' . $bundle . '.' . $mode;
if (!isset($displays[$display_id]) || !$displays[$display_id]
->status()) {
unset($options[$mode]);
}
}
return $options;
}
public function clearDisplayModeInfo() {
$this->displayModeInfo = [];
return $this;
}
public function getViewDisplay($entity_type, $bundle, $view_mode = self::DEFAULT_DISPLAY_MODE) {
$storage = $this->entityTypeManager
->getStorage('entity_view_display');
$entity_view_display = $storage
->load($entity_type . '.' . $bundle . '.' . $view_mode);
if (!$entity_view_display) {
$entity_view_display = $storage
->create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $view_mode,
'status' => TRUE,
]);
}
return $entity_view_display;
}
public function getFormDisplay($entity_type, $bundle, $form_mode = self::DEFAULT_DISPLAY_MODE) {
$storage = $this->entityTypeManager
->getStorage('entity_form_display');
$entity_form_display = $storage
->load($entity_type . '.' . $bundle . '.' . $form_mode);
if (!$entity_form_display) {
$entity_form_display = $storage
->create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $form_mode,
'status' => TRUE,
]);
}
return $entity_form_display;
}
}