Drupal Views Inside Other Views

Views Field View

We recently published a tutorial explaining how to build a business directory with Drupal.

Questions about directories have been a popular topic lately and some of our members have had more advanced questions.

For example, one member wanted to show all of the restaurants in a certain city.

In this tutorial, we’re going to use a module called Views Field View to show you one approach to getting that done.

Step #1. Set up

In this tutorial, we’re going to show all of the sports teams in a certain city.

First, let’s see our setup.

We have a content type called “Sports Teams” with 2 term reference field: Sport and City.

media_1407529173443.png

The images below show what the term reference fields link to. We have a vocabulary called Sports, with these terms:

media_1407524092555.png

And we have a vocabulary called Cities, with these terms:

media_1407524067794.png

Here’s an example of how the sports teams appear when published.

media_1407524490535.png

Step #2. The Inside View

What we’ll do first is create the view to show our actual sports teams. This will appear inside a larger view that shows our cities.

The settings for this first view are straight-forward:

media_1407529415683.png

Here’s how that view appears with the name of the sports team and the logo.

media_1407529484707.png

What we need to do next is more complex. This is one part of the process that will link this small view to the larger view we’ll create in Step 2.

On the right-hand side of the view we need to create a contextual filter:

  • Go to Contextual Filters and click Add.
  • Check “Content: Has taxonomy term ID”.
media_1407525167364.png
  • Check “Display contents of “No results found””.
  • Check “Specify validation criteria”
  • Choose “Basic validation” and “Display contents of “No results found””.
media_1407525432013.png

When you have done this, your View preview will show as empty. Don’t worry.

Step #3. The Main View

Now we’re going to create a larger view that will show all our cities and our sports teams inside that.

media_1407526043035.png

What you’ll see is a very simple list of the city terms like the one below:

media_1407530107369.png

We’re now going to connect this main view to the view we created in Step 2.

Remember that we created a contextual relationship called “Content: Has taxonomy term ID”. We’re going to now add a “Taxonomy term: Term ID” field, so that the contextual relationship can hook onto it.

  • Click Add next to Fields.
  • Choose “Taxonomy term: Term ID”.
  • Check the ” Exclude from display ” box.
  • Save the field.

So that’s the connection made. Now we need to import our small view into the main view. That’s where Views Field View is useful. That module has provided a field called “Global: View”.

  • Click Add next to Fields.
  • Choose “Global: View”.
  • Choose your View as in the image below:
media_1407525923825.png
  • Find for the “Contextual Filters” box.
  • Click “Replacement Patterns”
  • Take the token for “Taxonomy term: Term ID” and place it into the “Contextual Filters” box.

As you may have guessed, we need “Taxonomy term: Term ID”. This is the final step that tells our mini-view to hook onto the Taxonomy term in the main view.

media_1407530509652.png

Save the field and the image below shows how your view should appear. All of your sports teams should appear next to the correct city. You have a View inside a view and the two views are talking to each other!

media_1407528634082.png

Author

  • Steve Burge

    Steve is the founder of OSTraining. Originally from the UK, he now lives in Sarasota in the USA. Steve's work straddles the line between teaching and web development.

0 0 votes
Article Rating
Subscribe
Notify of
16 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Tanashin Kishimoto
Tanashin Kishimoto
9 years ago

great tutorial!

Wyeknott
Wyeknott
8 years ago

Excellent! This works – now all I have to do re-read and understand it all! Thanks

Marek Hlava
Marek Hlava
8 years ago

thanks for the tutorial it works fine but anonymous users can’t see the table, any idea why?

Jimmy Three Eyes
Jimmy Three Eyes
7 years ago
Reply to  Marek Hlava

sounds like a permissions issue. you’ll have to make sure that anonymous users have permissions access for one or more of the view, the page the view is on, the fields the view is displaying and possibly others (and all depending on what sort of permissions-control modules you have installed)

vivek singla
vivek singla
7 years ago

Sir how to include multiple views in a single view??

frank kelly
frank kelly
7 years ago

Note that, as with so many other Drupal contrib modules, as of 2/3/2017 there is no stable Drupal 8 release for views/field_view.

steve
steve
7 years ago
Reply to  frank kelly

Thanks for reporting that, Frank

Mathias Andersen
Mathias Andersen
6 years ago

In Drupal 8 using [%tid] as the contextual filter gave me no results. Changing it to {{ fields.tid }} seemed to work, but gave me a mile of errors when showing the page. So I ended up using {{ raw_fields.tid }}. This works great.

steve
steve
6 years ago

Thanks for sharing the fix Mathias.

Umair
Umair
6 years ago

embedding working for me. I have a question regarding statuses module. Have you used it? Let me know.

danielpickering
6 years ago
Reply to  Umair

Hi Umair,
We haven’t used that module sorry.

Thomas Gimzer
Thomas Gimzer
6 years ago

Hi. Nice tutorial, but I can’t make it work :/
For an event, I have a programme (each activity is a node), each activity having an infinite number of guests (nodes) and each guest having an infinite number of skills like author, illustrator, etc. (taxonomy terms).
No problem to filter guests by taxonomy term, so my page shows only the guests displaying the skills asked for, but the problem is that I also need to show the activities in which those guests are involved.
I thus used the Views Field View module to show the filtered activities where the shown guests step in.

So my page shows the guests (all authors for example), but for each guest I only managed to show the entire programme, not the filtered programme. I suppose my problem is to pass the contextual filter to my programme, but I can’t manage to do that :/
Can somebody help me with this? Thanks!

James Rome
James Rome
5 years ago

With Drupal 8.5, Mathias Andersen’s fix does not work for me. And it is not an allowed replacement pattern. I tried all of them, and none work for me. Please rewvisit this tutorial for the latest version of Drupal.

James Rome
James Rome
5 years ago

You must add the field Taxonomy term: Term ID above the global view to make it work. See https://www.youtube.com/watch?v=oOKH1kYKnZo

Adler
Adler
3 years ago

Great !

It’s possible to add exposed filters using inside view or field inside view ?

Adler

Neeraj
Neeraj
3 years ago

Hi,
All works great. I am facing the issue when the view (referred by global view field ) returns no result.

I have made all changes to hide the field when there are no results but it is still visible.

Any ideas?

Thanks and Regards,
Neeraj

Ps : Issue : https://www.drupal.org/project/views_field_view/issues/2928014

16
0
Would love your thoughts, please comment.x
()
x