Score:1

How to pass paragraph id from layout builder to view?

us flag

I have asked this question before but in a different, more convoluted context. This is a simpler example.

I have a paragraph type which I use Layout Builder to define it's layout. In that layout I have a View (paragraph) used to display content from that paragraph. If I add a contextual filter for Paragraph ID to my View and in Views preview I enter a PID, the View displays the correct results. But there is no way I can see to pass the PID from my layout to the view.

There is a Views option to set Block settings and enable Contextual filter which then shows up when I configure the block in LB. But this will not accept a token like [paragraph:id]; although technically it shouldn't need this as the View should know the context of where it is placed.

What am I missing here?

No Sssweat avatar
ua flag
`There is a Views option to set Block settings and enable Contextual filter which then shows up when I configure the block in LB` where? I don't see it. Perhaps this is coming from a contrib module that I don't have.
Score:0
ua flag
  • In this case, don't create a paragraph View
  • Instead, create a content View
  • Then under Advanced, add relationship to your node's paragraph field.
  • In Fields, add your paragraph fields.
  • Now you can add context of ID and grab from URL.
liquidcms avatar
us flag
Yes, I have done a few views like that; but for this one there are reasons it is preferred to do this as a Paragraphs view and I am trying to understand how its possible to pass the paragraph ID, or if not, why that wouldn't be possible. As it turns out I have "hacked this in" by setting pid as a static var during paragraph_load and then wrote a new argument handler for PID which pulls this value - but can't help feeling this should be there naturally.
No Sssweat avatar
ua flag
Another hacky way, to be dynamic instead or hard coded, would be to add the paragraph ID as a field in the content view, and then use [Views field view](https://www.drupal.org/project/views_field_view) and to insert the paragraph view in your content view which will pass the paragraph ID as contextual.
No Sssweat avatar
ua flag
`if not, why that wouldn't be possible` @liquidcms Coding wise, anything is possible, I think such feature just needs to be built. However, it probably hasn't been built because its not really needed. Most people don't use Views to display a single paragraph in LB. What most people do is **1)** create a "view mode" (admin/structure/display-modes/view) **2)** Enable view mode in paragraph manage display. **3)** In LB insert the paragraph directly and select the "view mode" they created in step1 for the configuration setting. **4)** Theme the "view mode" by creating a template file (twig) & CSS.
No Sssweat avatar
ua flag
PS: step 1 and 2 isn't mandatory. You could use "default" as your view mode if you like.
Score:0
ve flag

I use this method: in the LB I remove all the fields and keep only the "title" field (for example) and I use "view" as a formatter of the field.

Then choose the view you created previously and as "View Arguments" I select "Entity ID".

I use this method when I need to create views that have "logic" behind that I can edit faster than creating a template / twig.

We use a lot of contrib modules and I don't know if this possibility comes from the core or from a module.

I use this method on D9.

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.