Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By armada1 on
Hello everybody,
I would like to integrate an accordion effect for my sidebar navigation using Jquery.
Therefore I tried to change the output of the function phptemplate_menu_tree.
But unfortunately I am not able to change the php code to get such a listing:
<ul>
<li class="expanded"><a href="/">menu1</a></li>
<ul class="menu">
<li class="leaf"><a href="/">submenu1</a></li>
<li class="leaf"><a href="/">submenu2</a></li>
</ul>
</ul>
<ul>
<li class="expanded"><a href="/">menu1</a></li>
<ul class="menu">
<li class="leaf"><a href="/">submenu1</a></li>
<li class="leaf"><a href="/">submenu2</a></li>
</ul>
</ul>
My template.php looks like this:
function phptemplate_menu_tree($pid = 1) {
$msm = variable_get('menu_secondary_menu', 0);
if ($tree = menu_tree($pid)) {
$output .= "<ul>";
$ul = $msm == $pid ? '<ul>' : '<ul class="menu">';
return "\n<ul class=\"menu2\">\n".$tree."\n</ul>\n";
$output .= "</ul>";
return $output;
}
}
function phptemplate_menu_item($mid, $children = '', $leaf = TRUE) {
$msm = variable_get('menu_secondary_menu', 0);
$menu = menu_get_menu();
if (in_array($mid, $menu['visible'][$msm]['children'])) {
return '<li>'. menu_item_link($mid) . $children ."</li>";
} else {
return '<li class="'. ($leaf ? 'leaf' : ($children ? 'expanded' : 'collapsed')) .'">'. menu_item_link($mid)."</li>\n". $children;
}
}
Has anybody tried to integrate an accordian menu in his site yet and may help?
I am using Drupal 5 with multiflex37 theme.
Thank You!
Comments
Yeah
Yeah actually I have done it with a module called DHTML Menu. Make sure you download the head of it though because the other versions act a little wonky in multiflex.
yup
dhtml menu works like a charm for this without the coding
How I achieved this
I achieved this effect by rewriting the theme functions just like you were going for. But to finish the job, I had to also rewrite some of the core Drupal menu functions--but NOT by hacking the core. I just copied the core functions
menu_tree
andmenu_item_link
into my template.php, renamed them slightly (tomenu_tree_full
andmenu_item_link_accordion
), and then in my new theme functions I called these new functions instead of the normal core ones. Now I had a self-contained menu-generating system based on the Drupal one, but not conflicting with it.In the comments of the
phptemplate_menu_tree
function I paste below, you'll see the lines:This requires some manual configuration. When you set up a menu in the Menu admin, and want it to be an accordion, you have to figure out what "pid" it is by following the commented instructions. Then you add it to the array. In my case it was 2. You can add others, comma-separated, and these menus -- and ONLY these menus -- will be rendered by the new accordion-enabled functions. Your other menus will be unchanged.
This was custom written to deal with my site's needs, and my accordion in particular (jQuery Accordion), and assigns CSS classes as such. You may need to modify this to fit your needs, but I hope it'll at least serve as a model for what you can do. I'd paste a link to a finished example, but the site isn't live yet!
little problem
Vallenwood thanks for your great contribution. It's nearly doing what I am looking for, it's just not picking the good menu in my hierarchy. I have searched as you mentioned the pid of the menu and replaced it in the code. But it's picking instead the menu pid of the complete admin architecture.
Example: if I am putting 2, it's displaying "Recent Posts" which is the second item in my admin menu.
Any idea how to fix this?
Thanks
Nicolas
You have to manually pick menu - whole menus ONLY
Well, you have to manually find and enter the number of the menu you want. The way you locate the "pid" number of the menu you want is to go into the Menu administration screen, then find the menu you want to use. Keep in mind that it must be a ROOT menu, like "Primary Links" or "Navigation" -- I'm pretty sure this won't work right, if at all, if you try to apply it to a "branch" of a higher-level menu. This must apply to a WHOLE menu, or nothing at all.
So let's say you want to apply this effect to the "Primary Links" menu:
phptemplate_menu_tree
and find the line:Replace the number "2" with the number "5," since 5 is the number of YOUR desired menu. Your code will now be:
(If you have another menu you also want to do this with, just add it to the array, so if you want to do this with two different menus, you might have something like
array( 5, 16 )
)Now, wherever that menu is rendered, it will render using the new theme functions I pasted here, and output all the child menus.
Any one got this to work in 6
I'm really interested in getting the accordion for a primary menu working in 6.3 . Anyone got any tips?
http://modelagnostic.co.uk/
div containers?
Is it possible to output in div containers
something like:
//and so on...
Just a quick observation...
I don't know **** about jscript so I won't comment on that but this looks wrong to me:
You can't have a an (un)ordered list directly in a parent (un)ordered list.
is wrong.
Make that:
www.opstijgendenevel.nl
My simple solution
Hi,
I also needed to wrap the second level entries of a menu into a div container for styling purposes. Here's what I did:
This will result in something like this:
Best regards,
Björn