Quantcast
Channel: Eureka! » URL Alias
Viewing all articles
Browse latest Browse all 4

Drupal – Override the Menu Link in Taxonomy Menu

$
0
0

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


Filed under: CMS Tagged: Drupal, Drupal Development, Pathauto, Taxonomy, Taxonomy Menu, URL Alias

Viewing all articles
Browse latest Browse all 4

Trending Articles