Drupal 7.x

I have created several basic pages and i would like to show some of them as sub links, this is currently what i have:

        <div id="header-nav">
		    <?php if ($main_menu): ?>
			<?php print theme('links__system_main_menu', array(
				'links' => $main_menu,
				'attributes' => array(
					'id' => 'nav-links',
				),
			)); ?>
			<?php endif; ?>
        </div>

However, it only displays the top level menu items. How can i display the top and lower level links?? I would like to achieve:

<ul>
    <li>
        Example Top Level
        <ul>
            <li>Example Second Level</li>
        </ul>
    </li>
</ul>

Comments

nikonetwork’s picture

In the base template stark, I tried: <?php print_r($main_menu); ?>, and $main_menu doesn't carry any of the sub link items I created... Maybe a remaining bug?
I'm relatively new to Drupal so I'm not really sure if we should create an Issue for that...? I might miss something. Just in case: http://drupal.org/node/add/project-issue/drupal.

nikonetwork’s picture

pazhyn’s picture

You can show sub menu items adding next function to template.php:

function your_theme_links($variables) {
  if (array_key_exists('id', $variables['attributes']) && $variables['attributes']['id'] == 'main-menu-links') {
      $pid = variable_get('menu_main_links_source', 'main-menu');
    $tree = menu_tree($pid);
    return drupal_render($tree);
  }
  return theme_links($variables);
}

Don't forget to change function name 'your_teme'
Also this module could be helpful:
http://drupal.org/project/menu_block

agalligani’s picture

Where are you finding this hook? There doesn't seem to be a hook called hook_links()...

jaypan’s picture

It's not a hook, it's a theme function (theme_links()) that you can override.

Contact me to contract me for D7 -> D10/11 migrations.

pixelfuture’s picture

I have been searching for hours now and found no solution to this yet. If anyone knows how to achieve this please link to the solution.

Scott J’s picture

There are very detailed instructions at http://deltup.org/page.php?4/bartik to create drop-down menus in Bartik theme, which you should be able to adapt to any theme.

susan5in7’s picture

Check You have selected "Show as expanded" while creating the Parent menu. This is the most common error happening for parent-child menu items.

markh_’s picture

I wish I could delete my post - I realise now that it's probably not very relevant to v7. Might be useful to someone using v6.

This is a simplified version of how I themed my menus (in Drupal 6.15):

page.tpl.php:

...
<?= $navbar ?>
...

menu_tree.tpl.php:

<ul><?= $tree ?></ul>

menu_item.tpl.php:

<li><?= $link.$menu ?></li>

menu_item_link.tpl.php:

<a href="/<?= $item['href'] ?>"><?= $item['title'] ?></a>

A more advanced version of menu_tree.tpl.php - my submenu markup was different to the top-level menu so I changed includes/menu.inc and includes/common.inc (not recommended) to pass in the level of the menu and the parent link:

menu_tree.tpl.php:

<? 
switch ($level){
	case 0:?>
		<ul class="nav-primary"><?= $tree ?></ul><? 
		break;
			
	case 1:
		$class='nav-submenu';
		if ($parentLink['in_active_trail'])
    		    $class .= ' active-trail';?>
                <ul class="<?=$class ?>"><?=$tree?></ul><?
                break;
?>

changes to menu.inc:

0737:function menu_tree_output($tree, $level=0, $parentLink=null) {
0760:$output .= theme('menu_item', $link, $data['link']['has_children'], menu_tree_output($data['below'], $level+1, $data['link']), $data['link']['in_active_trail'], $extra_class, $data['link']);
0766:return $output ? theme('menu_tree', $output, $level, $parentLink) : '';
1139:function theme_menu_tree($tree, $level, $link) {

changes to common.inc:

3099:'arguments' => array('tree' => NULL, 'level' => 0, 'parentLink' => NULL),
betarobot’s picture

Mark, hacking drupal core files is very bad idea anyway.

mehlbye’s picture

Same here...

mehlbye’s picture

After quite a while I stumbled over this module that fixes the problem (D7):
http://drupal.org/project/menu_block

betarobot’s picture

The other way to solve it:

Create a region for menu (if there are no suitable yet) and then just enable main links block there. This will hopefully will be the only way for D8, but I use this technique starting from D5. Works like charm.

UPD: of course you'll have to style that menu block.

tea.time’s picture

I found this thread when I came upon the same issue in Drupal 6. (I'm assuming "main menu" in D7 is the equivalent of "primary links" in D6.) Here's my solution in case anyone can make use of it:

1) Define a custom theme hook (template.php)

function my_theme_theme(&$existing, $type, $theme, $path) {
	...
	
	// custom theme function for single ubiquitous site menu
	// (using Drupal default primary links menu, BUT with full tree output, not single level.)
	$hooks['full_site_menu'] = array(
		'arguments' => array(
			// if NULL, function checks variable 'menu_primary_links_source'
			'menu_name' => NULL
		)
	);
	
	...
	return $hooks;
}

2) Implement the output function (template.php)

// our theme's custom primary links menu theme function
// note when a theme (rather than a module) registers a hook,
// the assumed function name will be [theme_name]_[hook_name] ... not theme_[hook_name]
function my_theme_full_site_menu($menu_name = NULL) {

	if(is_null($menu_name)) {
		$menu_name = variable_get('menu_primary_links_source', 'primary-links');
	}

	// outputs the full hierarchy of this menu
	return menu_tree($menu_name);
}

3) Use it (probably in page.tpl.php)

    <nav id="site-menu">
        <?php print theme('full_site_menu'); // custom, registered by our theme ?>
    </nav>
0500053’s picture

Have sub menu items added dynamically with menu_link_save but links not visible.
Tried switching different items on and off for a link (in database entry). By default links are added as external=0. Changing to 1 makes sub - links always appear.

0500053’s picture

-

dianikol’s picture

I use superfish module if i want drop down menus. It's very easy and flexible

alanom’s picture

... and it's ridiculous that it isn't, here's the simple steps. This turns your menu into a menu tree with as many levels as you want (can be all of them). After this, you can add whatever CSS or JS you want to this to make them open or close or expand or dropdown or whatever on mouseover, click, etc.

  • Get the Menu Block module (stable for D5, D6, D7, probably will be for D8...)
  • Go to the Blocks page ( Structure > Blocks in D7 ) and click Add New Menu Block
  • Click Advanced Settings (it looks like it does nothing, but it adds a few options further down the page). Tick Expand all, and set it to start at level 1, continue to level 'Unlimited'

This prints to the page the whole output for the whole menu tree structured like this (may have more/less classes depending on theme). It doesn't impose anything else, it just gives you the whole Drupal menu in the HTML output to do your own thing with:

<div class="menu-block-wrapper">
  <ul class="menu">
     <li class="first leaf">
         <a href="/some-link">Some link</a>
     </li>
     <li class="expanded">
         <a href="/some-link">Some link</a>
         <ul class="menu">
             <li class="first leaf">
                <a href="/some-link">Some link</a>
             </li>
             <li class="last leaf">
               <a href="/some-link">Some link</a>
             </li>
         </ul>
     </li>
     <li class="last leaf">
         <a href="/some-link">Some link</a>
     </li>
  </ul>
</div>
tayebakel’s picture

am not sure if this is the correct post for my question ! am quite new to drupal

i was wondering if there is a way to get the title of a parent menu item ?
i am rendering the submenu in a separated block of the main menu and i want to print the parent link title as a title for the submenu !
i hope u guys can point me in the right direction

thanx

deepa_zyxware’s picture

Please try this instead,

<?php
  $main_menu_tree = menu_tree(variable_get('menu_main_links_source', 'main-menu')); 
  print drupal_render($main_menu_tree);
?>

Hope this helps.

Mirza Obaid’s picture

this simple code of your just worked superbly thank you very much i was just going here and there but finally found ha small and exact solution

sravya_12’s picture

To render all menus from main menu define the following function in your theme's template.php file

function THEMENAME_preprocess_page(&$variables) {
// Get the entire main menu tree
$main_menu_tree = menu_tree_all_data('main-menu');

// Add the rendered output to the $main_menu_expanded variable
$variables['main_menu_expanded'] = menu_tree_output($main_menu_tree);
}

Then, in page.tpl.php, you can just use:

print render($main_menu_expanded);

fonea’s picture

in template.php of the theme you can add the following code

function YOURTHEME_preprocess_page(&$vars) {

  $menu_tree = menu_tree_all_data('main-menu');
  $tree_output_prepare = menu_tree_output($menu_tree);
  $vars['primary_navigation'] = drupal_render($tree_output_prepare);
}

Thanks!