You are here

public function DownloadLinkFieldFormatter::viewElements in Media Entity Download 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/DownloadLinkFieldFormatter.php \Drupal\media_entity_download\Plugin\Field\FieldFormatter\DownloadLinkFieldFormatter::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/DownloadLinkFieldFormatter.php, line 71

Class

DownloadLinkFieldFormatter
Plugin implementation of the 'media_entity_download_download_link' formatter.

Namespace

Drupal\media_entity_download\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $parent = $items
    ->getParent()
    ->getValue()
    ->id();
  $settings = $this
    ->getSettings();
  foreach ($items as $delta => $item) {
    $route_parameters = [
      'media' => $parent,
    ];
    $url_options = [];
    if ($delta > 0) {
      $route_parameters['query']['delta'] = $delta;
    }

    // @todo: replace with DI when this issue is fixed: https://www.drupal.org/node/2053415

    /** @var \Drupal\file\FileInterface $file */
    $file = \Drupal::entityTypeManager()
      ->getStorage('file')
      ->load($item->target_id);
    if (empty($file)) {
      continue;
    }
    $filename = $file
      ->getFilename();
    $mime_type = $file
      ->getMimeType();
    $url_options['attributes'] = [
      'type' => "{$mime_type}; length={$file->getSize()}",
      'title' => $filename,
      // Classes to add to the file field for icons.
      'class' => [
        'file',
        // Add a specific class for each and every mime type.
        'file--mime-' . strtr($mime_type, [
          '/' => '-',
          '.' => '-',
        ]),
        // Add a more general class for groups of well known MIME types.
        'file--' . file_icon_class($mime_type),
      ],
    ];

    // Add download variant.
    $url_options['query'][$settings['disposition']] = NULL;
    if ($settings['disposition'] == ResponseHeaderBag::DISPOSITION_INLINE) {
      if (!empty($settings['target'])) {

        // Link target only relevant for inline downloads (attachment
        // downloads will never navigate client locations)
        $url_options['attributes']['target'] = $settings['target'];
      }
    }
    if (!empty($settings['rel'])) {
      $url_options['attributes']['rel'] = $settings['rel'];
    }
    $url = Url::fromRoute('media_entity_download.download', $route_parameters, $url_options);
    $elements[$delta] = [
      '#type' => 'link',
      '#url' => $url,
      '#title' => $filename,
    ];
  }
  return $elements;
}