You are here

public function DAMAsset::getUrl in Media: Acquia DAM 8

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 Media::getUrl

File

src/Plugin/Linkit/Substitution/DAMAsset.php, line 56

Class

DAMAsset
A substitution plugin for the URL to a file.

Namespace

Drupal\media_acquiadam\Plugin\Linkit\Substitution

Code

public function getUrl(EntityInterface $entity) {

  // We need special handling for Acquia DAM media sources.
  // LinkIt assumes that a Media source field will be a FileInterface which is
  // not a valid assumption.
  if ($entity instanceof MediaInterface) {
    $source = $entity
      ->getSource();
    if (!empty($source) && $source instanceof AcquiadamAsset) {
      $fid = $source
        ->getMetadata($entity, 'file');
      if (!empty($fid)) {
        $file = $this->entityTypeManager
          ->getStorage('file')
          ->load($fid);

        // This is the original LinkIt behavior.
        if (!empty($file) && $file instanceof FileInterface) {
          $url = new GeneratedUrl();
          $url
            ->setGeneratedUrl(file_create_url($file
            ->getFileUri()));
          $url
            ->addCacheableDependency($entity);
          return $url;
        }
      }
    }
  }
  return parent::getUrl($entity);
}