0

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.

4
  • I'm sorry if I'm intruding on your theming question, but there is a way to solve it through the Views interface. You can use the custom global field to output custom HTML about the business. I'm assuming the Views lists Offers, and they have ER field towards Business. You use the ER field (Title) to group the results, and add Relationship for the ER field to get the Business info into View. Or maybe even better create a new View mode on the Business CT and use that to display Business info in Views. – prkos Sep 5 '20 at 20:28
  • Thanks for your input. I have in fact already created view with a relationship and group all that data into a global custom field which outputs as "title" in the template file. I also do incidentally output the offers info in another global custom field. I'll try to make a screenshot of the views config page if that will make things easier. – Doomd Sep 5 '20 at 20:42
  • Yup, seems like you're on the right track. What is left to do? Are you open to doing it through the interface or must it be templates? Are you confused about how to split the Business info so Offer info displays in between Business title and the rest? – prkos Sep 5 '20 at 21:31
  • I'm open to doing it however it will work. The trouble is...any of the business data I mark as "grouped", will all output together in the template files under the "title" section. If I don't group it, it will output multiple times with each offers row. I basically need two seperate business groups, and the ability to place them in two seperate areas in the template, or somehow be able to output them separately through the UI. – Doomd Sep 7 '20 at 16:27
1

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.

2
  • Thank you for your answer. I will definitely keep this strategy in mind for some of the other views/content types I have to create...but what I ended up using in this particular case is a simple css "trick". I created a <div> for the first bit section of biz info, then another <div> with two more sub containers...one for the other biz info I wanted at the bottom, and one for the offers in the middle...and then I just used css flex, column-reverse to essentially put the second section of biz info at the bottom (even though its code is generated above the offers). – Doomd Oct 29 '20 at 15:43
  • 1
    Yes, Flexbox can really help with the order of things :) – prkos Oct 29 '20 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.