I need to add 'Active' to the <li> tag of the Active link. At the moment this just appears in the <a> tag. In menu.inc this appears to be handled by:

function theme_menu_local_task($link, $active = FALSE) {
  return '<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n";
}

Is there a way to add this to your menu?

Many thanks

Comments

yokell’s picture

Category: feature » support
yokell’s picture

This seems to work. Is this the best way of doing this?

function phptemplate_dynamic_persistent_menu_menu_item($link, $extra_class = NULL, $id = NULL) {
   if (!empty($extra_class)) {
     $class .= ' '. $extra_class;
   }
   
 if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
  
	$active_class="active";
	 $class .= ' '. $active_class;
}

   return '<li style="display:inline" class=" '. $class .'" id="'.$id.'">'. theme('menu_item_link', $link) ."</li>\n";
 }
xurizaemon’s picture

I implemented a similar fix, but did it by overriding the theme_links() function. The code I added was:

      if (isset($link['href'])) {
        $link_path_expression = str_replace("/", "\/", drupal_get_path_alias($link['href']));
        if (preg_match("/^".$link_path_expression."/i", drupal_get_path_alias($_GET['q']))) {
          $class .= ' parent-active';
        }
      }
 

And the resulting mytheme_links() function was,

/**
 * Override theme_links() to highlight the current item's parents (calculated by path)
 *
 */
function mytheme_links($links, $attributes = array('class' => 'links')) {
  $output = '';
  if (count($links) > 0) {
    $output = '<ul'. drupal_attributes($attributes) .'>';

    $num_links = count($links);
    $i = 1;
    
    foreach ($links as $key => $link) {
      $class = $key;
      
      // Add first, last and active classes to the list of links to help out themers.
      if ($i == 1) {
        $class .= ' first';
      }
      if ($i == $num_links) {
        $class .= ' last';
      }
      if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
        $class .= ' active';
      }
      if (isset($link['href'])) {
        $link_path_expression = str_replace("/", "\/", drupal_get_path_alias($link['href']));
        if (preg_match("/^".$link_path_expression."/i", drupal_get_path_alias($_GET['q']))) {
          $class .= ' parent-active';
        }
      }
      $output .= '<li class="'. $class .'">';
      
      if (isset($link['href'])) {
        // Pass in $link as $options, they share the same keys.
        $output .= l($link['title'], $link['href'], $link);
      }
      else if (!empty($link['title'])) {
        // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (empty($link['html'])) {
          $link['title'] = check_plain($link['title']);
        }
        $span_attributes = '';
        if (isset($link['attributes'])) {
          $span_attributes = drupal_attributes($link['attributes']);
        }
        $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
      }
      
      $i++;
      $output .= "</li>\n";
    }
    
    $output .= '</ul>';
   }
  
   return $output;
}

 

If yokell's version works, though, it seems a lot cleaner :)

yrocq’s picture

Component: Miscellaneous » Code
Assigned: Unassigned » yrocq
Category: support » bug
yrocq’s picture

Status: Active » Fixed

I added an 'active-trail' class to 'li' tags for active menu items, like standard Drupal menus.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

cruzinxxx’s picture

Could you tell me exactly how you implemented the function.