You are here

public function FileLinkFormatter::viewElements in File Link 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldFormatter/FileLinkFormatter.php \Drupal\file_link\Plugin\Field\FieldFormatter\FileLinkFormatter::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 LinkFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/FileLinkFormatter.php, line 27

Class

FileLinkFormatter
Plugin implementation of the 'file_link' formatter.

Namespace

Drupal\file_link\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = parent::viewElements($items, $langcode);
  foreach ($items as $delta => $item) {
    if (!isset($element[$delta])) {
      continue;
    }
    $new_element = [
      '#theme' => 'file_link_formatter',
      '#link' => $element[$delta],
    ];
    if (!empty($item->size)) {
      $size = $this
        ->getSetting('format_size') ? format_size($item->size) : $item->size;
      $new_element['#size'] = $size;
    }
    if (!empty($item->format)) {
      $new_element['#format'] = $item->format;
    }
    $element[$delta] = $new_element;
  }
  return $element;
}