Hi. I have noticed that many people are asking to have one feature - namely - to have parent <li> tag to have .active class attribute when child is selected/clicked.

I have found this code but couldn't manage to make it working with drupal 5.2.
Probaly somebody will make it and share with us.

WOULD BE HIGHLY APPRECIATED IF THIS FEATURE IS IPLEMENTED INTO NEAREST NICE.MENU RELEASE.

<?php
function _nice_menu_tree($pid = 1) { 
  $menu = menu_get_menu($pid); 
  $output['content'] = ''; 

  $output['subject'] = $menu['items'][$pid]['title'];

    $trail = _menu_get_active_trail();
  
  if ($menu['visible'][$pid]['children']) {
    foreach ($menu['visible'][$pid]['children'] as $mid) {
      $is_active = array_search($mid, $trail);
      if (count($menu['visible'][$mid]['children']) > 0) {
        $class_tags = $is_active ? " class=\"active menuparent\"" : "class\"menuparent\"";
        $output['content'].= "<li id=\"menu-$mid\"$class_tags>".menu_item_link($mid);
        $output['content'].= "<ul>";
        $tmp = _nice_menu_tree($mid);
        $output['content'].= $tmp['content'];
        $output['content'].= "</ul>";
        $output['content'].= "</li>";
      } 
      else {
        $class_tags = $is_active ? " class=\"active\"" : "";
        $output['content'].= "<li id=\"menu-$mid\"$class_tags>".menu_item_link($mid)."</li>";
      }
    }
  }
  return $output;
}
?>

Comments

ivrh’s picture

Title: Active parent Item when child selected » Solution:

I have changed nicemenus.module a bit to do what I want - I hope it'll be usefull to somebody else.

To use my code, find "function theme_nice_menu_tree" string (drupal 5.x ONLY) and replace the whole function with my code

Two thing you need to do:

1. Find out Menu ID and change $pid = ...;
2. Find out Top-level menu items ids ('pid' column in 'menu' table - one ID per one top-level menu item) and either change my values (if they go each after another - using placeholder in SQL statement below) or list them one by one using placeholder in SQL statement below.

This is the code:

function theme_nice_menu_tree($pid = 2, $menu = NULL) {
  $menu = isset($menu) ? $menu : menu_get_menu();
  $output['content'] = '';
  $trail = _menu_get_active_trail();

  $output['subject'] = $menu['items'][$pid]['title'];

  if ($menu['visible'][$pid]['children']) {
    foreach ($menu['visible'][$pid]['children'] as $mid) {
		
      // Build class name based on menu path e.g. to give each menu item individual style.
      $path_class = 'menu-path-'. str_replace('/', '-', $menu['items'][$mid]['path']);
      if (count($menu['visible'][$mid]['children']) > 0) {
	  
	  if($_GET['g']) { 
	  		$drupal_path = $_GET['q'];
		} else {
					$drupal_path = arg(0);
					if(arg(1)) $drupal_path .= '/' . arg(1);
		}
	  $Path = drupal_get_path_alias($drupal_path);
	  if($Path) {
	  		$proper_menu = "";
	  		$sql = db_query("SELECT m.pid FROM {menu} m WHERE m.path LIKE '%s' AND m.pid > '%d' AND m.pid < '%d'", $Path, 101, 108);
	  		$result = db_fetch_object($sql);
			$Mid = $result->pid;
	  }

	 //print $result->pid;
			//print $mid;
	  	if($Mid == $mid) {$path_class .= ' active';}
			$output['content'] .= '<li id="menu-'. $mid .'" class="menuparent '. $path_class .'">'. menu_item_link($mid);
			$output['content'] .= '<ul><!--[if lt IE 7]><iframe></iframe><![endif]-->';
			$tmp = theme('nice_menu_tree', $mid);
			$output['content'] .= $tmp['content'];
			$output['content'] .= '</ul>';
			$output['content'] .= '</li>';
      }
      else {
        $output['content'] .= '<li id="menu-'. $mid .'" class="'. $path_class .'">'. menu_item_link($mid) .'</li>';
      }
    }
  }
  return $output;
}
add1sun’s picture

Category: support » feature

Moving to feature request. Note that the best way to get features into the module is to supply patches for your code so it is easier for others to test and play with.

add1sun’s picture

Title: Solution: » Parent LI tag to have .active class

Also retitling.

add1sun’s picture

Arg, this is actually a duplicate of yet another issue: http://drupal.org/node/73902. I'll keep this as the main one since other dup'd issues point here already. PLEASE keep all discussion of this issue in one thread - this one.

add1sun’s picture

I'm adding this forum link here as well: http://drupal.org/node/188596

add1sun’s picture

Version: 5.x-1.x-dev » 6.x-2.x-dev

Moving all feature requests to HEAD. We'll work on new features after the port to 6 is complete.

add1sun’s picture

Status: Active » Closed (duplicate)

Patches started in another thread so while this is older, I'm going to mark this duplicate. http://drupal.org/node/219804