Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By lokimoto on
Hi,
there is this nice <li{{ item.attributes }}>
thing to set active class for menu items.
Now I have a structure, where I have e.g. /node/subnode. For /node the active class is set for the "node" menu item but for /node/subnode the "node" menu item should also have the active class set.
Thanks for hints
Comments
For those interested, here is
For those interested, here is a solution using the theme.theme file:
Thanks, it's help.
Thanks, that helped me.
I just changed
$variables['items'][$key]['attributes']['class'][] = 'active';
to
$variables['items'][$key]['attributes']->addClass('active');
Will this only work if the
Will this only work if the menu id is "menu"? Is that what this does. if ($hook == 'menu'. I am outputting a li list of page title from a View, each are link to the content. How can I add a class active to this?
Also check out https://www
Also check out https://www.drupal.org/project/menu_trail_by_path
'active' and 'active-trail' classes for entire menu tree.
Thanks, very helpful approach.
Works well for first level of menu.
I have modified it to work for entire depth of menu tree.
Great, my modified version
themename.theme
menu--main.html.twig
How would I have to change
How would I have to change this, if I want to mark a specific menu trail active (1st and 2nd level) when a specific content type is opened?
You dont need to change it
You could use css to target the specific content types (classnames on bodytag) and set styles to active-trail on level 1 & 2. Thats how I would do it.
Or, perhaps the question is about expanding the menu on a certain content type? In D7 I think I used the context module for that but I dont know if its ready for d8 yet.
On sites I build nowadays I always install "menu_block" (the extra module for D8) and make the primary navigation expanded (expand all menu links). Doing so I always have the complete menu tree expanded at the top of the page, for Google to index, and for me to style as I see fit. For instance I can make the menu collapsable on mobile devices without page reload. This is the latest example I've built (based on twitter bootstrap) http://www.radiohjalpen.se/.
Yes, that's what I want to do
Yes, that's what I want to do, expand the menu to a specific menu item on 2nd level when opening articles and mark it as active. I found this module https://www.drupal.org/project/context_active_trail but after reading your solution I was wonderung if I can do this without installing two extra modules.
Ohh, that problem
Yes, that's a problem one recognizes. Haven't found a solution for that in D8 though, but it's the context-module we're lacking. Sofar I've created several menu-blocks to expand a certain subtree for a content type, but ist a bad workaround, i think. Maybee this guy is onto something? https://www.dunix-data.de/blog/drupal_8_set_menu_active_trail_based_on_t...
Well, the idea with the
Well, the idea with the different menu blocks is not bad! And how do you mark a specific menu item as active?
Well, thats the major drawback with that idea :)
There is no good way to do that (as I've figured out anyway). Maybe you could make a special theme template for that menu block and do it in twig. Or, you could style (css) a fixed menu-option as "active" on article-nodes.
hi, joos! active class works with twig cache disabled
Your code works fine. But i have multilingual site and Main menu links are created with Taxonomy Menu missile. Your code works only with twig cache disabled while i debug my theme.
But when I enable twig cache and clear cache, this code affects only first menu item I visited. And when I open other items and their nodes, 'active' is that first element I visited first time. I think result of this code is cached and is always using.
Is there solution set 'active' class to main menu item for it works even with Twig cache enabled?
Set Active Trail on LI
This appears to work really quite well. I'm curious if there is an alternate version of this which would work on the
<li>
instead of the<a>
tags on the page. Having the class on the<li>
would make it more flexible when trying to target sub-menu's of an active item.Any help would be greatly appreciated here.
Nicholas A. Bumgarner
Web Developer
Inclind, Inc.
Found a Solution
I was actually able to get this working by simply grabbing the menu.html.twig code from the Drupal 8 Core theme "Classy".
Nicholas A. Bumgarner
Web Developer
Inclind, Inc.
Simple and easy
Cool, this is exactly what I was looking for.
where did you placed the file
Nicholas
where did you placed the file? Sorry the ignorance.
Did you place it in the theme/template folder and named it like the machine name of the menu? menu--menuname.html.twig ?
Thanks for your help, I cant get it to work so far.
/themes/YOURTHEME/templates
I placed the file in my theme's templates directory. The path is themes/YOURHTEME/templates/menu.html.twig
Make sure you clear your caches!
Nicholas A. Bumgarner
Web Developer
Inclind, Inc.
Need to use class added using link attribute module to li
I have installed link attributes module and adding classes from Menu UI. Need to apply those classes to li. with li{{ item.attributes }} nothing appears in li tag. Can someone help.
This is my version without using the current path