Hi

I've been using Nice Menus since Drupal 6. I'm now working on my first Drupal 8 site and I've noticed that the Nice Menu module in Drupal 8 doesn't add the class 'active-trail' (or any other class) to the parent of an active item. We need that class added so we can style the parent of an active page within a dropdown menu.

Any ideas?

Thanks

Frank

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

frankdesign created an issue. See original summary.

xiukun.zhou’s picture

HI. drupal8 have "menuparent" class

caspervoogt’s picture

I don't actually see an 'active' class being added to any menu items at all.
I do see a 'menuparent' being applied to items that have sub-items but these are also getting the 'menu-item--expanded' class even when they are not expanded.

caspervoogt’s picture

Slight correction. An "is-active" class gets added to my front page menu item link. No other menu items get an "is-active" class. My theme has nothing overriding anything remotely menu-related. I have tried it with Bartik as well - same thing.

ytsurk’s picture

I can confirm the issue.

menu-parent means that the menuitem has childrens ... not that it's active.
is-active is only set on the anchor tag.

Looking at the core's menu block i made a patch to get the active trail. Now the class .menu-item--active-trail is added like in other menus.

Attention: With this patch the menu is only autoexpanded to the current path, no longer everything! You need to expand the menu items yourself at the moment.

Is there a reason why nice_menus uses the toolbar.menu_tree service and not the menu.link_tree service like cores' does ? This could be adapted as well.

ytsurk’s picture

Here another simplified approach, with every item expanded (like it was before).

ytsurk’s picture

Here the final patch also respecting caching !

Also removing the rendering in build(), we can leave that to twig.

ytsurk’s picture

Status: Active » Needs review
Huelfe’s picture

Status: Needs review » Needs work

I just tried the patch from #7 but there is a caching problem. When I rebuild the cache and visit a page which is linked in the menu, then the visited menu item always has the class "menu-item--active-trail". When I click on other menu items, the class still belongs to the menu item, which was visited first after cache rebuild.

ytsurk’s picture

I cannot reproduce this behavior. What cache are you using ?

I have enabled page_cache and dynamic_page_cache, running drupal 8.4.3 and also below ..

.menu-item--active-trail is appended to the active menu entry (li tag) and every parent of it.
.is-active is appended only to the active menu entry (a tag).

  • ytsurk authored f2e4650 on 8.x-1.x
    Issue #2846707 by ytsurk: active-trail not added to parent of is-active...
xiukun.zhou’s picture

Thanks ytsurk

i have added a default config.

xiukun.zhou’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

dfletcher’s picture

Thanks patch in #7 worked great for me (from 8.x-1.0-beta2).