In Drupal 8.9x, I have a view with two content types in it: Each business (and its details are grouped), and underneath each business I'm displaying multiple offers by that business...and then I want to display more business details underneath that.

For example:

Business #1 "title"
 -- offer A for business #1 (row)
 -- offer B for business #1 (row)
Business #1 Details (ideally inserted via the twig template)

Business #2 "title"
-- offer C for business #2 (row)
-- offer D for business #2 (row)
Business #2 Details (ideally inserted via the twig template)

I've got my view set up so that ALMOST all of the business details are grouped into a custom field. This outputs as "title" in the views-view-unformatted.html.twig template file, and all the offers are the "rows" that are placed inside the default twig for loop. The problem is, I need to place more business details after the for loop, and I don't know how to reliably insert that data via the template.

For example, if I try to access the Business's nid:

{% set bizNid = rows[0].content['#row'].node_field_data_node__field_business_location_nid %}

This grabs the very first row in the array, which doesn't necessarily correspond with the first business in the list. I spend a few hours looking through the twig var dump to see if I could find a reliable way of extracting the nid, but I just can't figure it out. My question is, how can I access the grouped/relationship data/fields/content inside a twig template file so I can sandwich the businesses offers between two sets of business data from the same business?

Here's a gist of my views-view-unformatted.html.twig file for more insight on what I'm trying to do.

And here's a screenshot of ultimately what the output should look like: enter image description here

And here's a screenshot off my Views config page for "offers": enter image description here

Thanks in advance for your help.

有帮助吗?

解决方案

Here's one way to solve this. This is the easiest way to solve it through the interface, without the need for templating or coding.

It may not be the best option performance-wise because of combining data from two db tables through Views, but then again you'd probably need to build a very custom query to optimize, and caching might solve possible lags.

It can all be done through the Offers Views, without Relationships and Contextuals, but with the help of View Modes.

View Modes don't have anything to do with Views module, so this is something to keep in mind, I wish they were named Display Modes or something along those lines. You've probably already used two of them before: Full and Teaser.

View Modes are different ways you can display your entity Fields. Each mode controls which fields are shown, in which order, and with which display settings.

You need to create two additional View Modes for your Business CT (you can do that on the "Manage display" page of your CT or through Structure > Display modes > View modes, it will appear among the tabs). One mode will be for the top Business info (so you should include the fields you want to show there), and the other for what is shown bellow the Offers.

In your Offers Views add the Offers Fields how you want them displayed.

Then add two Business Entity Reference fields as well.

Rearrange the fields so you have one Business reference on top of the fields list, and the other on the bottom.

Configure the top one's Formatter to Rendered entity and choose the View mode you created earlier for it. Exclude it from display and save, then choose it as the 1st grouping field under Format Settings.

Configure the bottom Business reference to also format as Rendered entity but choose the second View mode you created for it.

You should now have it all groupped by Businesses and displaying correct info in correct places. Be mindful of the pagination you set because it can "break" one Business to display on two pages depending on the number of offers.

许可以下: CC-BY-SA归因
scroll top