Last active
August 15, 2020 18:32
-
-
Save occupant/528452b7b250476c7f5b to your computer and use it in GitHub Desktop.
Add active class to active 'li' in Drupal 8 menus
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
function THEMENAME_preprocess_menu(&$variables, $hook) { | |
if ($hook == 'menu_main') { | |
THEMENAME_menu_active_item($variables['items']); | |
} | |
} | |
function THEMENAME_menu_active_item(&$items) { | |
$current_path = \Drupal::request()->getRequestUri(); | |
foreach ($items as $key => $item) { | |
// if path is current path, set active to li | |
if ($item['url']->toString() == $current_path) { | |
// add active linl | |
$items[$key]['attributes']['class'] = 'active'; | |
} | |
if (!empty($items[$key]['below'])) { | |
THEMENAME_menu_active_item($items[$key]['below']); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm working on twig template for bootstrap. can you please post twig code for this same.