Skip to content

Instantly share code, notes, and snippets.

@occupant
occupant / THEMENAME.theme
Last active August 15, 2020 18:32
Add active class to active 'li' in Drupal 8 menus
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