URL's, pathauto and passing variables

<< Go back | Posted: Monday, August 25 2008 - 01:21

Tags: drupal, paths

I am currently building a rather large site with lots of single hierarchy taxonomy levels. Each page will basically look the same, but the blocks and content on each page will only correspond to that page's taxonomy level; meaning the content will be filtered down to that level. Not too hard so far huh? Now on top of that, there is a block containing custom filters that will be applied to the blocks, on top of the taxonomy that is.

The problem I had was passing variables in URL's so I could tell the next page to filter just more than the taxonomy. I didn't want to use sessions, as I want it all to reflect in the URL. (By the way, I am using pathauto, and of course views for the most part of the blocks).

As the URL's for pathauto are directly referenced in the database, you cannot add to them, as it gives a 404. I could have used a ? at the end of the friendly URL, but that would make it not so friendly and quite frankly, ugly. I mean, imagine this:
domain.com/category1/category2/category3?filter1
Yuk.

Custom URL rewrite to the rescue. I am still building on this function at the moment to automatically generate the taxonomy levels and ignoring the filter segments, but here is a simple example, how to change taxonomy/term to category in the URL path and retain the term ID after it:

function custom_url_rewrite($op, $result, $path) {
  global $user;
  if ($op == 'alias') {
    if (preg_match('|^taxonomy/term/(.*)|', $path, $matches)) {
      return 'category/'. $matches[1];
    }
  }
  if ($op == 'source') {
    if (preg_match('|^category/(.*)|', $path, $matches)) {
      return 'taxonomy/term/'. $matches[1];
    }
  }
  return $result;
}

So pathauto will now be disabled for any types that I want to manipulate through this function.


click here to add a comment