S-Files: Adding Basic Search Fields and Filters to Views
by Brenda Boggs Boggs
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.