Adding Meta Tags to taxonomy_term override view with Pathauto, Nodewords and Views

Submitted by Janak on Mon, 08/03/2009 - 13:11

If you are using the wonderful Views module to override the default taxonomy_term pages, you will have noticed that it no longer displays the meta data defined for the term. This is due to the way nodewords handles Views and Taxonomy. It has no way of knowing that we are overriding the taxonomy term page so it can display the term data correctly.

To help and illustrate the problem, imagine your setup is as follows:

Taxonomy Setup

Vocab 1 "Book Category"
-- Term 1 "Fiction"
-- Term 2 "Crime"
-- Term 3 "Horror"

Each term has been setup with term mets description and keywords.

Pathauto Setup

taxonomy/term/tid is aliased as "books/[cat-raw]"
IE:
-- books/fiction
-- books/crime
-- books/horror

Views Setup

Imagine you enabled the default taxonomy_term override view and configured it for teaser view
ie: taxonomy/term/%

Now, every time you visited your /books/crime view, you expect the meta data to be displayed, afterall you defined it for the term....ermmm. NO

Drupal and Nodewords module will associate identify /books/crime as a view and will try to display the meta data you assigned for the taxonomy/term/% view.. There is an additional issue with handling wildcard documented here..

If you are running the older version (CVS ver DRUPAL-6--1-0), the only way to get around this problem and get the meta data working again is to add your own hook_nodewords like this:

function mymodule_nodewords(&$tags, $op, $type, $ids){
  if(($op == 'prepare') && ($type == 'views')){
    $args = explode('/', $_GET['q']);
    if($args[0] == 'taxonomy' and $args[1] == 'term' and is_numeric($args[2])){ // we have a valid url
      $term_obj = taxonomy_get_term($args[2]);    
      if($term_obj){ // do we have a term obj
        $term_meta = _nodewords_load('term', $term_obj->tid);      
        if($term_meta){ // did we get any meta data for term?
          $tags['description'] = $term_meta['description'];
          $tags['keywords'] = $term_meta['keywords'];        
        }        
      }
    }   
  }
}

UPDATE for CVS ver DRUPAL-6--1 of nodewords module:

The nodewords module (CVS ver DRUPAL-6--1) has been re-written slightly and the above method is outdated. It now uses hook_views_pre_render function to add the meta data to the page. We can hijack this function slightly and inject our own meta data (Drupal 6) like this:

function mymodule_views_pre_render(&$view) {
  // get the right view, make sure the name matches you view name
  if($view->name == 'taxonomy_term'){  
    foreach ($view->display as $display) {
      // We're only interested in main page views if it has arguments.      
      if (drupal_substr($display->id, 0, 5)  == 'page' and !empty($view->args)) {
        $term_obj = taxonomy_get_term($view->args[0]);    
        if($term_obj){ // do we have a term obj
          drupal_set_html_head(nodewords_output_tags(nodewords_get(array('term', array($term_obj->tid))))); 
        }
      }
    }
  }
}

If you have found a better solution or found this workaround useful, please leave me a comment.

UPDATE

If you are using the Nodewords 6.x-1.11 or newer, there is now a dedicated API and the above solution will not work. Please read this tutorial to use the new hook_nodewords_tags_alter() method:
http://janaksingh.com/blog/drupal-6-define-seo-meta-tags-dynamically-us…