TrackDaFilesFormatter.php in Track da files 8
File
src/Plugin/Field/FieldFormatter/TrackDaFilesFormatter.php
View source
<?php
namespace Drupal\track_da_files\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\Url;
class TrackDaFilesFormatter extends FileFormatterBase {
public function viewElements(FieldItemListInterface $files, $langcode) {
$elements = array();
foreach ($this
->getEntitiesToView($files, $langcode) as $delta => $file) {
$entity = $files
->getEntity();
$type = $entity
->getEntityTypeId();
$entity_bundle = $entity
->getType();
$id = $entity
->id();
$mime_type = $file
->getMimeType();
$filesize = $file
->getSize();
$filename = $file
->getFilename();
$url = track_da_files_create_url($file
->getFileUri());
$options['attributes']['type'] = $mime_type . '; length=' . $filesize;
$options['query']['file'] = '1';
$item = $file->_referringItem;
if (isset($type)) {
$options['query']['type'] = $type;
}
if (isset($id)) {
$options['query']['id'] = $id;
}
$text = isset($item->description) ? $item->description : $filename;
$link = \Drupal::l($text, Url::fromUri($url, $options));
$elements[$delta] = array(
'#markup' => $link,
'#file' => $file,
'#description' => $file->description,
'#entity_type' => $type,
'#entity_bundle' => $entity_bundle,
'#entity_id' => $id,
);
if (isset($item->_attributes)) {
$elements[$delta] += array(
'#attributes' => array(),
);
$elements[$delta]['#attributes'] += $item->_attributes;
unset($item->_attributes);
}
}
if (!empty($elements)) {
$elements['#attached'] = array(
'library' => array(
'file/drupal.file.formatter.generic',
),
);
}
return $elements;
}
}