Skip to content

Instantly share code, notes, and snippets.

@gagarine
Created July 29, 2012 21:11
Show Gist options
  • Save gagarine/3201854 to your computer and use it in GitHub Desktop.
Save gagarine/3201854 to your computer and use it in GitHub Desktop.
Working with drupal theme_menu_tree
<?php
/**
* Theme_menu_tree doesn't provide any context information
* THIS SUCKS
* But you can use hook_block_view_alter to change the theme wrapper
* OUF!
*/
function MYTHEME_menu_tree(&$variables) {
return '<ul class="nav">' . $variables['tree'] . '</ul>';
}
function MYTHEME_menu_tree__menu_top_menu(&$variables) {
return '<ul class="nav center">' . $variables['tree'] . '</ul>';
}
//main menu (top) using the menu block module
function MYTHEME_menu_tree__menu_block__2(&$variables) {
return '<ul class="nav main-nav nav-horizontal">' . $variables['tree'] . '</ul>';
}
//main menu (level 2 sidebar) using the menu block module
function MYTHEME_menu_tree__menu_block__1(&$variables) {
return '<ul>' . $variables['tree'] . '</ul>';
}
function MYTHEME_menu_tree__menu_block__1__level1(&$variables) {
return '<ul class="nav nav-list">' . $variables['tree'] . '</ul>';
}
/**
* Implements hook_block_view_alter().
*/
function MYTHEME_block_view_alter(&$data, $block) {
// Check we get the right menu block (side bar)
if ($block->bid == 'menu_block-1') {
// change the theme wrapper for the first level
$data['content']['#content']['#theme_wrappers'] = array('menu_tree__menu_block__1__level1');
}
}
@henrytran9x
Copy link

Thank you ! This helpful for me !

@henrytran9x
Copy link

hi ! I had two menu custom , but i want to two menu same theme_wrapper ! this my code

function mytheme_block_view_alter(&$data, $block) {
    // Check We get the menu services block
    switch ($block->delta) {
        case 'menu-wordpress-services':
        case 'menu-joomla-services':
            // change the theme wrapper
            $data['content']['#content']['#theme_wrappers'] = array('menu_tree__menu_custom_services');
            break;
    }

}

function mytheme_menu_tree__menu_custom_services($vars) {
    return '<ul class="menu accordion">' . $vars['tree'] . '</ul>';
}

function mytheme_menu_link__menu_custom_services(array $vars) {
    $element = &$vars['element'];
    $sub_menu = '';
    if ($element['#below']) { 
        $element['#attributes']['class'][] = 'deeper parent';
        $sub_menu = drupal_render($element['#below']);
    }
    $output = '<p><i class="fa fa-angle-right"></i>' . l($element['#title'], $element['#href'], $element['#localized_options']) . '</p>';

    return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
} 

Change wrapper mytheme_menu_tree__menu_custom_services it worked but mytheme_menu_link__ menu_custom_services not work !

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