Front-end engineer with a passion for learning something new every day

Custom Taxonomy Pages with Drupal Views Using Selective Overrides

Drupal 7 has an option to turn on a default View for Taxonomy term pages via the contrib module, Views. This is generally pretty good but if you want highly designed pages with additional custom fields than what the default view renders, you could simply update and customize this view but there's a few drawbacks:

  • You'll be overriding all Taxonomy Pages so it's not very granular.
  • Using the default Taxonomy View won't be too exportable in regard to things like Features.

Creating Taxonomy Theming Granularity

Enter the Taxonomy Views Integrator module aka 'TVI'. This is one of those nice little gems that you don't hear about too often but it's pretty powerful and allows for that granularity that we are looking for here. It enables you to customize only specified Taxonomies with a new custom Views override per Vocabulary. The added benefit is that you can keep the original Taxonomy path aliases. Other methods suggest hackish ways to override Taxonomy term pages by creating an alternate term path alias structure for the overrides but this gets pretty messy.

First Steps

It's probably a good idea to have a custom Vocabulary set up beforehand but theoretically you could use the default Tags Vocabulary that comes with the Article Content Type. You'll need the core Taxonomy module enabled but that's already by default with a typical Drupal 7 install. You'll also need the contrib module, Views as well. Next, download and enable TVI. TVI does not do much on its own, you need a view from which to reference for a specific Taxonomy to make this all work. I found the best method is to simply clone the default taxonomy_term view located at /admin/structure/views and customize it but you may need to 'enable' it first. When you clone this view, be sure to name the newly cloned view something meaningful, you can also edit the machine name at this point as well. Ideally, you are doing all this work on a dev or local site, not a live one.

Cloning the Default Taxonomy Term View

Mind Your Path

Now that you have your new custom Taxonomy Term View, we need to make a few changes to some of the default settings that came with it. For the default 'Page Display', change the Path to something like nopath/%. It really doesn't matter what you put here as long as it's not the default term path which is taxonomy/term/% or any other real path in your site. The reason we do this is we don't want to intercept the original path of the Taxonomies' Alias.

Refining Arguments

The other change we want to do is to alter one the Contextual filters aka Views Arguments. Look under the View's advanced settings for Configure contextual filter: Content: Has taxonomy term ID (with depth). In my case I'm customizing a Vocabulary called "Image Category", so for the setting, Specify validation criteria, I check the box for that given Vocabulary.

Adding Additional Fields and Customizing

You can pretty much go to town with adding additional fields but you may need to add Views Relationships depending on what you are doing, just be aware of that, your milage may vary. Finally save your View. Note that because there's a Contextual Filter in play, you won't see a preview of your View right off the bat so you can use "Preview with contextual filters" and fill in an id. For example, I have a Taxonomy term id of 273 so I can input that into the text box and see a preview after updating. This comes in handy as otherwise you are a bit in the dark during configuration.

Connect the Dots

A finished Taxonomy page using a custom override with TVI All we've done so far is focus in on our custom Taxonomy Term Page View. However there's still a final step to put it all together. It's time to edit the Taxonomy you want to customize. Go to /admin/structure/taxonomy /[YOUR_VOCABLUARY]/edit. There's a new area called View usage, Check the box called Use view override. You'll see two new select lists appear which are self-evident, Using the view and View display. You can now select the View and display you customized from the cloned view as mentioned above. If all went well, now when you visit your custom Vocabulary term pages, presto, you should see your customized View in effect! One major note is that for any other Taxonomies you don't want customized, you'll simply need to set each to the default Taxonomy term View on the Vocabulary's edit page if you still want the defaults Views intercept to happen.

The possibilities are endless here and it will give your selectively overridden Taxonomy pages a unique look that can be highly designed, it gets away from boring lists of things that are typical of these type of pages.



Read other blog posts