Drupal: How to generate different view results, using a taxonomy keyword as an argument

The following would apply if you wanted to generate nodes according to a specific taxonomy term, but you didn't want to create a different view for each case.

For instance, we have a market place and in the preface region of our theme, we've setup a 3x3 panel, and in each of the 9 dials we want to demonstrate the latest product from a specific category.

We would create a new node type(Product) for our market products, and a taxonomy vocabulary(Product Categories), which we would then asign to the aforementioned node type. Finally, we would create a view with the following options:

"Products" View

Basic settings
Items to display -> 1
Sort Criteria
Node: Post Date -> Desc
Filters
Taxonomy: Vocabulary -> Product Categories
Node: Type -> Products
Taxonomy: Term -> X_Category(ie Electric Appliances)

Then make 8 copies of of it, and change the "Taxonomy: Term -> X_Category" Filter, to the rest of the categories, which we want to use in our dials. After that, we would link each view to a specific dial from the panel module settings.
However a panel could easily have 50 dials or more and even in our case, 9 views are a lot to have for a simple panel. Additionally, each time we'd want to change the categories at hand, we'd have to edit the filters in all of our views.
Thankfully there is a more appropriate way of achieving the above. Enter arguments.

An argument is drupal's way, of feeding parameters to a view, using the URL. For instance, we have a view called "Products", and it is linked to the path "/products". When a user goes to "oursite.com/products", he gets a dynamically generated page, according to the view "Products". But if he goes to the URL "oursite.com/products/electrical_appliances", drupal will assume, that the user is trying to give to the "Products" view, the parameter "electrical_appliances". In other words, we can use this to generate different results with the very same view, just by feeding it different parameters.

So what changes would we do to the "Products" view, so that it would use arguments? First we would get rid of the filter "Taxonomy: Term -> X_Category". Then we would add a new Argument, namely "Taxonomy: Term". The most important options would be the validator options, which are the options responsible for filtering our view's results using the parameter at hand. We would proceed by selecting from the "Validator" select list the option "Taxonomy Term", and picking from the "Vocabularies" checkboxes below our taxonomy vocabulary, in our example "Product Categories". Finally we would be setting "Argument type" to "Term name or synonym".

We are ready now to link our dials with this view, provided that we change the URL according to the category for every dial i.e. "oursite.com/products/electrical_appliances" or "oursite.com/products/cooking_equipment" etc

Tags: 
Drupal 6, Taxonomy

Recent Comments

Forward by Dries Buytaert, Founder and Project Lead, Drupal; CTO Acquia.

Order now from:

Managing Expectations Podcast with DrupalEasy

Author Interview http://tinyurl.com/8a8nxno

We provide the most Shiny Drupal Bits & Pixels

Since Drupal 4.x