Drupal Views Inside Other Views

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.

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

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

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

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:

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

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”.

- Check “Display contents of “No results found””.
- Check “Specify validation criteria”
- Choose “Basic validation” and “Display contents of “No results found””.

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.
- Install the Views Field View module: https://www.drupal.org/project/views_field_view
- Create a view for your City taxonomy. This can be a very simple view:

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

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:

- 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.

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!

great tutorial!
Excellent! This works – now all I have to do re-read and understand it all! Thanks
thanks for the tutorial it works fine but anonymous users can’t see the table, any idea why?
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)
Sir how to include multiple views in a single view??
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.
Thanks for reporting that, Frank
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.
Thanks for sharing the fix Mathias.
embedding working for me. I have a question regarding statuses module. Have you used it? Let me know.
Hi Umair,
We haven’t used that module sorry.
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!
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.
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
Great !
It’s possible to add exposed filters using inside view or field inside view ?
Adler
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