Score:0

How to pass Paragraph ID from Layout Builder to Views?

us flag

This is a bit convoluted; so hopefully it makes sense:

I have a parent entity: Event. Event has a Paragraph field on it: Sessions (paragraph type: Session).

I use Layout Builder to format the Full Content view mode of the Event. In the layout I add a (paragraph) view which lists the Session paragraphs as Teasers (view mode of Session) with a contextual filter of Paragraph: Parent ID. This all works as expected.

Next I set the Session teaser view mode to use Layout Builder. Set up a LB section and add a couple of the Paragraph's fields. This also works as expected. Which means through all of this, Layout Builder knows which paragraph it should be displaying fields for. Excellent.

Next (and finally) I create another View (paragraph) to create a formatted list of some of my Paragraph fields. I set the contextual filter to Paragraph ID. When I preview by entering the PID of one of my Sessions, this works as expected. When I add this view to my Session layout, of course, on my Event page, the View has no results. This is sad but expected as there is no way that Layout Builder could have passed the paragraph ID to the View - even though LB does know which paragraph to use to display individual paragraph fields.

Is this possible? Is there some trick i have missed which allows the "Session fields" view to know which paragraph it is for? Although I think this is a stretch; it does seem like not that far off from I knowing which field to display.

Score:0
us flag

I am guessing some that will say this is not a great way to do this; but for now, this is how I managed it:

In views_query_alter for the top View (list of Sessions embedded in Event Layout) I create a drupal_static array of that list of paragraph ids.

Then, in a the various views used to build the view mode layout for the Session paragraph, I simply array_pop that drupal_static array and $view->setArguments() to the next paragraph id from the array. The paragraph views are set to have contextual filter as Content ID from URL.

Yes, I know I could do some of this using templates for the paragraph view mode but Views gives me a lot of tools for formatting field data which would be painful to build up otherwise. For example, the fields in question here have static google map formatters buried in a views popup field. Easy to put together in a View.

liquidcms avatar
us flag
I have more than one view to grab bits for my paragraph layout; so i will need to sort out a cool way to manage the array of ids. The array_pop i have now runs on each view so i dont get all pids available to each view. Easy enough to fix though.
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.