Skip to content

Instantly share code, notes, and snippets.

@rbg246
Forked from angryobject/template.php
Last active August 29, 2015 14:23
Show Gist options
  • Save rbg246/67690b5468d94051f705 to your computer and use it in GitHub Desktop.
Save rbg246/67690b5468d94051f705 to your computer and use it in GitHub Desktop.
<?php
/**
* This snippet adds extra classes to Drupal
* menu leafs (li tags) and menu itself (ul tags).
*/
/**
* Since the standart preprocess function for menu tree, which is
* template_preprocess_menu_tree, located in includes/menu.inc,
* tends to overwrite all information about menu being rendering,
* contained in $variables['tree'], with this bit of code:
*
* $variables['tree'] = $variables['tree']['#children'],
*
* we need to override this preprocess function with our own.
*/
function MYTHEME_theme($cache) {
$mytheme['menu_tree'] = $cache['menu_tree'];
array_unshift(
$mytheme['menu_tree']['preprocess functions'],
'MYTHEME_preprocess_menu_tree_override'
);
// Here's the overriding declaration
$mytheme['menu_tree']['override preprocess functions'] = TRUE;
return $mytheme;
}
function MYTHEME_preprocess_menu_link(&$variables) {
$element = &$variables['element'];
$element['#attributes']['class'][] = 'leaf-lvl-'.$element['#original_link']['depth'];
if(in_array('active', $element['#attributes']['class'])) {
$element['#attributes']['class'][] = 'leaf-lvl-'.$element['#original_link']['depth'].'-active';
}
if(in_array('expanded', $element['#attributes']['class'])) {
$element['#attributes']['class'][] = 'leaf-lvl-'.$element['#original_link']['depth'].'-expanded';
}
if(in_array('active-trail', $element['#attributes']['class'])) {
$element['#attributes']['class'][] = 'leaf-lvl-'.$element['#original_link']['depth'].'-active-trail';
}
if ($element['#below']) {
$element['#below']['#depth'] = $element['#original_link']['depth'] + 1;
}
}
function MYTHEME_preprocess_menu_tree_override(&$variables) {
$variables['tree_raw'] = $variables['tree'];
}
function MYTHEME_menu_tree(&$variables) {
$menu_level = isset($variables['tree_raw']['#depth']) ? $variables['tree_raw']['#depth'] : 1;
$class = "menu menu-lvl-".$menu_level;
return '<ul class="'.$class.'">' . $variables['tree'] . '</ul>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment