You are here

public function MediaDownload::getUrl in Media Entity Download 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Linkit/Substitution/MediaDownload.php \Drupal\media_entity_download\Plugin\Linkit\Substitution\MediaDownload::getUrl()

Get the URL associated with a given entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to get a URL for.

Return value

\Drupal\Core\GeneratedUrl A url to replace.

Overrides SubstitutionInterface::getUrl

File

src/Plugin/Linkit/Substitution/MediaDownload.php, line 71

Class

MediaDownload
A substitution plugin for a direct download link to a file.

Namespace

Drupal\media_entity_download\Plugin\Linkit\Substitution

Code

public function getUrl(EntityInterface $entity) {
  $url = new GeneratedUrl();

  /** @var \Drupal\media\Entity\MediaType $media_bundle */
  $media_bundle = $this->entityTypeManager
    ->getStorage('media_type')
    ->load($entity
    ->bundle());

  // Default to the canonical URL if the bundle doesn't have a source field.
  if (empty($media_bundle
    ->getSource()
    ->getConfiguration()['source_field'])) {
    return $entity
      ->toUrl('canonical')
      ->toString(TRUE);
  }

  // @todo: Discover if we can support file field deltas at some point via the suggestion matcher.
  $url_object = Url::fromRoute('media_entity_download.download', [
    'media' => $entity
      ->id(),
  ], [
    'query' => [
      $this
        ->getContentDisposition() => NULL,
    ],
  ]);
  $url
    ->setGeneratedUrl($url_object
    ->toString());
  $url
    ->addCacheableDependency($entity);
  return $url;
}