Hi,

I have added a drupal menu to my site called Forum Navigation. I want to alter the html code for only this one menu so that the main

    can have a custom ID and CLASS (
      . How can I do this? I have searched the forums, and have tried the instructions here: but they seem to be only for secondary and primary links not other menus. Thanks in advance for the assistance.

      Ano1

Comments

ano1’s picture

Hi,

It seem I forgot to but my code between < code >tags therefore it does not show up. Here is a re-post of my questions.

I have added a drupal menu to my site called Forum Navigation. I want to alter the html code for only this one menu so that the main <ul> can have a custom ID and CLASS (<ul class="demo" id="demo">. How can I do this? I have searched the forums, and have tried the instructions here: but they seem to be only for secondary and primary links not other, custom, menus. Thanks in advance for the assistance.

Ano1

ano1’s picture

Hi,

After some more research I have located this snippet:

$menuhtml = theme_menu_tree(182);
return "$menuhtml"; 

This outputs my menu. The menu gets outputted with classes like Leaf, expanded etc. However I would like this one menu to be output without these. I was debating a string replace, but was hoping for another solution using the API. Also I would need the 1st < u l > that was outputed to have a custom id and custom class and then all the rest of the ULs to be class and ID less.

Any suggestion of how to do this? If I was to do string replace how could I replace the only 1st < u l > class of menu with something else?

Thanks,
Ano1

jraper@groups.drupal.org’s picture

I am not sure if this is the full answer, but I found this very helpful in what I think is a similar need:

http://webpodge.com/2007/02/28/drupal-tutorial-part-4-styling-an-individ...

Replace all instances of "#block-block" with "#block-menu" and it allows you to style individual menus.

Cheers.

WilliamB’s picture

Hello, i wanted to add a custom id "top" to the top level of the main menu in Drupal 7.
To do it, i added the following function hook in template.php

function mytheme_menu_tree($variables) {
	if ($GLOBALS['custom_variables']['main-menu']['active'] ) {
		if ($GLOBALS['custom_variables']['main-menu']['level'] == 0) {
			$GLOBALS['custom_variables']['main-menu']['active'] = false;
			return '<ul class="menu" id="top">' . $variables['tree'] . '</ul>';
		} else {
			$GLOBALS['custom_variables']['main-menu']['level'] -= 1;
		}
	} 
	
	return '<ul class="menu">' . $variables['tree'] . '</ul>';
}
function mytheme_menu_link($variables) {

	$element = $variables['element'];
	$sub_menu = '';
	
	//Si on est dans le menu principal
	if ($element['#theme'] ==  'menu_link__main-menu') {
		if (!isset($GLOBALS['custom_variables']['main-menu'])) {
			$GLOBALS['custom_variables']['main-menu']['level'] =  0;
			$GLOBALS['custom_variables']['main-menu']['active'] =  true;
		}
		
		if ($element['#below']) {
			$GLOBALS['custom_variables']['main-menu']['level'] += 1;
		}
	}
  	
	
  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

Basicaly i created a new global variable $GLOBALS['custom_variables']['main-menu'] that will let me know if i'm in the main-menu and if i'm it'll let me know at which level i'm. Generating specific HTML if i'm in the main menu on the top level.

duckzland’s picture

is it neccessary to do a $GLOBAL ?

I think it is best if you check the &$variables first, it should contain all the $menu array generated by drupal.

cheers

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com

WilliamB’s picture

Well when i check the $variables in mytheme_menu_tree($variables) it only contains the html of the current level i'm working on.

What i would like is to have access to an array that have all the information of my menu. I mean the higher level element their children and everyone links. That way i could just add the html myself. But i can't find how to do it and it's really bothersome as i've to integrate a rather complicated menu layout.

duckzland’s picture

Not too sure if this is gonna work :)

by looking at theme_menu_tree function it calls for :

function template_preprocess_menu_tree(&$variables) {
  $variables['tree'] = $variables['tree']['#children'];
}

as you can see it only looks for the children ($variables['tree']['#children']) maybe if you override it to show the entire tree ($variables['tree']) it can show the whole tree?

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com

WilliamB’s picture

This doesn't work because all i get is the rendered HTML.
I want to get the array and build my menu myself.

Need to add CSS and place link in separate column depending on their description etc...

Thanks for answering though.

I'm really starting to be late on this, so if anyone can help, please do.

simone960’s picture

doesn't seems working... have your tested it?

EugeneChechel’s picture

/**
 * Generate the HTML output for a single menu link.
 *
 * @ingroup themeable
 */
function yourtheme_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }

  return l(t($link['title']), $link['href'], $link['localized_options']);
}
simone960’s picture

I need a solution also, anyone found it?