Drupal: How to Enable Active Trail with Nice Menus

As you maybe know Nice Menus is one of the top ten modules in Drupal, because we can enable a js menu with sub levels.

The problem I found at least until now is nice menus doesn't implement by default the active trail classes used to highlight a menu or section.

If you as me needs this functionality just copy these functions to your template.php file in your theme and clean the cache, after that select a menu and you will see new active-trails css classes. CSS classes to highlight menu aren't included.

function phptemplate_nice_menu_tree($menu_name, $mlid = NULL, $menu = NULL) {

	  // Load the full menu array.

	  $menu = isset($menu) ? $menu : menu_tree_all_data($menu_name);

	

	  if(isset($menu)) {

	     $menu_page=menu_tree_page_data($menu_name);

	     $trail=build_page_trail($menu_page);

	     unset($menu_page);

	  }

	

	

	  // For custom $menus and menus built all the way from the top-level we

	  // don't need to "create" the specific sub-menu and we need to get the title

	  // from the $menu_name since there is no "parent item" array.

	

	  // Create the specific menu if we have a mlid.

	  if (!empty($mlid)) {

	    // Load the parent menu item.

	    $item = menu_link_load($mlid);

	    $title = check_plain($item['title']);

	    // Narrow down the full menu to the specific sub-tree we need.

	    for ($p = 1; $p < 10; $p++) {

	      if ($sub_mlid = $item["p$p"]) {

	        $subitem = menu_link_load($sub_mlid);

	        // Menu sets these ghetto-ass keys in _menu_tree_check_access().

	        $menu = $menu[(50000 + $subitem['weight']) .' '. $subitem['title'] .' '. 
                        $subitem['mlid']]['below'];

	      }

	    }

	  }

	  // Otherwise just set a title and move on.

	  else {

	    // Get the title from the DB since we don't have it in the $menu.

	    $result = db_result(db_query("SELECT title FROM {menu_custom} WHERE " .
                                " menu_name = '%s'", $menu_name));

	    $title = check_plain($result);

	  }

	

	  $output['content'] = '';

	  $output['subject'] = $title;

	

	  if ($menu) {

	    //$output['content'] .= theme('nice_menu_build', $menu);

	      $output['content'] .= theme('nice_menu_build', $menu,$trail);

	  }

	

	  return $output;

	} 
function phptemplate_nice_menu_build($menu,$trail=NULL) {

	  $output = '';

	  foreach ($menu as $menu_item) {

	    $mlid = $menu_item['link']['mlid'];

	    // Check to see if it is a visible menu item.

	    if ($menu_item['link']['hidden'] == 0) {

	      // Build class name based on menu path

	      // e.g. to give each menu item individual style.

	      // Strip funny symbols.

	      $clean_path = str_replace(array('http://', '<', '>', '&', '=', '?', ':'),
                            '', $menu_item['link']['href']);

	      // Convert slashes to dashes.

	      $clean_path = str_replace('/', '-', $clean_path);

	      $path_class = 'menu-path-'. $clean_path;

	

	      // If it has children build a nice little tree under it.

	      if ((!empty($menu_item['link']['has_children'])) && 
                  (!empty($menu_item['below']))) {

	        // Keep passing children into the function 'til we get them all.

	        //$children = theme('nice_menu_build', $menu_item['below']);

	        $children = theme('nice_menu_build', $menu_item['below'], $trail);

	        // Set the class to parent only of children are displayed.

	        $parent_class = $children ? 'menuparent ' : '';

	

	        if($trail && in_array($mlid,$trail)) {

	           $menu_item['link']['localized_options']['attributes']['class'] = 
                           'in-active-trail';

	           $parent_class .= ' in-active-trail ';

	       }

	

	        $output .= '<li id="menu-'. $mlid .'" class="'. 
                            $parent_class . $path_class .'">'. 
                            theme('menu_item_link', $menu_item['link']);

	        // Build the child UL only if children are displayed for the user.

	        if ($children) {

	          $output .= '<ul>';

	          $output .= $children;

	          $output .= "</ul>\n";

	        }

	        $output .= "</li>\n";

	      }

	      else {

	        $output .= '<li id="menu-'. $mlid .'" class="'. $path_class .'">'. 
                            theme('menu_item_link', $menu_item['link']) .'</li>'."\n";

	      }

	    }

	  }

	  return $output;

	} 
function build_page_trail($menu) {

	  $trail=array();

	  foreach($menu as $item) {

	    if($item['link']['in_active_trail']) {

	        $trail[]=$item['link']['mlid'];

	    }

	

	    if($item['below']) {

	        $trail=array_merge($trail,build_page_trail($item['below']));

	    }

	  }

	

	  return $trail;

	}

Enjoy it.

enzo.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Attribution Noncommercial
This Work, Drupal: How to Enable Active Trail with Nice Menus, by enzo is licensed under a CC BY-NC license.