You are here

public function FileBrowserController::preview in File Entity Browser 8

Renders a preview of a file for use with File Browser.

Parameters

\Drupal\file\FileInterface $file: The requested file.

string $image_style: (Optional) An image style to preview the (image) file in.

Return value

array A render array representing the preview.

1 string reference to 'FileBrowserController::preview'
file_browser.routing.yml in ./file_browser.routing.yml
file_browser.routing.yml

File

src/Controller/FileBrowserController.php, line 26

Class

FileBrowserController
Endpoints for the File Browser module.

Namespace

Drupal\file_browser\Controller

Code

public function preview(FileInterface $file, $image_style = '') {
  $build = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'file-browser-preview-wrapper',
    ],
    '#attached' => [
      'library' => [
        'file_browser/preview',
      ],
      'drupalSettings' => [
        'file_browser' => [
          'preview_path' => Url::fromRoute('file_browser.preview', [
            'file' => $file
              ->id(),
          ])
            ->toString(),
        ],
      ],
    ],
  ];
  $preview = FileBrowserPreview::getFilePreview($file, $image_style);
  if ($preview['#theme'] === 'image_formatter') {
    $build['image_style'] = [
      '#type' => 'select',
      '#options' => image_style_options(),
      '#value' => $image_style,
    ];
    $build['image_style']['#options'][''] = $this
      ->t('No image style');
  }
  $build['preview'] = $preview;
  return $build;
}