You are here

private function UrlExtractor::getUrlFromEntity in Twig Tweak 3.1.x

Same name and namespace in other branches
  1. 3.x src/UrlExtractor.php \Drupal\twig_tweak\UrlExtractor::getUrlFromEntity()

Extracts file URL from content entity.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: Entity object that contains information about the file.

bool $relative: (optional) Whether the URL should be root-relative, defaults to true.

Return value

string|null A URL that may be used to access the file.

1 call to UrlExtractor::getUrlFromEntity()
UrlExtractor::extractUrl in src/UrlExtractor.php
Extracts file URL from a string or object.

File

src/UrlExtractor.php, line 84

Class

UrlExtractor
URL extractor service.

Namespace

Drupal\twig_tweak

Code

private function getUrlFromEntity(ContentEntityInterface $entity, bool $relative = TRUE) : ?string {
  if ($entity instanceof MediaInterface) {
    $source = $entity
      ->getSource();
    $value = $source
      ->getSourceFieldValue($entity);
    if (!$value) {
      return NULL;
    }
    elseif ($source instanceof OEmbedInterface) {
      return $value;
    }
    else {

      /** @var \Drupal\file\FileInterface $file */
      $file = $this->entityTypeManager
        ->getStorage('file')
        ->load($value);
      if ($file) {
        return $file
          ->createFileUrl($relative);
      }
    }
  }
  elseif ($entity instanceof FileInterface) {
    return $entity
      ->createFileUrl($relative);
  }
  return NULL;
}