Sub menu system
Hi,
Just starting with Drupal (which looks great btw). I have a little menu building issue and am a bit confused with all the answers I read in the forum.
I have a double menu system :
+ 4 rubrics which remain the same all through the navigation (which I made my primary menu in the settings)
+ 8 different rubrics with sub-rubrics in some on them (my secondary menu in the settings).
I am using a customized theme and am struggling a bit with the display of the secondary menu.
I have something so far looking like that, which is working fine for the main rubrics. But I am stuck on the way to display the sub-rubrics when clicking on the main rubric link.
<?php
$secondary_links = menu_secondary_links();
if (is_array($secondary_links)) {
$output = "<ul id=\"menuDeroulant\">";
foreach ($secondary_links as $link) {
// display main rubric
$output .= "<li><a href='".$link['href']."'>".$link['title']."</a></li>";
// sub rubric
}
$output .= "</ul>";
}
echo $output;
?>Any help would be really appreciated :)
Thanks a lot
Nicolas

define:rubric
define:rubric
I thought I was going to learn a new word today, but now I'm more confused than ever. I suspected it was something to do with magic invocations ... and it seems it is ... but still can't figure out how applies to menu structures :-}
(My vocabulary elsewhere is full of terms like 'ontological semantics' and 'axiomatic predicate' - but I've never yet had the chance to use 'rubric' in a sentence :-)
Primary and secondary menus are sometimes treated like special-cases, and may not nest well.
Do you get different results if you just invoked/enabled the menu block (using block admin), rather than using the menu_secondary_links() thing?
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
Sorry for my "mystical"
Sorry for my "mystical" term. English is not my mother tongue as you could have guessed ...
I tried playing with the blocks using the Nice menu contribution. I now have my sub rubrics :) displayed in my admin. But as I am using a customized theme for the front office, how could I call this block in my page.tpl.php?
Thanks for your help and if you need some more weird word give me a call :)
Nico
try this
If your page.tpl is customised so much that you can't use the existing 'regions', you can invoke a block by module-name and id.
Find the menu id of your menu (see the URL when you go to edit it) then
<?php$modulename = 'menu';
$blockid = 145;
print theme('block', (object)module_invoke( 'block', $modulename, 'view', $blockid) );
?>
It really is easier to use the regions however. Are you using 'header'?
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
something missing ...
Thanks Dan but that does not help
My menu is structured that way
* Menu 1
* Menu 2
--- Sub menu 2.1
--- Sub menu 2.2
*Menu 3
.....
What i can't understand is that when i execute the following code i don't have in my array the sub menu 2.1 and 2.2 ...
$menu = menu_get_menu();var_dump($menu['visible']);
i must be missing something somewhere ...
anyway thanks again for your help. I have decided to write my own sql queries to build my menu
What is 'visible' is decided by context
What is 'visible' is decided by context and the menu tree rules. Where you are is detected from the URL, not from what you asked for.
If you are at root, then the submenu branches are not normally visible. So even though you can manually ask for menu2, the system will not have filled in the tree below it as they were not needed for that page.
Unless you'd flagged it always expanded.
The menu just takes care of all those rules for you. And caching. And security. I'd warn that trying to build the whole thing on the fly each pageload will probably impact your performance.
Use menu_get_menu() and remake the tree from that instead.
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
tree structure from menu_get_menu
Dan, I followed your advices and built my menu thanks to menu_get_menu function. It works perfectly.
for those who struggled like me here is my piece of code if it can help ...
<?php
$mid = variable_get('menu_secondary_menu', 0); // gets the mid from secondary menu (use menu_primary_menu) for primary links
$menu = menu_get_menu(); // retrieves the tree structure
$root_menu = $menu['items'][$mid]; // gets the tree structure for the secondary|primary links
// you can then start building your menu
$output = "<ul id=\"menuDeroulant\">";
for($i=0;$i<count($root_menu['children']);$i++){ // loops on all the children of the main links
$child = $menu['items'][$root_menu['children'][$i]];
$output .= "<li><a href='".$child['path']."'>".strtoupper($child['title'])."</a>";
// check for grand children and do the same kind of loop
if(is_array($child['children'])){
$output .= "<ul class=\"sousMenu\">";
for($j=0;$j<count($child['children']);$j++){
$grandchild = $menu['items'][$child['children'][$j]];
$output .= "<li><a href=\"".$grandchild['path']."\">".strtoupper($grandchild['title'])."</a></li>";
}
$output .= "</ul>";
}
$output .= "</li>";
}
$output .= "</ul>";
?>
I guess you can then use this bit of code in a block structure.
Thanks for your help and keep the good work
Nico
Makes enough sense
Makes enough sense for your special case.
You've lost a bit of the theme goodness, but that's normal. :-}
You may like to have the 'active' flag back in your code however. I think theme_menu_link() shows how it works.
Glad you managed to play with menu_get_menu() OK. It's a bit of a monster to look at, but makes sense after analysing it for an hour or two ;-)
Yep, that's a useful snippet for learning to build on.
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
quick hack
Just a quick hack to display only the visible items of your menu
replace in all code
$menu['items']by
$menu['visible']I did not manage to get the active flag however. Can't find in the menu_get_menu array any 'active' string as mentionned in the theme_menu_links function.
The menu_get_active_item() is also returning negative results. I have tried with the menu_get_active_nontask_item() and read some comments on the forum but still don't figure out how to get the mid of the active page...
Nico
found everything i needed ...
on this page http://drupal.org/node/11816
I've not played with this
I've not played with this myself, but AFAIK the built-in primary/secondary is designed to handle sub-menus, so you might be better off using the built-in menu magic.
I think you set the same menu as both Primary and Secondary, and then Secondary will show the child items of the currently selected Primary menu item.
That would take care of your 8-topic menu.
You could add the 4 other topics in Primary as items with no children, or make an entirely new custom menu, which you can then display as a block.
Just what I was looking for!
"I think you set the same menu as both Primary and Secondary, and then Secondary will show the child items of the currently selected Primary menu item."
Wow! You are my hero. I think this should really be somewhere in the newbie documentation... now it all makes sense!
Phew!
------------------------------------------------
I provide web hosting for $50/year. To contact me email contact [at] Drozzy [dot ]com