Skip to content

Instantly share code, notes, and snippets.

@ezheidtmann
Last active August 29, 2015 13:55
Show Gist options
  • Save ezheidtmann/8696538 to your computer and use it in GitHub Desktop.
Save ezheidtmann/8696538 to your computer and use it in GitHub Desktop.
Properly render menu trees in a menu_block when using drupal-bootstrap subtheme (Drupal 7, bootstrap 7.x-3.x, menu_block)
<?php
/**
* Implements theme_menu_link() for the main menu when displayed in menu_block
* block.
*
* Stock bootstrap theme doesn't render menus deeper than 2 levels because the
* navbar feature doesn't support a third level. But when rendering the menu in
* the sidebar, we can do unlimited nesting because we're not using the dynamic
* navbar.
*
* This is only useful in a subtheme of bootstrap (tested with 7.x-3.0); you
* might need to adjust the name of the theme override to match your needs.
*
* We crib from bootstrap_menu_link() and theme_menu_link().
*/
function THEME_menu_link__menu_block__main_menu(array &$variables) {
$element = $variables['element'];
$sub_menu = '';
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
}
// Add "has-children" class to links which have children
if ($element['#original_link']['has_children']) {
$element['#attributes']['class'][] = 'has-children';
}
// On primary navigation menu, class 'active' is not set on active menu item.
// @see https://drupal.org/node/1896674
if (($element['#href'] == $_GET['q'] || ($element['#href'] == '<front>' && drupal_is_front_page())) && (empty($element['#localized_options']['language']))) {
$element['#attributes']['class'][] = 'active';
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment