Created
August 22, 2014 00:12
-
-
Save vijaycs85/61f2b2757f83aaa5260e to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Override or insert variables into the page template. | |
*/ | |
function MYTHEEME_preprocess_page(&$vars, $hook) { | |
// Build the main menu links tree. | |
if (!empty($vars['main_menu'])) { | |
// Build links. | |
$config = Drupal::config('menu_ui.settings'); | |
$menu_enabled = Drupal::moduleHandler()->moduleExists('menu_ui'); | |
// When menu module is not enabled, we need hardcoded default values. | |
$main_links_source = $menu_enabled ? $config->get('main_links') : 'main'; | |
// Allowed max depth. | |
// @see MenuTreeStorage::MAX_DEPTH | |
$level = 9; | |
$menu_tree = \Drupal::menuTree(); | |
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($main_links_source); | |
$parameters->setMaxDepth($level); | |
$tree = $menu_tree->load($main_links_source, $parameters); | |
$manipulators = array( | |
array('callable' => 'menu.default_tree_manipulators:checkAccess'), | |
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'), | |
); | |
$tree = $menu_tree->transform($tree, $manipulators); | |
$main_menu = MYTHEME_build_menu_tree($tee) | |
$vars['main_menu'] = $main_menu; | |
$vars['main_menu']['#attributes']['id'] = 'main-menu-links'; | |
$vars['main_menu']['#attributes']['class'] = array('links', 'clearfix'); | |
$vars['main_menu']['#theme_wrappers'] = array('menu_tree__primary'); | |
} | |
function MYTHEME_build_menu_tree(array $tree){ | |
$build = array(); | |
foreach ($tree as $data) { | |
$class = array(); | |
/** @var \Drupal\Core\Menu\MenuLinkInterface $link */ | |
$link = $data->link; | |
// Generally we only deal with visible links, but just in case. | |
if (!$link->isEnabled()) { | |
continue; | |
} | |
// Set a class for the <li>-tag. Only set 'expanded' class if the link | |
// also has visible children within the current tree. | |
if ($data->hasChildren && !empty($data->subtree)) { | |
$class[] = 'expanded'; | |
$class[] = 'li-level-' . $level // @TODO: find a way to get level correct. | |
} | |
elseif ($data->hasChildren) { | |
$class[] = 'collapsed'; | |
$class[] = 'ul-level-' . $level // @TODO: find a way to get level correct. | |
} | |
else { | |
$class[] = 'leaf'; | |
$class[] = 'li-level-' . $level // @TODO: find a way to get level correct. | |
} | |
// Set a class if the link is in the active trail. | |
if ($data->inActiveTrail) { | |
$class[] = 'active-trail'; | |
} | |
// Allow menu-specific theme overrides. | |
$element['#theme'] = 'menu_link__' . strtr($link->getMenuName(), '-', '_'); | |
$element['#attributes']['class'] = $class; | |
$element['#title'] = $link->getTitle(); | |
$element['#url'] = $link->getUrlObject(); | |
$element['#below'] = $data->subtree ? $this->build($data->subtree) : array(); | |
if (isset($data->options)) { | |
$element['#url']->setOptions(NestedArray::mergeDeep($element['#url']->getOptions(), $data->options)); | |
} | |
$element['#original_link'] = $link; | |
// Index using the link's unique ID. | |
$build[$link->getPluginId()] = $element; | |
} | |
if ($build) { | |
// Make sure drupal_render() does not re-order the links. | |
$build['#sorted'] = TRUE; | |
// Get the menu name from the last link. | |
$menu_name = $link->getMenuName(); | |
// Add the theme wrapper for outer markup. | |
// Allow menu-specific theme overrides. | |
$build['#theme_wrappers'][] = 'menu_tree__' . strtr($menu_name, '-', '_'); | |
// Set cache tag. | |
$build['#cache']['tags']['menu'][$menu_name] = $menu_name; | |
} | |
return $build; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment