Home / Comment permalink

S-Files: Adding Basic Search Fields and Filters to Views

Tech Support Case Studies

Being that Views is both a very popular module and included in Acquia Drupal, we get a lot of questions regarding how to configure and customize views in various ways. One thing that has come up a few times is a customer wanting to add custom searching or filters to their views.

While this is a core feature of Views, it's often overlooked and can seem mysterious to those new to Drupal. I'm going to walk through adding a textfield search which will search our node's titles, as well as a drop down filter to our taxonomy term pages. This is a view that comes by default. First, navigate to Administer -> Views and Enable the taxonomy_term view.

This view will now be used for our default taxonomy pages. For example, on my site I have a vocabulary called Categories with terms Category 1 and Category 2. If I go to one of my term pages, http://example.com/taxonomy/term/1, this default view is used.

Now we're going to add these filters. Edit the taxonomy_term view. Add a new filter by clicking the plus sign under the "Filters" section.

Check the Node: Title option and click Add. You should then have an option to Expose this filter. Click that.

Leave everything as it is except change the Operator to "Contains" and uncheck the "Case sensitive" option and click Update. To see what we've just done, we can preview this page using the "Live preview" option of Views. Just put a 1 in the "Arguments" textfield and hit Preview. The argument, in this case, is our term ID.

Now we have a textfield that will filter based on node title. Hurray! If this is all you need, make sure to remember to save your view. We're going to add a select option now, though. Continue on.

Back to our "Filters" section, let's add another one by clicking on the plus sign again. On my site, I have a field called "Status" added to my default Page and Story content types using CCK and Optionwidgets. This should work for any kind of select field(s) you may be using.

In my case, I select "Content: Status" and click Add. I'll leave Operator as it is, but select all the options under Status. Then, once again, click Expose.

Leave the default values but check the "Limit list to selected items" option, click Update and then Save the view. That should be it.

Now let's take a look at my taxonomy term page.

As you can see, now we have both a textfield that filters by the title of the node and a drop down select to filter by Status.

You can use all sorts of different fields to add sorting and searching options to your different views. Play with the options a bit to see how you can restrict things or allow for multiple options to be selected.

Comments

Posted on by drupalist (not verified).

I didn't know this feature was out there, this is ideal. I followed your instructions and I see the search box and the drop down on my category pages. But when I actually try a search, it always comes up blank, no results, even if there are dozens of matches in the actual content being filtered.I ran cron but that didn't seem to make a difference. Do you know what I might be doing wrong? Thanks.

Posted on by drupalist (not verified).

Ah, fixed. I needed to choose "contains" rather than the default "is equal to" radio button in order to filter node titles by keyword(s).

Posted on by Brenda Boggs Boggs.

Ahh. Glad you figured it out and thanks for the note. I'll update the post accordingly.

Posted on by drupalist (not verified).

One quick follow-up. Although the keyword filter is working now, it's not really very useful in terms of accuracy because if I enter "art" for example without the quotes it displays all posts with "art" anyywhere in the word, such as "start", "Walmart" I'm not sure how to make it filter whole-words only like standard search. Any thoughts? Thanks again.

Posted on by sime (not verified).

Might I suggest to change the default colours of Garland when taking screen shots. Scanning through the light blue theme between Acquia and Garland meld together and I don't know what I'm looking at: content or an image.

Thanks for the post... just starting out with views.

Posted on by Robert Douglass.

Good suggestion. We'll discuss this internally but I see that Brenda is doing screenshots with Skitch (we _love_ Skitch), and it has a drop-shadow feature. That would fix the visual problem nicely, I think.

Robert Douglass
Senior Drupal Advisor, Acquia

Posted on by Brenda Boggs Boggs.

Thanks, Simon. I redid the screenshots. I still need a bit of practice, but they should be a lot more readable now.

Posted on by pankajshr_jpr (not verified).

Hi ,
I am making a search block using views from the procedure you have told above. In which I have added a taxonomy Vocabulary filter and the taxonomy term filter. I want that when I select a Vocabulary in the taxonomy Vocabulary filter, the terms in the taxonomy term filter dropdown list should change to the terms added inside that Vocabulary. Please tell how I could do it? Thanks in advance

Posted on by Joshua Brauer.

This would take a bit of customization. You'd need to have a module that provided some javascript (jquery probably) code that would bind to the update function of the Vocabulary field and when you changed the vocabulary would cause the term list to refresh.

Thanks,
Josh
Acquia Technical Assistance Center