Theme colour switcher

How to get file URI and filename from referenced media entity field in Twig

    <a href="{{ file_url(node.field_document.entity.field_media_document.entity.uri.value) }}">
      {{ node.field_document.entity.field_media_document.entity.filename.value }}
    </a>

Of course you'll need to replace the fieldnames with yours.

The file_url function is necessary to transform the URL into a workable public URL scheme, otherwise you'll get the URL prefixed with Drupal's public files directory.

Tags:

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.