Tech Blog :: Drupal hack: Working around Ubercart's disconnected catalog/taxonomy paths


Jun 21 '10 10:31am

Drupal hack: Working around Ubercart's disconnected catalog/taxonomy paths

One of my current projects involves an inherited Ubercart (Drupal ecommerce) store. It's set up with the standard Catalog module and products are broken down by content type and taxonomy. Unfortunately, Ubercart's catalog paths (mapped in this case to catalog/[category]) overlap/conflict with the taxonomy/term/% paths for the product categories. They have to be themed separately, and menu links point to one or the other, so effectively half the product-listing pages on the site are orphaned and/or unwanted.

I posed this question at an Ubercart session at Design4Drupal Boston this weekend, and the proposed solution was to disable the UC Catalog module. That's probably a good idea if I'm building it from scratch, but on this project I'd like to avoid restructuring the whole legacy site if possible. So I came up with this rather messy (but sufficiently functional) hack, to redirect one group to the other for consistency (comments in the code). You'll want to change 'store' to whatever path your Pathauto is using:

function MYMODULE_store_init() {
  // stopgap fix for disconnected taxonomy/term/% and catalog/% paths
  // (redirect first to second)
  // @TODO figure out a better solution
  $menu_trail = menu_get_active_trail();   // current page's breadcrumb trail
  $menu_item = is_array($menu_trail) ? array_pop($menu_trail) : array();   // current page's menu item
  $url_parts = explode('/', drupal_get_path_alias($_GET['q']));    // URL as seen by user
  // dsm($menu_item);    // debugging with Devel
  // dsm($url_parts);
 
  if (isset($menu_item['path']) && $menu_item['path'] === 'taxonomy/term/%'   // is a term page
    && is_numeric($menu_item['page_arguments'][0])    // has a term #
    && $url_parts[0]==='store'    // where Pathauto puts the store, dif for each site
    && count($url_parts)===2) {     // a store category, not a product page or other sub-page
 
      if ($term = taxonomy_get_term($menu_item['page_arguments'][0])) {    // is a valid term
        // dsm($term);
        // redirect to catalog category page of same term id
        drupal_goto('catalog/' . $menu_item['page_arguments'][0]);
      }
  }
}

I need to test this some more, but for a stopgap solution that avoids restructuring an existing site, it seems to work.
I'd be interested in hearing if people have thoughts on this method, or what would be a better way.

I had a similar problem a few days ago. I wanted to alter the path that the ubercart catalog module used; altering it with pathauto meant I couldn't then set an active path with the context module. You could just change the name of the taxonomy you use for Ubercart products - that name will then be used as the new path ... but in the end I chose to just use a view in place of the catalog module.

Dear Mr Buckman,

I am having exactly the same "overlapping links" problem. Your solution seems to be just what I need.

However, being pretty new to Drupal I need some basic informatin in implementing it.
Where exactly do I have to place that code? How is this function to be called?
I'd highly appreciate if you could give more detalied instructions.

Place the code in a custom module, and rename MYMODULE in the snippet with the name of your module.
Module development guide is here: http://drupal.org/developing/modules

Ben

I just ran into the same problem, and discovered the Taxonomy Redirect module. It appears to do what you're doing there.

I think there is a module called path redirect which may also do what you're trying to do here although it's fair to say I've never used it, but just in this help anyone look in the right direction I thought I'd drop a note on this post about it.