You are here

public function TableFormatter::viewElements in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\TableFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php, line 28
Contains \Drupal\file\Plugin\Field\FieldFormatter\TableFormatter.

Class

TableFormatter
Plugin implementation of the 'file_table' formatter.

Namespace

Drupal\file\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  if ($files = $this
    ->getEntitiesToView($items, $langcode)) {
    $header = array(
      t('Attachment'),
      t('Size'),
    );
    $rows = array();
    foreach ($files as $delta => $file) {
      $rows[] = array(
        array(
          'data' => array(
            '#theme' => 'file_link',
            '#file' => $file,
            '#cache' => array(
              'tags' => $file
                ->getCacheTags(),
            ),
          ),
        ),
        array(
          'data' => format_size($file
            ->getSize()),
        ),
      );
    }
    $elements[0] = array();
    if (!empty($rows)) {
      $elements[0] = array(
        '#theme' => 'table__file_formatter_table',
        '#header' => $header,
        '#rows' => $rows,
      );
    }
  }
  return $elements;
}