You are here

public function TwigExtension::fileUrl in Twig Tweak 8.2

Returns a URL path to the file.

Parameters

string|object $input: Can be either file URI or an object that contains the URI.

Return value

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

File

src/TwigExtension.php, line 1283

Class

TwigExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\twig_tweak

Code

public function fileUrl($input) {
  if (is_string($input)) {
    return file_url_transform_relative(file_create_url($input));
  }
  if ($input instanceof EntityReferenceFieldItemListInterface) {
    $referenced_entities = $input
      ->referencedEntities();
    if (isset($referenced_entities[0])) {
      return self::getUrlFromEntity($referenced_entities[0]);
    }
  }
  elseif ($input instanceof EntityReferenceItem) {
    return self::getUrlFromEntity($input->entity);
  }
}