public static function FileBrowserPreview::getFilePreview in File Entity Browser 8
Renders a preview of an arbitrary file.
Parameters
\Drupal\file\FileInterface $file: The file you want to render a preview of.
string $image_style: (Optional) An image style to render the preview in.
Return value
array A render array representing the file preview.
2 calls to FileBrowserPreview::getFilePreview()
- FileBrowserController::preview in src/
Controller/ FileBrowserController.php - Renders a preview of a file for use with File Browser.
- FileBrowserPreview::render in src/
Plugin/ views/ field/ FileBrowserPreview.php - Renders the field.
File
- src/
Plugin/ views/ field/ FileBrowserPreview.php, line 66
Class
- FileBrowserPreview
- Defines a custom field that renders a preview of a file, for the purposes of.
Namespace
Drupal\file_browser\Plugin\views\fieldCode
public static function getFilePreview(FileInterface $file, $image_style = '') {
// Check if this file is an image.
$image_factory = \Drupal::service('image.factory');
// Loading large files is slow, make sure it is an image mime type before
// doing that.
list($type, ) = explode('/', $file
->getMimeType(), 2);
if ($type == 'image' && ($image = $image_factory
->get($file
->getFileUri())) && $image
->isValid()) {
// Fake an ImageItem object.
$item = new \stdClass();
$item->width = $image
->getWidth();
$item->height = $image
->getHeight();
$item->alt = '';
$item->title = $file
->getFilename();
$item->entity = $file;
$build = [
'#theme' => 'image_formatter',
'#item' => $item,
'#image_style' => $image_style,
];
}
else {
$path = drupal_get_path('module', 'file_browser');
$build = [
'#theme' => 'image',
'#attributes' => [
'src' => base_path() . $path . '/images/document_placeholder.svg',
],
];
}
return $build;
}