| Drupal

Views Field ViewWe 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

About the author

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.