Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101523 Viewers
61 Video lessons · 88281 Viewers
71 Video lessons · 72155 Viewers
56 Video lessons · 103916 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.
Your file was successfully uploaded.