1

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

2
  • Have you tried node.field_media.entity.field_media_oembed_video.0?
    – shelane
    Commented Sep 2, 2021 at 20:25
  • @shelane yea that didn't work :/ came back as null Commented Sep 2, 2021 at 22:54

2 Answers 2

4

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.

1
  • awesome thanks! Crazy how long some of those variables are lol Commented Nov 9, 2021 at 20:33
1

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 %}
3
  • I am just looking to display a link to the video not the thumbnail but that is good to know for future use Commented Sep 3, 2021 at 14:00
  • 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.
    – shelane
    Commented Sep 3, 2021 at 14:39
  • 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 drupal.org/project/twig_vardumper module but not much help either. Thank you though I'll look into something like that! Commented Sep 3, 2021 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.