
| Current Path : /var/www/html/rocksensor1/web/core/modules/file/src/Plugin/Field/FieldFormatter/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html/rocksensor1/web/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php |
<?php
namespace Drupal\file\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\StringTranslation\ByteSizeMarkup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Plugin implementation of the 'file_table' formatter.
*/
#[FieldFormatter(
id: 'file_table',
label: new TranslatableMarkup('Table of files'),
field_types: [
'file',
],
)]
class TableFormatter extends DescriptionAwareFileFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
if ($files = $this->getEntitiesToView($items, $langcode)) {
$header = [$this->t('Attachment'), $this->t('Size')];
$rows = [];
foreach ($files as $file) {
$item = $file->_referringItem;
$rows[] = [
[
'data' => [
'#theme' => 'file_link',
'#file' => $file,
'#description' => $this->getSetting('use_description_as_link_text') ? $item->description : NULL,
'#cache' => [
'tags' => $file->getCacheTags(),
],
],
],
['data' => $file->getSize() !== NULL ? ByteSizeMarkup::create($file->getSize()) : $this->t('Unknown')],
];
}
$elements[0] = [];
if (!empty($rows)) {
$elements[0] = [
'#theme' => 'table__file_formatter_table',
'#header' => $header,
'#rows' => $rows,
];
}
}
return $elements;
}
}