"view" filter does not render paragraph display modes.

Created on 21 May 2025, about 1 month ago

Problem/Motivation

When building a carousel of content, using nested paragraphs, I was trying to use {{ drupal_entity() }}, to show different display view modes of the same paragraph, to show the thumbnail images, and then each carousel item at the same time in a template.

Where this would show the carousel "tabs" or thumbnails for each slide.

                {% for title in paragraph.carousel_items %}
                    {{ drupal_entity('paragraph', title.entity.id(), 'carousel_tab_title') }}
                {% endfor %}

and then this.

                {% for slide in paragraph.carousel_items %}
                    <div class="swiper-slide">
                    {{ drupal_entity('paragraph', slide.entity.id(), 'carousel_tab_content') }}
                    </div>
                {% endfor %}

Would show the proper carousel...

This works... except... I discovered was that this doesn't work when you try to PREVIEW the node.

This : https://www.drupal.org/project/twig_tweak/issues/3365729 πŸ’¬ Twigtweak breaks preview, also TypeError: Drupal\twig_tweak\TwigTweakExtension::drupalField(): Argument #3 ($id) must be of type string, null given Closed: duplicate and
https://www.drupal.org/project/twig_tweak/issues/3182657 β†’ say to use the "|view" filter to render things like this instead, for reasons that make sense.

So I tried

{{ paragraph.carousel_items|view('carousel_tab_title') }}

and

{{ paragraph.carousel_items|view('carousel_tab_content') }}

And it doesn't work.

In both cases, it renders the DEFAULT view mode of the paragraph type, not the display view mode that I put in to the argument.

The documentation here : https://git.drupalcode.org/project/twig_tweak/-/blob/3.x/docs/cheat-shee... suggests I can pass a view mode? Or ... am I doing something wrong?

Or is this a legit bug?

Thank you.

πŸ› Bug report
Status

Active

Version

3.4

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States alphex Atlanta, GA USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @alphex
  • πŸ‡ΊπŸ‡ΈUnited States alphex Atlanta, GA USA
  • πŸ‡·πŸ‡ΊRussia Chi

    It should work. Check that the specified view modes are configured correctly.

  • πŸ‡ΊπŸ‡ΈUnited States alphex Atlanta, GA USA

    They are...

    I have two files.

    paragraph--carousel-tab-item--carousel-tab-title.html.twig
    and
    paragraph--carousel-tab-item--carousel-tab-content.html.twig

    This goes along with two display modes.

    "Carousel Tab Content" and "Carousel Tab Title".

    Which both are assigned to the nested paragraph type, "Carousel Tab Item"

    Using
    {{ drupal_entity('paragraph', title.entity.id(), 'carousel_tab_title') }}
    To get those paragraphs, and passing it the view mode argument, DOES work... and gives me the correct tempplate suggestions.

    But when I use
    {{ paragraph.carousel_items|view('carousel_tab_title') }}

    It renders it as the default view, and does not give me alternate templates to use.

  • πŸ‡·πŸ‡ΊRussia Chi

    paragraph.carousel_items|view('carousel_tab_title')

    I think , in this case the carousel_tab_title view mode is expected to be defined on parent paragraph. But that's might be not what you want. You need to loop over paragraph entities stored in carousel_items field and apply the view mode to each one individually.

Production build 0.71.5 2024