You are here

public function UriExtractor::extractUri in Twig Tweak 3.x

Same name and namespace in other branches
  1. 3.1.x src/UriExtractor.php \Drupal\twig_tweak\UriExtractor::extractUri()

Returns a URI to the file.

Parameters

Object|null $input: An object that contains the URI.

Return value

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

File

src/UriExtractor.php, line 41

Class

UriExtractor
URI extractor service.

Namespace

Drupal\twig_tweak

Code

public function extractUri(?object $input) : ?string {
  $entity = $input;
  if ($input instanceof EntityReferenceFieldItemListInterface) {
    if ($item = $input
      ->first()) {
      $entity = $item->entity;
    }
  }
  elseif ($input instanceof EntityReferenceItem) {
    $entity = $input->entity;
  }

  // Drupal does not clean up references to deleted entities. So that the
  // entity property might be empty while the field item might not.
  // @see https://www.drupal.org/project/drupal/issues/2723323
  return $entity instanceof ContentEntityInterface ? $this
    ->getUriFromEntity($entity) : NULL;
}