Skip to content

Instantly share code, notes, and snippets.

@freshyill
Last active August 29, 2015 14:05
Show Gist options
  • Save freshyill/3e726b252bcdeb88b16f to your computer and use it in GitHub Desktop.
Save freshyill/3e726b252bcdeb88b16f to your computer and use it in GitHub Desktop.
Drupal Menu Attributes
<?php
/**
* Implements theme_menu_tree__[MENU NAME].
*/
function [THEME NAME]_menu_tree__[MENU NAME]($variables) {
global $level;
$class = ($level == 1) ? 'topnav' : 'subnav';
return '<ul class="' . $class . '">' . $variables['tree'] . '</ul>';
}
/**
* Implements theme_menu_link__[MENU NAME].
*/
function [THEME NAME]_menu_link__[MENU NAME]($variables) {
unset($variables['element']['#attributes']['class']);
$element = $variables['element'];
$sub_menu = '';
// set the global variable in order to use it in hook_menu_tree()
// Named "level" to avoid confusing with $depth
global $level;
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
$level = 1; // set the level as first for each list with submenu
$element['#attributes']['class'][] = 'topnav__item';
}
else {
$level = $element['#original_link']['depth'];
$element['#attributes']['class'][] = 'subnav__item';
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
if(strpos($output, "active")>0){
$element['#attributes']['class'][] = "current";
}
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
?>
@freshyill
Copy link
Author

Customize the ul and li class of a Drupal submenu

This gets asked all over the place and I've come across lots of bullshit answers. This actually works. I didn't make this from scratch. I just customized the correct answers to match my needs.

Here's some sources:

http://drupal.stackexchange.com/questions/78171/assigning-custom-class-to-nested-menu-ul-elements
http://drupal.stackexchange.com/questions/10574/remove-classes-from-menu

Oh and don't add a third level menu. Just don't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment