Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Adding dynamic filters to views

From: Drupal 7: Reporting and Visualizing Data

Video: Adding dynamic filters to views

You already saw how to select records to display in the video, sorting and filtering data, but the tricks you saw there only take you part of the way. They let you filter based on specific criteria. In our example, they only show records of employees. Now, I am going to show you how to use Filters that are a little bit more flexible. Drupal calls from Contextual Filters because they vary based on what's currently showing on the site. That is, on their context. As usual, it becomes clear when you see it in action. So let's get started. We are going to implement a filter similar to this Department Filter, but instead of being a simple pop-up list, it will be passed through the URL.

Adding dynamic filters to views

You already saw how to select records to display in the video, sorting and filtering data, but the tricks you saw there only take you part of the way. They let you filter based on specific criteria. In our example, they only show records of employees. Now, I am going to show you how to use Filters that are a little bit more flexible. Drupal calls from Contextual Filters because they vary based on what's currently showing on the site. That is, on their context. As usual, it becomes clear when you see it in action. So let's get started. We are going to implement a filter similar to this Department Filter, but instead of being a simple pop-up list, it will be passed through the URL.

To get started, we will go to our Views; Structure>Views. The one we are going to use is the view we were just looking at, Employee list. I am going to do a lot to this view, and then I'm going to delete it at the end of the video. So first, I'll clone it. That way we can do whatever we want and we can be sure that we still have our original copy. I will keep the view name as Clone of Employee list and continue. Before going on, I want to make one small change. I want to edit the page, so that the path to that page instead of being employee-list, that's the same as the other one, it's simply test.

That way, we can see what we're doing a lot better, and then I will save. So that we can keep an eye on it, I'll open up that page in a new tab. There it is! And as you can see, it's simply /test. The area that we're going to be playing with is this Contextual Filters over here which is in this Advanced Group. Now by default, Drupal shows this as collapsed like that. If that's the case on your computer, then it's easy enough to show, just click this link here, and you'll see it. In the video Changing views administrative options, I showed how to change a setting so it's always showing.

To go forward, I am going to add a Contextual Filter. The one we'll add will be Department. That matches the filter you've already seen. We add it and I'll just accept the defaults. However, I will make it only affect this display. So I will override and apply. Now, if I scroll to the bottom, I see one change. There's this preview with contextual filters, but the rest of the preview is exactly the same. The way this works is if I type in the name of the department into that field, I will able to see only people who work in that department.

So let's try, for example, Back office and Update preview and there they are; only the people who work in that department. If we save this and go back to the View itself, we reload the page, and we actually don't see anything different. What changes is what you type in the URL. If I were to say field here, for example, we see only those people who work in the field. That's great as it goes, but it requires users to manually type something into the URL and furthermore, they have to know exactly what to type.

Fortunately, we can change the settings on this Contextual View to make it more user-friendly. But first, we're going to have to wipe out some other things we did earlier in the course, because they get in the way of our Contextual View filtering. We will go and edit the view. I am going to remove some of our other filter criteria. First, I'll go in here and override, so I don't change any of the other displays and apply. Now, I'll go in. I'll remove the exposed department filtering. Make sure it's overriding and remove.

I'll also remove the others just for simplicity sake. We can also remove some sort criteria again to simplify things, but again play around. I found that sometimes when something isn't working, it's because the View is too complicated and the more you simplify it, the better you are off. Try just subtracting a little bit and then a little bit more until you get what you need. In this case again I am just going to wipe out a few things and make this very simple. Now, we are ready to go. We go back up to our department contextual filters and I am going to change a setting here.

Before, as you saw, when there was nothing after our /test, that is, if we hadn't added a filter into the URL, we saw all of the records. This time, I am going to show a summary of the records. I click there and apply it. If we scroll down, we can see what that does. Ah! We see exactly how many people are in the back office and the field and so forth. I will save this and show you what it looks like on our page. We go back up to our URL and remove field which you might remember is what was in there before. There it is! Now, if we want to see everybody who's in the front office, we simply click this link, and there they are.

You will notice that after we click that link, we see the results up here in the URL. We clicked Front office and there it is. There's one curious thing about this kind of Contextual Filter. You will notice that the summary disappears whenever you click one of the links. That's because it's only there when nothing is selected. We can improve its interface a lot by using Contextual Filters in combination with attachments which you learned about in the video Creating multipart views using attachments. In that case, you could keep that directory appear even while it showed results underneath.

But our group of employees is small enough that I don't really think it's necessary to go forward. I am just going to get rid of this Contextual Filter and return the site to the way it was. I go up, edit the view, go back to my Views, and delete my clone, and delete. Now, if we go back to our front page, you will remember it's still our original employee list. It should be apparent that we really only dipped our toes into what Contextual Filters can do. But from the little you saw here, I am sure you can understand how they can really make record selection dynamic, especially on sites that have a lot of data.

Show transcript

This video is part of

Image for Drupal 7: Reporting and Visualizing Data
Drupal 7: Reporting and Visualizing Data

44 video lessons · 6765 viewers

Tom Geller
Author

 
Expand all | Collapse all
  1. 13m 50s
    1. Welcome
      50s
    2. What you need to know
      4m 49s
    3. Using the exercise files
      8m 11s
  2. 12m 58s
    1. A yoga studio
      3m 18s
    2. A national organization
      2m 51s
    3. An entertainment company
      4m 3s
    4. An organization with hyperlocal branches
      2m 46s
  3. 40m 49s
    1. Planning your site's data structure
      3m 31s
    2. Creating record templates
      5m 40s
    3. Understanding Drupal 7's data-template model (entities)
      4m 27s
    4. Creating categories using two methods
      6m 14s
    5. Creating unusual field types
      6m 38s
    6. Reusing and grouping fields
      6m 25s
    7. Looking at Drupal's database
      3m 56s
    8. Migrating custom fields from Drupal 6
      3m 58s
  4. 20m 46s
    1. Connecting two fields using references
      5m 35s
    2. Importing data using Feeds
      8m 48s
    3. Changing the appearances of fields
      6m 23s
  5. 53m 15s
    1. Understanding why views are useful
      5m 1s
    2. Learning from built-in views
      5m 52s
    3. Creating and deleting a simple view
      5m 41s
    4. Diving into the Views interface
      8m 48s
    5. Surveying field types
      6m 16s
    6. Adjusting the settings of individual fields
      5m 31s
    7. Sorting and filtering data
      8m 11s
    8. Changing Views' administrative options
      7m 55s
  6. 33m 33s
    1. Varying how a view appears in different displays
      10m 10s
    2. Formatting views with grid, list, table, and jump menu appearances
      6m 43s
    3. Rewriting field output for interesting results
      4m 21s
    4. Creating multipart views using attachments
      6m 8s
    5. Styling the output of views
      6m 11s
  7. 33m 56s
    1. Importing and exporting views
      6m 21s
    2. Controlling access to data
      6m 44s
    3. Adding dynamic filters to views
      5m 41s
    4. Extending views with relationships
      6m 24s
    5. Improving data administration with Views Bulk Operations
      8m 46s
  8. 29m 43s
    1. Understanding locational data
      4m 43s
    2. Setting up the Location module
      9m 42s
    3. Displaying basic maps with the GMap module
      8m 37s
    4. Displaying custom maps
      6m 41s
  9. 23m 50s
    1. Exporting data in simple text form
      7m 4s
    2. Adding dates and calendars
      8m 32s
    3. Displaying content in a slideshow
      8m 14s
  10. 1m 20s
    1. Next steps
      1m 20s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Drupal 7: Reporting and Visualizing Data.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.