0

I have a paragraph which can either contain an image or a video. When I let the output just come out in {{item.content}} the actual youtube iframe is output. However when I start parsing things to detect if a video or image is there I seem to lose this functionality.

I can still manually grab the URL output. However, Drupal seems to have some inbuilt features that I'd like to preserve. For example, the Youtube link ends with watch? which Drupal knows to convert to embed?, otherwise Youtube will refuse the iframe connection.

This is what I'm currently doing.

{% for item in items %}
    <div class="leftItem">
        <h4>{{ item.content['#paragraph'].field_content_2_headline.value }}</h4>
        <p>{{ item.content['#paragraph'].field_content_2_content.value|raw }}</p>
    </div>
    <div class="rightItem">
        {% if item.content['#paragraph'].field_content_2_media.0.entity.field_media_oembed_video is not empty%}
            {{  item.content['#paragraph'].field_content_2_media.0.entity.field_media_oembed_video.value }}
        {% else %}
            {% for list in item.content['#paragraph'].field_content_2_media.0.entity.field_media_image_1 %}
                <img src="{{ file_url(list.entity.fileuri) }}" alt="{{ list.value.alt }}">
            {% endfor %}
        {% endif %}
    </div>
{% endfor %}

If I just output item.content['#paragraph'].field_content_2_media when there's a video detected, I get an error that I'm trying to print a field list item.

Is there a way to reference the iframe functionality that Drupal seems to have?

1

Use field formatters instead of printing raw values. With Twig Tweak module it can be done as follows:

{{ item.content['#paragraph'].field_content_2_media|view }}
0

this one works for me. https://www.drupal.org/project/embed

Set your providers after adding the field

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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