Making Drupal Taxonomy Links Happy with rel-tag specification

joshk - May 24, 2006 - 18:33

I've recently been fighting to get technorati to pick up tags from my drupal site. I discovered that we're still not really confroming to the spec for the rel-tag microformat:

http://microformats.org/wiki/rel-tag

Becasue the end of all taxonomy urls is a numeric ID, the tags are read by spec-mindful web services as numbers. This can be solved on a tag-by-tab basis with path_aliases, but I need an automagic solution.

With a little investigation, it became clear that additional arguments on the url will be ignored by taxonomy.module by default, so I made an override for theme_links() that tacks on a rel-tag-friendly addition to the url.

This only works with clean_urls enabled, but if you don't have those on, you're screwed in any case:

function phptemplate_links($links, $delimiter = ' | ') {
  if (!is_array($links)) {
    return '';
  }
  if (variable_get('clean_url', FALSE)) {
    // this only works for clean urls
    $newlinks = array();
    foreach($links as $link) {
      if (strstr($link, 'rel="tag"')) {
        $result = preg_match('!\>([\w\s]*)\<!', $link, $tag);
        $link = preg_replace('!href="([\w\/]*)"!', 'href="${1}/'.drupal_urlencode($tag[1]).'"', $link);
      }
      $newlinks[] = $link;
    }
    return implode($delimiter, $newlinks);
  }
  else {
    return implode($delimiter, $links);
  }
}

HEAD has the new and shiny hook_link_alter, which will provide a much "cleaner" way to do this, but for now I think this works. Please note this is experimental. There may be problems that I have not yet begun to encounter. However, it should help some people.

Argh

Boris Mann - May 24, 2006 - 23:10

I would argue that Technorati is bogus, as I've said many times to Tantek, who insists that he is promoting "good URL design".

In any case, check out my Technorati recipe -- Technorati reads RSS 2.0 categories directly from Drupal, and yes, you do need pathauto to create the proper paths for it to work correctly.

hook_link_alter

20after4 - July 18, 2007 - 20:39

I just implemented your solution in 5.0 by using the hook_link_alter function. It's mostly a clean solution, however, I'm doing it in a theme template.php so it's not really the correct place to put a hook, afaik. Is there a cleaner way to do this?

At any rate, here is my code, it might prove useful to someone:

function blog_link_alter(&$node, &$links) {
        foreach ($links AS $module => $link) {
                if ($link['attributes']['rel'] == 'tag') {
                        $links[$module]['href'] .= "/".$link['title'];
                }
        }
}

Note: I used blog_link_alter because I needed to name the hook after some module. This might be due to my lack of understanding drupal's API, however, I don't know a better way to do it and this worked for me.

Related blog entry

20after4 - July 29, 2007 - 09:20

I blogged about this topic at http://www.developerfriendly.com/node/22

 
 

Drupal is a registered trademark of Dries Buytaert.