You are here

public function PreviewController::previewList in View Modes Display 8.2

Provides a link list with all available - dedicated - view mode previews.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match:

string $entity_type:

Return value

array

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityMalformedException

File

src/Controller/PreviewController.php, line 114

Class

PreviewController
Class DefaultController.

Namespace

Drupal\view_modes_display\Controller

Code

public function previewList(RouteMatchInterface $route_match, $entity_type) {
  $content = [];
  $links = [];
  $view_modes = $this->entityDisplayRepository
    ->getViewModes($entity_type);

  /** @var EntityInterface $entity */
  $entity = $route_match
    ->getParameter($entity_type);
  $entityDisplays = $this->previewFactory
    ->getEntityDisplays($entity_type, $entity
    ->bundle());
  foreach ($this->previewFactory
    ->getEnabledDisplayModes($entityDisplays) as $display) {
    $label = ucfirst($display);
    if (isset($view_modes[$display]['label'])) {
      $label = $view_modes[$display]['label'];
    }
    $url = $entity
      ->toUrl('vmd-preview-list');
    $url = $url
      ->setRouteParameter('view_mode', $display);
    $links[] = [
      '#type' => 'link',
      '#url' => $url,
      '#title' => t('Preview %label', [
        '%label' => $label,
      ]),
    ];
  }
  $content['preview_links'] = [
    '#theme' => 'item_list',
    '#items' => $links,
    '#title' => t('Available ViewMode Previews:'),
  ];
  return $content;
}