How to theme Nice Menus as table-based menus
Wherever Drupal's theme() function is called, there is an opening to change the default theming. So for Nice Menus, just check out the source code of this module where we have these two functions:
function theme_nice_menu($id, $menu_name, $mlid, $direction = 'right', $menu = NULL)
and
function theme_nice_menu($id, $menu_name, $mlid, $direction = 'right', $menu = NULL)
We'll use them as our starting point. In whatever theme you're working on there's a template.php file designed specially for this kind of purpose. Open it up and insert the following two code segments:
/*
NOTE: BEFORE YOU ACTUALLY DO THIS, PLEASE CLEAR ALL CACHES FROM YOUR DRUPAL SITE
This is the point when Nice Menus renders a fully built tree menu to the $output variable
We change it here to be wrapped inside a table instead of UL elements
*/
function YOURTHEMENAME_nice_menu($id, $menu_name, $mlid, $direction = 'right', $menu = NULL) {
$output = array();
if ($menu_tree = theme('nice_menu_tree', $menu_name, $mlid, $menu)) {
if ($menu_tree['content']) {
$output['content'] = '<table width="100%" class="site-menu"><tr>' .$menu_tree['content'] .'</tr></table>'."\n";
$output['subject'] = $menu_tree['subject'];
}
}
return $output;
}
}The consequent theme function that actually builds the menu tree:
function YOURTHEMENAME_nice_menu_build($menu) {
$output = '';
global $is_child;
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);
$current_node = str_replace('node-', '', $clean_path);
$path_class = 'menu-path-'. $clean_path;
if ($current_node == $GLOBALS['active_node']) {
$path_class.= ' active-content';
}
// If it has children build a nice little tree under it.
if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below']))) {
$is_child = true;
// Keep passing children into the function 'til we get them all.
$children = theme('nice_menu_build', $menu_item['below']);
// Set the class to parent only of children are displayed.
$parent_class = $children ? 'menuparent ' : '';
if (isset($parent_class)):
$output .= '<td id="menu-'. $mlid .'" class="'. $parent_class . $path_class .'">'. theme('menu_item_link', $menu_item['link']);
else:
$output .= '<li id="menu-'. $mlid .'" class="'. $parent_class . $path_class .'">'. theme('menu_item_link', $menu_item['link']);
endif;
// Build the child UL only if there is a child menu
if ($children) {
$output .= '<ul>';
$output .= $children;
$output .= "</ul>\n";
}
/* Notice that we have used <td> if we find a parent class otherwise it goes into <LI> elements. Here's why: All the parent menu items will be rendered inside table cells, so our idea is to create a menu system that can resize proportionally and horizontally to the browser's full width, the subsequent child elements can be placed into <UL><LI> elements which can be themed easily */
if (isset($parent_class)):
$output .= "</td>";
//Adds some extra separators after each td
$output .= '<td><span class="sep1"></span></td>'."\n";
else:
$output .= "</li>\n";
endif;
}
else {
if ($is_child == false) {
$output .= '<td id="menu-'. $mlid .'" class="'. $path_class .'">'. theme('menu_item_link', $menu_item['link']) .'</td>'."\n";
//Adds some extra separators after each td
$output .= '<td><span class="sep1"></span></td>'."\n";
}
else
{
$output .= '<li id="menu-'. $mlid .'" class="'. $path_class .'">'. theme('menu_item_link', $menu_item['link']) .'</li>'."\n";
}
}
}
}
$is_child = false;
return $output;
}And there you have it. The output will be a table-based menu. To read the full article with code and examples, see:
http://www.ravendevelopers.com
Look for the article in Blogs section

Can we change the direction
Can we change the direction parameter while overwriting the theme_nice_menu(....
change the direction parameter
I dunno Dinu if thats possible I just shared my part of knowledge here :) You have to experiment these things on your own I think...
Its better to rot in hell, than to suck in heaven.
http://www.ravendevelopers.com
Try this as the first
Try this as the first line:
function YOURTHEMENAME_nice_menu($id, $menu_name, $mlid, $direction = 'left', $menu = NULL)Note the change from
$direction = 'right'to$direction = 'left'.This will only change the DEFAULT direction, of course. Users can still set it in the admin page.
-Karlheinz
mismatched parenthesis runs into syntax error
Thankyou for your code, but testing on my drupal 6.13 "template.php" shows me that syntax error:
(!) Parse error: syntax error, unexpected '}' in /etc/drupal/6/sites/all/themes/bszen/template.php on line 198It should come out on the last line of segment code which, I think, has mismatched parenthesis in function YOURTHEMENAME_nice_menu();
Removing last parenthesis it took place in my theme as well.
Have a nice day!
d3p
mismatched parenthesis runs into syntax error d3p
Thanks for correction d3p ;)
Its better to rot in hell, than to suck in heaven.
http://www.ravendevelopers.com
working, cleaned and full tree
Thanks for the idea and codes.
Here is the working, cleaned and full tree support.
Main menu goes inside of td, other elements in ul->li. Supports unlimited branches
function analytic_nice_menu_build($menu, $main_menu=true) {
$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)
continue;
$clean_path = str_replace(array('http://', '<', '>', '&', '=', '?', ':'), '', $menu_item['link']['href']); //Strip symbols.
// Convert slashes to dashes.
$clean_path = str_replace('/', '-', $clean_path);
$current_node = str_replace('node-', '', $clean_path);
$path_class = 'menu-path-'. $clean_path;
if ($current_node == $GLOBALS['active_node']) {
$path_class.= ' active-content';
}
$children = '';
// If it has children build a nice little tree under it.
if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below']))) {
$is_child = true; // Keep passing children into the function 'til we get them all.
$children = theme('nice_menu_build', $menu_item['below'], false);
$children = ($children)?"<ul>$children</ul>\n":'';
// Set the class to parent only of children are displayed.
$parent_class = $children ? 'menuparent ' : '';
}
// Build the child UL only if there is a child menu
if($main_menu){
$output .= "<td id='menu-$mlid' class='{$parent_class}{$path_class}'>".theme('menu_item_link', $menu_item['link'])."$children</td>";
//Adds some extra separators after each td
$output .= "<td><span class='sep1'></span></td>\n";
}else {
$output .= "<li id='menu-$mlid' class='{$parent_class}{$path_class}'>".theme('menu_item_link', $menu_item['link'])."$children</li>";
}
}
return $output;
}
function analytic_nice_menu($id, $menu_name, $mlid, $direction = 'right', $menu = NULL) {
$output = array();
if ($menu_tree = theme('nice_menu_tree', $menu_name, $mlid, $menu)) {
if ($menu_tree['content']) {
$output['content'] = '<table width="100%" class="site-menu"><tr>' .$menu_tree['content'] .'</tr></table>'."\n";
$output['subject'] = $menu_tree['subject'];
}
}
return $output;
}
şarkı sözleri
Thanks buddy
Thanks buddy! Gracias !
Its better to rot in hell, than to suck in heaven.
http://www.ravendevelopers.com