You are here

public function PreviewController::previewEntity in View Modes Display 8.2

Returns preview for entity - dedicated view mode or all of them.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: Route match.

string $entity_type:

Return value

array Preview render array.

File

src/Controller/PreviewController.php, line 81

Class

PreviewController
Class DefaultController.

Namespace

Drupal\view_modes_display\Controller

Code

public function previewEntity(RouteMatchInterface $route_match, $entity_type) {

  /** @var EntityInterface $entity */
  $entity = $route_match
    ->getParameter($entity_type);
  $view_mode = $route_match
    ->getParameter('view_mode');
  $view_modes = $this->entityDisplayRepository
    ->getViewModes($entity
    ->getEntityTypeId());
  $renderArray = [];

  // Special view mode placeholder to fetch all. Default in the route
  // definition.
  if ($view_mode == 'all') {
    $renderArray = $this->previewFactory
      ->preview($entity);
  }
  else {
    $markup = $this->previewFactory
      ->buildMarkup($entity, $view_mode);
    $renderArray[] = [
      '#prefix' => '<div class="view-mode-list-item view-mode-list-item-' . $view_mode . '"><div class="view-mode-list-item-label">' . $view_modes[$view_mode]['label'] . '</div><div class="view-mode-list-item-content">',
      '#markup' => render($markup),
      '#suffix' => '</div></div>',
    ];
  }
  return $renderArray;
}