FileBrowserPreview.php in File Entity Browser 8
File
src/Plugin/views/field/FileBrowserPreview.php
View source
<?php
namespace Drupal\file_browser\Plugin\views\field;
use Drupal\Core\Url;
use Drupal\file\FileInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
class FileBrowserPreview extends FieldPluginBase {
public function render(ResultRow $values) {
$file = $this
->getEntity($values);
$build = [];
$build['image'] = $this
->getFilePreview($file, 'file_entity_browser_thumbnail');
$build['preview'] = [
'#title' => $this
->t('Preview'),
'#type' => 'link',
'#url' => Url::fromRoute('file_browser.preview', [
'file' => $file
->id(),
]),
'#attributes' => [
'class' => [
'file-browser-preview-button',
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => '{"classes": {"ui-dialog": "ui-corner-all file-browser-preview-dialog"}, "show": "fadeIn", "hide": "fadeOut"}',
],
];
return $build;
}
public function query() {
}
public function clickSortable() {
return FALSE;
}
public static function getFilePreview(FileInterface $file, $image_style = '') {
$image_factory = \Drupal::service('image.factory');
list($type, ) = explode('/', $file
->getMimeType(), 2);
if ($type == 'image' && ($image = $image_factory
->get($file
->getFileUri())) && $image
->isValid()) {
$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;
}
}
Classes
Name |
Description |
FileBrowserPreview |
Defines a custom field that renders a preview of a file, for the purposes of. |