Last active
July 3, 2018 01:00
-
-
Save angryobject/3879798 to your computer and use it in GitHub Desktop.
Drupal 7 menu extra classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>'; | |
} |
Forked this and got it working. Removed the unshift call, and just overwrote the array for preprocess functions to be my override function. Also fixed a typo (“$metheme”) in the _theme function. Thanks again.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful, thanks for putting this up. the menu_link part works great. The menu_tree override didn’t seem to work—I couldn’t get it to recognize the override. If I renamed it MYTHEME_preprocess_menu_tree it would execute, but the depth was not available in variables at that point.