Drupal – Override the Menu Link in Taxonomy Menu

The Taxonomy Menu allows you listing all taxonomies in a menu. You can either link the taxonomy by term id or term name. I have Pathauto installed such that each term is linked to a URL alias.

 

Unfortunately, you cannot replace the special characters in the term name. The following is a list of terms and their corresponding links.

  • Apple & Pearproducts/apple-%26-pear
  • l’orangeproducts/l’orange
  • Apple (Green)products/apple-(green)
  • Lemon/Grapefruitproducts/lemon/grapefruit

 

The above links are all not valid links. The correct URL alias created by Pathauto should be.

  • Apple & Pearproducts/apple-pear
  • l’orangeproducts/lorange
  • Apple (Green)products/apple-green
  • Lemon/Grapefruitproducts/lemongrapefruit

 

You can either change the Pathauto setting but this does not work anyway for a term name with ‘/’. An alternative is to replace the special characters when generating the menu link.

Edit the template.php in your theme folder and add the following theme function.

/**
 * This function replace the special characters in the taxonomy when generating the taxonomy menu with term name.
 */
// Remember renaming the function to <theme_name>_menu_item_link
function phptemplate_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  
  // replace special characters
  $href = str_replace('&-' ,'', $link['href']);
  $href = str_replace('(' ,'', $href);
  $href = str_replace(')' ,'', $href);
  $href = str_replace("'" ,'', $href);
  
  // replace the 2nd occurrence of '/'
  $href_head = substr($href, 0,  strpos($href, '/')+1);
  $href_tail = substr($href, strpos($href, '/')+1);
  $href_tail = str_replace('/' ,'', $href_tail);
  $href = $href_head.$href_tail;
  
  return l($link['title'], $href, $link['localized_options']);
}

 

This should solve the problem.

Done =)

Reference: Drupal APi – theme_menu_item_link

About these ads

2 thoughts on “Drupal – Override the Menu Link in Taxonomy Menu

  1. Brian Lewis

    I tried this with my theme name, and nothing. I am on Drupal 7.

    function _menu_item_link($link) {
      if (empty($link['localized_options'])) {
        $link['localized_options'] = array();
      }
        
      $href = "hackcheck";
       
      return l($link['title'], $href, $link['localized_options']);
    }
    

    Like

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s