PreviewFactory.php in View Modes Display 8.2
File
src/Service/PreviewFactory.php
View source
<?php
namespace Drupal\view_modes_display\Service;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class PreviewFactory {
protected $configFactory;
protected $entityTypeManager;
protected $entityDisplayRepository;
public function __construct(ConfigFactoryInterface $configFactory, EntityTypeManagerInterface $entityTypeManager, EntityDisplayRepositoryInterface $entityDisplayRepository) {
$this->configFactory = $configFactory;
$this->entityTypeManager = $entityTypeManager;
$this->entityDisplayRepository = $entityDisplayRepository;
}
public function preview(ContentEntityInterface $entity) {
$entityTypeId = $entity
->getEntityType()
->get('id');
$entityDisplays = $this
->getEntityDisplays($entityTypeId, $entity
->bundle());
$enabledDisplayModes = $this
->getEnabledDisplayModes($entityDisplays);
$viewModes = $this->entityDisplayRepository
->getViewModes($entityTypeId);
$renderArray = [];
foreach ($viewModes as $viewMode => $viewModeData) {
if (FALSE == in_array($viewMode, $enabledDisplayModes)) {
continue;
}
$markup = $this
->buildMarkup($entity, $viewMode);
$renderArray[] = [
'#prefix' => '<div class="view-mode-list-item view-mode-list-item-' . $viewMode . '"><div class="view-mode-list-item-label">' . $viewModeData['label'] . '</div><div class="view-mode-list-item-content">',
'#markup' => render($markup),
'#suffix' => '</div></div>',
];
}
return $renderArray;
}
public function getEnabledDisplayModes(array $displays) {
$enabledDisplayModes = [];
foreach ($displays as $display) {
if ($display
->status()) {
$enabledDisplayModes[] = $display
->get('mode');
}
}
if (FALSE == array_key_exists('full', $enabledDisplayModes)) {
$enabledDisplayModes[] = 'full';
}
return $enabledDisplayModes;
}
public function getEntityDisplays($entityTypeId, $entityBundle) {
$configPrefix = 'core.entity_view_display';
$prefix = $configPrefix . '.' . $entityTypeId . '.' . $entityBundle . '.';
$configKeys = $this->configFactory
->listAll($prefix);
$displayKeys = [];
foreach ($configKeys as $configKey) {
$displayKeys[] = str_replace($configPrefix . '.', '', $configKey);
}
return $this->entityTypeManager
->getStorage('entity_view_display')
->loadMultiple($displayKeys);
}
public function buildMarkup(ContentEntityInterface $entity, $viewMode) {
$entityTypeId = $entity
->getEntityType()
->get('id');
$viewBuilder = $this->entityTypeManager
->getViewBuilder($entityTypeId);
if ('block_content' == $entityTypeId) {
$blockManager = \Drupal::service('plugin.manager.block');
$blockInstance = $blockManager
->createInstance('block_content:' . $entity
->uuid(), [
'view_mode' => $viewMode,
]);
return [
'#theme' => 'block',
'#configuration' => $blockInstance
->getConfiguration(),
'#plugin_id' => $blockInstance
->getPluginId(),
'#base_plugin_id' => $blockInstance
->getBaseId(),
'#derivative_plugin_id' => $blockInstance
->getDerivativeId(),
'content' => $blockInstance
->build(),
];
}
return $viewBuilder
->view($entity, $viewMode);
}
}