function TableFormatter::viewElements

Same name in other branches
  1. 9 core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\TableFormatter::viewElements()
  2. 8.9.x core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\TableFormatter::viewElements()
  3. 10 core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\TableFormatter::viewElements()

Overrides FormatterInterface::viewElements

File

core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php, line 25

Class

TableFormatter
Plugin implementation of the 'file_table' formatter.

Namespace

Drupal\file\Plugin\Field\FieldFormatter

Code

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.