Main menu

Drupal ships with a lot of great things. One of these is Drupal’s taxonomy system. It allows us to categorise content and build listings automatically. Great for keeping track of our content, and the search engines love it too.

Still, it is not that easy to have the taxonomy listing be exactly how we want them to. Not a shame, we can take control over them quite easily.

Taxonomy listing?

Whenever we click on tags Drupal brings us to these listings. Out of the box, they have the url taxonomy/term/9 (taxonomy/term/tid).

The problem with default taxonomy listings in Drupal

Basically, we have three problems with the way Drupal outputs the taxonomy listings:

  • It displays all nodes of all content types tagged with the term. We want to control which content types to display on the listings.
  • Each content type is displayed in the same way in the listing. We want to change the display based on content type. For instance, display articles with the title and tags; stories on the other hand should contain the title and teaser.
  • Each term listing has the same layout. We want to have different layouts for different terms.

Grabbing control

The solution for our issues comes in four parts.

First, we need to take control over the taxonomy pages. Ctools’ Page Manager does exactly that. It allows us to specify what should be displayed on the Drupal paths like node/% and taxonomy/term/%.

Now we have control over the taxonomy pages (ehm, the menu callbacks), we want to do something with it. Meet the Panels module. This module allows us the make crazy layouts. So instead of the normal Drupal taxonomy listing, Page Manager will display a panel instead.

The great thing with Page Manager is we not only control the general taxonomy pages, but we can be very specific to state which panel handles which taxonomy term. So we now can have different layouts for different taxonomy terms.

Panels itself needs content to display. Because we want to list the nodes that are tagged, we are going to build a view for that and feed the view back to the panel. Because it’s a view, we can do all nice things like determining the fields and adding filters. We can filter the view on node type, which solves our first problem. So we need the Views module.

Finally, having control over the views fields is great, but it’s not such a great solution to control the way each node type is displayed. This is because the field settings are determined for all content in the view. So if we want to display the body field for articles, but we don’t want to do that for stories, we’re stuck.

Not too bad, let’s just write create a custom “build mode”. This “build module” will allow us to specify the fields on the content type visibility settings. We just set the body field visible for article, but hide it for the stories. Problem solved!

Let’s build this thing!

I want to make the taxonomy pages in Drupal 7 prettier. So I’m using Views 7.x-3.0-beta3, ctools 7.x-1.0-alpha4 and panels 7.x-3.0-alpha3. I see some errors flying around, but the thing is not exploding!

Starting with the custom “build mode”

Writing a custom “build mode” in Drupal 7 is easy. The hook hook_entity_info_alter allows us to specify new build modes.


/**
 * Implements hook_entity_info_alter().
 * 
 * We want to add a custom build mode to display nodes in taxonomy listings
 * differently.
 * Using build modes we can determine which fields will show up on the taxonomy
 * listings on a per node basis.
 *
 * Reference:
 *   http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_entity_info_alter/7
 *   http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_entity_info/7
 */
function gluecode_entity_info_alter(&$entity_info){
  $entity_info['node']['view modes']['taxonomy_listing'] = array(
    'label' => t('Taxonomy listing'),
    'custom settings' => TRUE,
  ); 
}

The screenshot below shows the content type display settings before we enabled our module with the hook.

The one below shows a tab ‘Taxonomy listing’ which is the new “build mode” we just specified. We will find this new tab on each content type. By dragging fields to the hidden area, we make sure the fields won’t be displayed (for this build mode).

As the screenshot above shows, we not only determine which fields to display, but also if the label should be visible and which format to use.

Since we the build mode is set per content type, we now have a means to display articles differently than posts within the same view.

Creating the view

Go to admin/structure/view to create the view. Just enter the name like “Taxonomy listing” and leave all other settings untouched. We will change some of them in the next step. So go ahead and click ‘Continue & edit’.

This will bring us to the screen below.

Now we need to change some settings. For one, change the teaser in “Show: Content | Teaser” to ‘Taxonomy listing” by clicking on it. This opens a window where we change “View mode” to “Taxonomy listing” as shown below.

Next, we want to filter the content of the view on taxonomy terms. So go to the “Advanced” section” and add a “Contextual filter”. Select “Taxonomy: Term ID” and hit “Add and configure contextual filters” as shown below.

Before we hit apply we need to check “Provide default value” and select “Taxonomy Term ID from URL”.

Now we have our view set up as we want it. We could save it as it is. But we can also make sure no unwanted content types show up on the taxonomy listings. We control this by adding a filter on content type as shown below.

Of course, we could add any filters we want.

Just save the view because we’re really done.

Last stop: Page Manager and Panels

Go to admin/structure/pages and enable the “Taxonomy term template”. It now controls the pages we see when clicking on taxonomy terms.

Now we need to instruct the Page Manager what to do when somebody clicks a taxonomy link or hit those taxonomy/term/tid url’s. So click "configure" and add a “new variant”.

We want to use Panels to control the page so go ahead, select it and click ”create variant”.

Select a layout.

Give it a name and hit ”update and save”.

I also checked “Disable Drupal blocks/regions” so the panel takes over all the regions in my theme. It basically gives it free reign to put blocks wherever it wants to and dumps the other regions such as the sidebars. We don’t really need this. It’s just personal taste.

Now we can add the content.

Set the title to “%taxonomy_term:name” which is a placeholder for the taxonomy term name as shown below.

We find three columns in the user interface (or some other composition depending on the layout we selected). In each of these columns we can add content (this is just normal panels stuff).

What is important to us is to add the view we just created as content in the panel. So we click the little gear icon and select “add content”. This will pop up the window below.

Under the tab “Views” we can select out view. So let’s do that.

Once selected it will ask us which display we want to use. We only created one so it doesn’t matter, but in other cases you could specify the exact view display.

Note, Master is just the default view (in our case it’s the same as the page display we created).

We don’t need to add a context since the view will use the term id in the url to determine which nodes to show). So we can just click ‘finish’ and have everything set up.

We can add other content in the columns like taxonomy description or blocks, nodes and such. I added some content in the central column and an advanced search box in the right. It’s to your liking what content you want to display on these taxonomy listings.

Just hit save and we have now tamed the taxonomy to display its content to our liking!

Go click on a taxonomy term in the front-end to view the listing we just created. Isn’t she a beauty?!

Some afterthoughts

What about having different layouts for different terms?

Well, we can add a “new variant” and set some rules that will trigger that variant for that specific term. We should also set a rule to our default variant so that it won’t show up for that specific term.

What about Drupal 6?

Things will work just the same in Drupal 6 except for our code to create a “build mode”. We find the code for Drupal 6 listed below.

/**
 * Implements hook_content_build_modes().
 *
 * We want to add a new content build mode which will
 * be used on the taxonomy overview pages.
 * 
 * Per content type, we want to say which fields should be
 * included on the taxonomy pages.
 *
 * References:
 *   http://data.agaric.com/raw/adding-node-build-mode
 *   http://api.lullabot.com/simplenews_content_build_modes
 */
function gluecode_content_build_modes() {
  return array(
    // Use name module to avoid namespace conflicts.
    // If we put 'basic' we will override the node module's settings
    // so full node and teaser will disappear.
    'gluecode' => array(
      // This is the tab that will be displayed on the content type display field settings.
      // It will be cached so clear caches if you change the title.
      'title' => t('Taxonomy listing'),
      'build modes' => array(
        'taxonomy_listing' => array(
          // This will show up in views
          // and it will be the title in the content type display fiels settings table for Taxonomy listing in view.
          'title' => t('Taxonomy listing'),
          // We want to be able to select it in views.
          'views style' => TRUE,
        ),
      ),
    ),
  );
}

12 comments

Default avatar
Lars Olesen
Thu, 06/10/2011 - 14:00

1) First, why do you create a custom build mode? Is it necessary? 2) Second, it seems that my panel is not giving the parameters to my my view. Could it be because of path auto and that my taxonomy page is found on http://vih.dk/fag/pakke/boldspil and it does not know exactly how to translate that? 3) Third, %taxonomy_term:name is only %term:name in my version of panels.
Default avatar
Anonymous
Fri, 21/10/2011 - 00:05

Great tutorial, but I see duplicates in my taxonomy term pages! "Reduce duplicates" in contextual filter option has no effect. Cache is always clean. Any hint of what could cause this behaviour? Thanks
Default avatar
Anonymous
Fri, 21/10/2011 - 04:15

I figured it out: I choose Taxonomy: Term ID (with depth), which lets you choose whether to allow multiple choices or not. To be honest, I'm way far from the very deep understanding of the views inner mechanisms, but at least it works :) There's always time for learning, right?
Default avatar
dready
Fri, 13/01/2012 - 10:10

Great, easy to follow and exactly what i was looking for! Thanks! Just one remark: In my case, when i wanted to add the contextual filter in the view, taxonomy: TermID was not showing up - actually, no taxonomy fields were available. So i had to add a relationship to Taxonomy terms on node, and after that i could follow your tutorial step by step. Maybe you could add that as a remark or extra step. Thanks again!
Default avatar
Juicy Couture outlet
Mon, 19/03/2012 - 07:13

The content of this field is kept private and will not be shown publicly.
Default avatar
Ali
Tue, 24/04/2012 - 09:25

The taxonomy term template isn't enabling for me. It says "Page manager module is unable to enable taxonomy/term/%taxonomy_term because some other module already has overridden with views_page." What should I do!? :-(
Default avatar
sclapp
Thu, 17/05/2012 - 03:34

This usually happens when the taxonomy/term/ View is enabled. You want to disable that "view" that's currently providing your default taxonomy term listing (go into the views ui & find the one with the path /taxonomy/term/%taxnomy_term & turn it off). Then you can enable the Panels Page for taxonomy term (& yes, you'll be using views within it, but that is a little different than having the whole page assembled exclusively by Views w/o panels' help. Does that make sense?
Default avatar
Ricki-Ellen
Wed, 25/04/2012 - 00:20

it's nice that we can have different displays per content type, but it would be good to be able to actually display the content type itself, too... doable?
Default avatar
SunRhythms
Sat, 22/09/2012 - 02:17

Can you do something similar with Profile2. Im having difficulty creating views for specific profiles.
Default avatar
Wewsnalse
Wed, 23/01/2013 - 13:17

Youre so cool! I dont suppose Ive read anything like this ahead of. So nice to find somebody with some original thoughts on this topic. realy thank you for beginning this up. this site is some thing that is required on the internet, someone with a small originality. valuable job for bringing something new to the net! <a href=http://www.freerunning3.com/>Nike free sko Gratis Fragt</a> <a href=http://www.freerunning3.com/nike-free-haven-3-0/>Nike Free Haven 3.0 for mænd</a>
Default avatar
LonehotH
Wed, 23/01/2013 - 13:24

you will like <a href="http://www.burberryoutlet-online2013.org/ ">burberry store</a> nwGrFZIL [URL=http://www.burberryoutlet-online2013.org/ - burberry online store[/URL - online shopping lcgQZlwj <a href="http://www.burberryoutlet-online2013.org/ "> http://www.burberryoutlet-online2013.org/ </a>
Default avatar
Semennfeq
Tue, 07/05/2013 - 20:08

We hebben op de bovenstaande web blog stelde een grote selectie van unieke nieuws over <a href=http://mig5.ru/>новинки игр новости</a>.