You are here

public function PreviewFactory::preview in View Modes Display 8.2

Preview entity view modes.

Parameters

\Drupal\Core\ContentEntityInterface $entity: Content Entity Interface.

Return value

array Preview content of entity view modes.

File

src/Service/PreviewFactory.php, line 67

Class

PreviewFactory
Class PreviewFactory.

Namespace

Drupal\view_modes_display\Service

Code

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;
}