Commit eeff52ca authored by Bryce Gilhome's avatar Bryce Gilhome Committed by Sascha Grossenbacher
Browse files

Issue #3100224 by bgilhome, JeroenT, Alina Basarabeanu: Views integration -...

Issue #3100224 by bgilhome, JeroenT, Alina Basarabeanu: Views integration - Add link to download file
parent f089a5c3
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Contains Views integration for the media_entity_download module.
 */

/**
 * Implements hook_views_data().
 */
function media_entity_download_views_data() {
  $data = [];
  $data['media']['media_download_link'] = [
    'title' => t('Link to download media file'),
    'help' => t('Provide a link to download the media file.'),
    'click sortable' => FALSE,
    'field' => [
      'id' => 'media_download_link',
    ],
  ];
  return $data;
}
+34 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\media_entity_download\Plugin\views\field;

use Drupal\Core\Url;
use Drupal\views\Plugin\views\field\EntityLink;
use Drupal\views\ResultRow;

/**
 * Field handler to present a link to download the media file.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("media_download_link")
 */
class DownloadLink extends EntityLink {

  /**
   * {@inheritdoc}
   */
  protected function getUrlInfo(ResultRow $row) {
    /** @var \Drupal\media\MediaInterface $media */
    $media = $this->getEntity($row);
    return Url::fromRoute('media_entity_download.download', ['media' => $media->id()])->setAbsolute($this->options['absolute'] ?? FALSE);
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultLabel() {
    return $this->t('Download');
  }

}