You are here

public function MediaDownload::getUrl in Media Entity Download 8

Same name and namespace in other branches
  1. 8.2 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 70

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\MediaBundleInterface $media_bundle */
  $media_bundle = $this->entityTypeManager
    ->getStorage('media_bundle')
    ->load($entity
    ->bundle());

  // Default to the canonical URL if the bundle doesn't have a source field.
  if (empty($media_bundle
    ->getTypeConfiguration()['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(),
  ]);
  $url
    ->setGeneratedUrl($url_object
    ->toString());
  $url
    ->addCacheableDependency($entity);
  return $url;
}