Advanced Views: Concepts and ResourcesPosted: May 28, 2013 under Drupal
Although Views is one of the most useful Drupal modules, it's also one of the most complex. As I started thinking about how to discuss the advanced features of this module, I realized that simply cranking out another narrowly focused tutorial wasn't really going to be the best approach.
What we're going to do instead is talk about a couple of the key concepts - basically get some important vocabulary straight - and then I'm going to provide you with a curated list of some of the very best advanced Views tutorials I've come across.
This way, you'll be exposed to a broader set of examples and if we're lucky, one of them may even directly discuss something you're currently working on.
Below is a screenshot highlighting the advanced settings inside Views that we'll be discussing. This image is from our Foundation installation profile and you're free to download it and mess about with both the "About the Author and "Taxonomy term" views you'll find included.
These views make use of the two features we'll be focusing on in this lesson - contextual filters and relationships.
Let's get started by explaining these in the simplest terms possible.
If you're new to Drupal, perhaps coming from a WordPress background, you may have struggled a bit with Drupal terminology. Some of it tends toward "deep nerd" and there isn't anything like the easy to understand documentation of the Codex to help. So it is with contextual filters.
What exactly are contextual filters? Well, they are similar to the simple filters we discussed in the last lesson, except they wait for a value to be provided before returning the view. This value can be provided to Views through the URL or from information loaded along with the content (aka, the node) - things like author id, node id and a lot more.
If you know a bit about SQL, think of it as a WHERE clause that is expecting a variable. If you remember from last time we said views is really just a query builder at heart. When we use any kind of filter, we're adding a WHERE clause to our query. An example of a simple view/query in plain English might be saying, "Drupal, please show me a list of titles where the content type is Article."
With contextual filters the query might change a bit to something more like, "Please show me a list of titles where the author is X". In this case 'X' is a variable that is provided when the page loads. The view might grab the variable from the URL and then use it to run the query and deliver back to you the results. Make sense? The tutorials listed below will help if you're still scratching your head.
Views relationships is another subject that can be tough to break down into simple terms, but let's give it a try. If we talk about it from the standpoint of a SQL query, it's simply a JOIN. If you're not very familiar with SQL, then another way to say it is that relationships allow us to build our queries by combining data from two or more tables inside our Drupal database.
You see, a well structured database will have tables that only hold data about specific types of things - users, comments, etc. Relationships allow us to tell Drupal how these things are connected.
For example, when we create a relationship between comments and users, we let Drupal know these two tables have something in common (the user id) and the query it builds for us should include data from both tables and join it together by using the user id as a connector, so to speak.
A Few Great Tutorials
Hopefully you've got a bit of a handle on what these features do on a conceptual level. I've done some legwork for you and tracked down a few really good tutorials that discuss these advanced features of Views. They will help sort out anything that I've left unclear as well as provide you will some practical things you can use in your projects right away. However, you should remember there is more to advanced views than just contextual filters and relationships.
Views is a very deep topic. You can include views within views, for example. There are just loads of things you might do with Views and many companion modules that extend the functionality. So, although it's a lot, don't freak out. It'll be fun, I swear!
Tutorials on Contextual Filters
- Using Views Contextual Filters to Create a Related Content Block With Drupal 7
- Using Drupal Contextual Filters in Views
- Learn Views with NodeOne, part 19: Contextual filters with paths [VIDEO]
Tutorials on Views Relationships
- Learn Views with NodeOne, part 11: Relationships [VIDEO]
- Views Mini-Course, Part II: Creating Flexible Views with Drupal 7 [Slides]
Unfortunately, the second link on relationships only briefly discusses them, but the rest of it is so good from a conceptual point of view that you should take the time to step through it. There aren't a lot of great tutorials specifically about relationships, but if you're strong technically, enable the Advanced help module and look at the section on Views. For some technical audiences it may prove to be very helpful.
That's it for this lesson in the series.