Problem/Motivation
When a menu item is active, an 'active' class is provided to the menu item, but it doesn't append to the array so clobbers any pre-existing classes.
This particular use case was employing Menu Attributes to provide a custom class on the menu item (<li>). When the page is visited and becomes 'active', that custom class is stripped.
Proposed resolution
In function radix_preprocess_menu() of /includes/menu.inc append to the array instead:
$variables['items'][$key]['attributes']['class'] = 'active';
to
$variables['items'][$key]['attributes']['class'][] = 'active';
Comments
Comment #2
somatick commentedThe above fix has been tested on a client site, this appears to rectify the issue
Comment #3
chroid commentedPatch provided to ensure classes aren't being stripped when menu item is active. Thanks @somatick for the initial investigation and fix!
Comment #4
bramdriesenRTBC +1 :)
Comment #5
crzdev commentedI was getting this notice due to attributes is an attribute class instance instead of an array:
"Notice: Indirect modification of overloaded element of Drupal\Core\Template\Attribute has no effect in radix_preprocess_menu()..."
Here goes new patch taking that into account.
Comment #6
crzdev commentedComment #7
chroid commented@CRZDEV, thanks!
I can see the same notice in the logs, but it hasn't occurred for a number of months on our dev site. Do you have any steps to reproduce the error so I could test the patch in a targeted manner?
Cheers
Comment #8
crzdev commentedNot sure about the exact combination of modules influencing into that but it can happen (most probbably is "link_attributes"), so i decided to contribute the patch, and it should also work in case the attributes is a simple array too.
Thanks @Chroid for the feedback.
Comment #9
yi_jiang commentedright, so bootstrap storybook came from here:
https://www.drupal.org/project/bootstrap_storybook/issues/3167086
My fixing
https://www.drupal.org/files/issues/2020-08-27/nav-menu-in_active_trail-...
Comment #10
chroid commentedCan we RTBC this?
Comment #11
chroid commented@CRZDEV - Was finally able to replicate this error (more that it just started popping up unannounced).
Confirming that the patch in #5 resolves this problem (Indirect modification of overloaded element)!
RTBC'ing
Cheers.
Comment #12
crzdev commentedLooking at referenced issue "https://www.drupal.org/project/radix/issues/3147154", here goes another patch for the "8.x-4.x" branch in order to not require a re-rol to apply.
With this change, all item attributes should be added correctly and won't be lost by not using then into nav component template.
Also adding another patch to add this fix also to "5.0.x" branch.
Comment #13
crzdev commentedReturning to needs review to evaluate both changes.
Comment #14
crzdev commentedComment #17
ckngThanks for all the patches. Unfortunately, the correct one is mentioned in #3167086: nav menu in_active_trail attribute isnt really set.
Comment #18
ckngBackported to 8.x-4.x.