Score:0

How to access url of Remote Video, Document and Image inside twig template?

gh flag

I have a field called Media (machine name: field_media) inside of a content type. This field Media takes the media types of Remote video, Image, and Documents. I am currently trying to render this media inside my twig template as a link to whatever media type it is. For example, if it's a remote youtube video, the link would take you to the video or if it's a pdf document, the link would take you to the internal page to display that document. Examples:

Document: enter image description here Remote video: enter image description here

So inside my field twig template, grab the above values and for each item, place it inside an href attribute like so:

<div{{ item.attributes.addClass('field__item') }}><a href={{ url_of_internal_document_image_or_remote_video }}>{{ item.content }}</a></div>

Inside the href value I've tried values such as node.field_media.entity.field_media_oembed_video.value (for remote video) but that didn't work.

My twig template is displaying like this: enter image description here Which looks how I want but I can't get the link to take me to the remote vid or internal pdf.

Side note: In the Manage Display menu of my content type, I have this configuration for the field. enter image description here

shelane avatar
cn flag
Have you tried `node.field_media.entity.field_media_oembed_video.0`?
gh flag
@shelane yea that didn't work :/ came back as null
Score:1
bo flag

After quite an extensive search and with help of https://www.drupal.org/project/twig_vardumper this worked for me in a Paragraphs Twig template, to print out the remote URL of a youtube video; managed in the Drupal core Media module.

{{ paragraph.field_media_1.entity.field_media_oembed_video.entity.field_media_oembed_video.value }} 

Replace field_media_1 with the system name of your defined field.

gh flag
awesome thanks! Crazy how long some of those variables are lol
Score:0
cn flag

The name of our field is field_images and we have this to display a thumbnail of our video. That if statement may be the key.

{% if content.field_images[0]["#media"].field_media_video_embed_field.0.value %}
  {{ drupal_image(content.field_images[0]["#media"].thumbnail.entity.uri.value, 'ourimagestyle') }}
{% else %}
  {{ content.field_images[0] }}
{% endif %}
gh flag
I am just looking to display a link to the video not the thumbnail but that is good to know for future use
shelane avatar
cn flag
Something in this though `content.field_images[0]["#media"].field_media_video_embed_field.0.value`. If I could ever get the xdebug to work with twig, it would be easy to try to dissect what that variable breakdown looks like.
gh flag
yea seems you have to flush cache everytime you want xdebug to run and even then doesn't always seem to work. I'm trying the https://www.drupal.org/project/twig_vardumper module but not much help either. Thank you though I'll look into something like that!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.