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!