You are here

public function GenericFileFormatter::viewElements in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\GenericFileFormatter::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/GenericFileFormatter.php, line 28
Contains \Drupal\file\Plugin\Field\FieldFormatter\GenericFileFormatter.

Class

GenericFileFormatter
Plugin implementation of the 'file_default' formatter.

Namespace

Drupal\file\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $file) {
    $item = $file->_referringItem;
    $elements[$delta] = array(
      '#theme' => 'file_link',
      '#file' => $file,
      '#description' => $item->description,
      '#cache' => array(
        'tags' => $file
          ->getCacheTags(),
      ),
    );

    // Pass field item attributes to the theme function.
    if (isset($item->_attributes)) {
      $elements[$delta] += array(
        '#attributes' => array(),
      );
      $elements[$delta]['#attributes'] += $item->_attributes;

      // Unset field item attributes since they have been included in the
      // formatter output and should not be rendered in the field template.
      unset($item->_attributes);
    }
  }
  return $elements;
}