Improving Drupal's support for the rel-tag Microformat

I recently installed the Operator extension for Firefox. Operator is an interesting little extension which attempts to expose microformats embedded in web pages through a toolbar in the browser.

While browsing this site with Operator installed I noticed a problem with the tags that are typically attached to each post to organize the site into categories. Drupal attempts to conform to the rel-tag microformat by attaching a rel="tag" attribute to each tag link (Look at the section titled Tags: at the top of this post.) The problem is that drupal uses numbers for each category (aka "Taxonomy Term.") and numbers don't make very readable tags. As several others have already pointed out, this could be considered a deficiency in the microformat. The rel-tag specification doesn't provide any alternate way to specify a text-version of the tag, other than the last segment of the url. In the case of drupal, the last segment of a taxonomy url is the taxonomy term id, which is a number.

Fortunately, this is one case where the flexibility of Drupal's API comes to the rescue. I did a quick search of Drupal.org and turned up this post. The solution offered was not up to date with Drupal 5.x and it didn't work for my version, however, that article mentioned a new API hook in Drupal HEAD called hook_link_alter, a feature that eventually made it's way into the final release for Drupal 5. Using hook_link_alter I was able to hack together a short function that rewrites the tag links so that they end with the tag name instead of a number. Fortunately for us, Drupal's taxonomy system ignores the extra baggage on the end of taxonomy URLs and everything still works as expected.

All of this explanation has quickly become much longer than the code that I am describing. Without further delay, here is some code that you can place in your drupal theme's template.php to make more sensible rel-tag taxonomy links:


/* Alter rel-tag links to include the link tag name in the url */
function blog_link_alter(&$node, &$links) {
        foreach ($links AS $module => $link) {
                if ($link['attributes']['rel'] == 'tag' && !empty($link['title'])) {
                        $links[$module]['href'] .= "/".$link['title'];
                }
        }
}

Warning: This is slightly hackish because I am placing a drupal api hook inside a theme template.php - this hook is prefixed with the blog module name and it actually belongs inside the blog module, however, I don't want to alter drupal core code so I chose to place this in my theme.

Technorati tags: ,
Related Project:
Drupal

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Could it be that this just works on "home"

Getting more into microformats, it was getting wrinckles on my forehead, as I realized that the operator-extension shows no single microformat (esp. tags) for my own drupal-system with a huge tag-cloud. So searched around the web and found your solution.... so i modified the template.php. Now, tags are recognized - BUT: On "home" the whole names of the tags are shown, but on the according node-sites (e.g. admins blog), operator still just finds numbers :( Do already know the problem? I think it has something to do with the placement in the theme !? best regards, martin

Indeed you are correct, this

Indeed you are correct, this only works on the default home page listing. I don't know the solution to this, however, I will post an update here if I come up with the solution.

update....

I just tried this with the latest version of Firefox (3.0 Alpha 8) and the latest version of Operator. It seems to work correctly now, though I might be missing something.