Score:0

Get parent Paragraph Entity from Referenced View

me flag

I have a custom paragraph type - which has 2 fields:

  • Display Mode,
  • View to be rendered

I want to let users choose Display Mode in this paragraph, then I will render view dynamically based on this Display Mode value.

The problem is, it seems there is no way to get this Display Mode value inside view template(views-view-unformatted.html.twig) or hook (HOOK_preprocess_views_view).

Only what I can access is view object, which definitely does not have any parent entity info.

Any idea?

Score:0
sa flag

It sounds like you need Viewsreference. With this you just need 1 field on the paragraph, the viewsreference field, which provides a widget to select the view (autocomplete, or select) and the display ID, as well as it lets you limit to pre-defined list of views, and display types.

Score:0
ru flag

All entities (*) in Drupal are meant to be stand-alone and self-contained, so do it the other way round: Instead of querying upwards from the view, pass the field value downwards from the paragraph template to the view as a parameter.

E.g. create a paragraph.html.twig template and then call the view with the paramater from your paragraph field (using the Twig Tweak module):

{{ drupal_view('my_view_id', paragraph.field_display_mode.0.value) }}

The first parameter of drupal_view is the machine name of the view, the 2nd parameter is the machine name of the display in that view.

(*) Paragraphs is kind of an exception as it is by data-model explicitly designed to be directly depending on a parent entity.

Austin L avatar
me flag
Simply not working with this error : Twig\Error\SyntaxError: Unknown "drupal_view" function.
ru flag
`{{ drupal_view() }}` is part of the twig tweak module I've linked in my answer
I sit in a Tesla and translated this thread with Ai:

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.