D8 explanation:
I've installed bootstrap 8.x-3.x on a multilanguage site, but in the default main menu the home link is never active, when I'm in the home page the claass="is-active" won't appear.
This doesn't just occur with bootstrap though, it's a core issue.
See screenshots:
Active class missing (illustration)
Active After Applying Active Class
D7 explanation:
There is a bug with the "active-trail" class not showing up on <li> for menu items with Drupal 7.14. The problem is that "active-trail" is missing for a menu link to the front page (path "<front>"), all other links have "active-trail" just fine.
The situation is also slightly different for the horizontal primary link tabs that can be switched on from theme settings Toggle display - Main menu, than they are for the vertical navigation in the Main menu block. In the horizontal tabs the front page <li> has "active" but not "active-trail". So "active" could be used to do get around this issue for horizontal menus. But unfortunately if you want the vertical menu, there is no "active" nor "active-trail".
I verified that both "active" and "active-trail" are missing with a fresh install without any added modules on Drupal 7.4 to 7.14. The behaviour is identical with Bartik, Garland and Stark themes, as well as Zen.
Steps to reproduce:
1. Create a fresh install of Drupal 7.14.
2. On the front page go to Structure -> Blocks.
3. Move "Main menu" block to "First sidebar"
4. Create a new "Basic page" with test content and click "Provide a menu link" -> "Parent item" [Main menu]
5. Use e.g. Firebug to verify that the newly created test page link on the sidebar has "active-trail" on <li> but "Home" does not.
Attached is a patch that fixes the bug by adding special handling for the front page when adding the active-trail class. A better solution would be to get the 'in_active_trail' variable correctly set for links to <front>, but unfortunately I could not find out how to do that.
Comment | File | Size | Author |
---|---|---|---|
#66 | active_class_missing.png | 189.63 KB | joseph.olstad |
#66 | active_propert_after_applying_active class.png | 188.27 KB | joseph.olstad |
#64 | D8.9.x-menu_class_active_trail-1578832-64.patch | 2.07 KB | joseph.olstad |
#64 | D9.0.x-menu_class_active_trail-1578832-64.patch | 2.04 KB | joseph.olstad |
Issue fork drupal-1578832
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
ttiurani CreditAttribution: ttiurani commentedAttached is v2 of the patch, which is better because it does the special case handling in the place the 'in_active_trail' variable is set. With this patch caching works like it should.
Comment #2
ttiurani CreditAttribution: ttiurani commentedThere seems to be a bigger problem on menu.inc than just the missing "active-trail". I stumbled on a second problem when trying to create a submodule on menu_block which hides inactive menu items. Setting 'hidden' to TRUE works great for every other link except the one(s) pointing to <front>. The variable change just doesn't stick - it turns back to FALSE right after I set it. I also don't think menu_block is to blame because it doesn't do anything to the 'hidden' variable.
I found one place which is clearly wrong in menu.inc starting from line 2468:
The problem is that none of these will find a link to <front>. When I add a fifth condition like this:
I get a little bit further, but not too much because in menu.inc there is a lot of $map = explode('/', $path); which do not work with <front> which has path 'node'.
So the bug seems to be quite a lot bigger than I anticipated.
Comment #3
ttiurani CreditAttribution: ttiurani commentedI fixed my new problem with 'hidden' not sticking by using references to values (i.e. added a few ampersands). But I don't understand why the problem was only with links to the front page and not all links.
Attached is version 3 of the patch. It is against the 7.x branch and does not have tabs or whitespace problems.
The fix isn't perfect as this only adds "active-trail" to the vertical menu, not the horizontal primary links tabs.
I hope this patch will be applied or a better solution is found to new 7.x versions.
Comment #4
kristofferwiklund CreditAttribution: kristofferwiklund commentedI had the same problem. When change the menu linking for "Home" link from node/6 to the li lost it's active-trail that was used for styling. After applied the patch everything worked as it should.
Might not be the best solution to special case but I don't know if there is any other way.
Comment #6
gagarine CreditAttribution: gagarine commentedI think is why using
Fail and make the menu block disappear.
I don't understand the tests than fail but we need to add some test for this patch anyway.
Comment #7
CowTownFarmBoy CreditAttribution: CowTownFarmBoy commentedI have this problem and am wondering if the proposed patch is really a fix or does it fail in other places. If someone is watching this is this fixed or not?
TIA
Comment #8
ttiurani CreditAttribution: ttiurani commentedAttached is a fourth version of the patch with a trivial "isset($item['link_path'])" addition that should make the patch pass those tests. I don't know how to run (or write new) tests, so unfortunately I can't say for sure. Could someone run the tests again?
I don't think this patch causes any problems and it is a fix for the problem, although not very pretty. The real issue seems to be much deeper and harder to solve as I outlined in comment #2 above.
Comment #9
gagarine CreditAttribution: gagarine commentedComment #10
ttiurani CreditAttribution: ttiurani commentedUnfortunately the v2, v3 and v4 do cause problems: when you apply the patch and press "Clear all caches", and then navigate to any other page except the front page, "active-trail" doesn't show up. And if you do go to the front page after clearing caches and get "active-trail" to show up, it stays on when you navigate to the user page, which I don't think it should. I also have managed to get "active-trail" to disappear a few times when I went back and forth to the user page and menu.
So it seems that _menu_tree_data() is too complex a method for a quick fix.
The first version of the patch, where the special casing is done in menu_tree_output() is the most stable, but doesn't fix the problem in modules such as menu_block which override that method.
So I guess the only thing to do is to fix this properly. I need to get this to work immediately so I will just do a quick fix in Menu Block.
Comment #11
aliyayasir CreditAttribution: aliyayasir commentedStill have this issue in drupal 7.16
Comment #12
drewschmaltz CreditAttribution: drewschmaltz commentedI'm also not getting children of in the secondary menu - maybe this is related. Sort of shocked here. Fighting this and the fact that "active" never shows up for "my account".
Comment #13
matulis CreditAttribution: matulis commentedWorkaround for the
<front>
linkshttp://drupal.org/node/1571058#comment-6788662
Comment #14
jenlamptonPatch in #8 works great, thanks @ttiurani!
Comment #15
David_Rothstein CreditAttribution: David_Rothstein commentedAs far I can tell, this would exist in Drupal 8 too? Also, #10 indicates there are problems with the patch...
Comment #16
David_Rothstein CreditAttribution: David_Rothstein commentedSee also possible related issue or duplicate: #1063278: Menu item linked to <front> doesn't know its active
Comment #17
vortex9 CreditAttribution: vortex9 commentedI applied the patch v4 to 7.19, but it does't work
Comment #18
ttiurani CreditAttribution: ttiurani commentedAs I pointed out in #10, only the first version of the patch works, but it is a hack. I ended up using Menu Block to get around this problem.
Comment #19
vortex9 CreditAttribution: vortex9 commentedI'm using Nice menu. I can't combine Nice menu with Menu Block :(
Add this http://drupal.org/node/1571058#comment-6788662
Its works!
Comment #20
jenlamptonAlright, this takes the approach from the comment mentioned above and adds the active class attribute in the theme function - getting around the caching issues caused by the previous approach.
Attached patches for D8, and D7.
Comment #21
zhangx1a0 CreditAttribution: zhangx1a0 commentedpatch #20 works great!
Comment #22
Screenack CreditAttribution: Screenack commentedPatch core at your own risk. Until core is updated appropriately, I like this solution better: http://drupal.org/node/1571058#comment-6788662
Comment #23
vortex9 CreditAttribution: vortex9 commentedupdate to 7.22 and apply #20 - cool!
Comment #24
David_Rothstein CreditAttribution: David_Rothstein commentedComment #25
peyman_6000 CreditAttribution: peyman_6000 commentedfix_missing_active_trail.patch queued for re-testing.
Comment #26
vortex9 CreditAttribution: vortex9 commented#20: core-fix_missing_active_trail-1578832-20.patch queued for re-testing.
Comment #27
gappleAs per #2126319: <strong>Strip HTML tags for issue titles in listings</strong>, HTML tags in issue titles are not entirely escaped on the listing page, which is breaking validity.
(and has been preventing http://drupalreleasedate.com from being able to parse the page in order to get a count of issues for some time now)
Comment #28
jenlamptonPatch in #20 still applies cleanly to 7.32
Comment #29
jenlamptonadding tag back.
Comment #32
jenlamptonPatch in #20 still applies cleanly to 7.39
Comment #34
toomanypets CreditAttribution: toomanypets commentedComment #35
toomanypets CreditAttribution: toomanypets commentedThis work-around almost works with Drupal 8.1.8...
/admin/structure/menu/link/standard.front_page/edit
/admin/structure/menu/manage/main/add?destination=/admin/structure/menu/manage/main
Menu link title: Home
Link: /node
But /node is shown when you hover over the Home link, and /node will be exposed to search engines... neither of these are desirable.
I believe this is another symptom of the
<front>
or/node
or/
mess.Comment #37
alisonHarrumph, any chance of a fix getting into 8.x soon, or a functional patch for D8? :( :(
(Yes I know I'm not helping....... I'll look now and see if I feel capable enough to do something to help instead of just whining........)
Comment #38
alisonErrrrr never mind! Apparently it "just works," we just had a config wrong (someone added the menu link via menu admin UI, and linked to the front page or node/1 -- as soon as I removed that link and instead edited node #1 and added the menu link from in there, it worked fine/no troubles).
Sorry and never mind and thanks!
Comment #40
alsator CreditAttribution: alsator commentedUsing superfish module has resolved it for me
Comment #43
legolasboI tried fixing this in the part that builds the menu tree, but time constraints on my project force me to take the template_preprocess_menu() route.
Comment #44
legolasboShouldn't have changed the version. Sorry
Comment #46
legolasboPrevious patch caused fatal errors with external urls,
Comment #48
jenlamptonPatch in #34 still applies cleanly to Drupal 7.59.
Comment #50
lamp5I can confirm that code from #46 works well. I use it as my theme preprocess until it will be tested and committed to core.
Comment #51
Martijn de WitTried patch #48 as described in #50
When I visit the frontpage. Not only the frontpage link < li > is in the active-trail. But also all siblings.
Comment #52
pdenooijer CreditAttribution: pdenooijer at Ordina Digital Services for Sijthoff Media commentedImproved patch 46:
instanceof Url
checks to make sure no WSOD occur.Comment #53
pdenooijer CreditAttribution: pdenooijer at Ordina Digital Services for Sijthoff Media commentedComment #54
lukassykora CreditAttribution: lukassykora as a volunteer commentedThis version is working for me.
Comment #56
pdenooijer CreditAttribution: pdenooijer at Ordina Digital Services commentedSorry lukassykora but your patch is just a less defensive version of my patch and it failed the tests, so changing it back to review on my patch.
Comment #57
mishac CreditAttribution: mishac commentedThe patch in 52 seems to make *all* links on the front page have the "active-trail" class instead of just links to the front page :(
Comment #58
adamtong CreditAttribution: adamtong commentedi found a work-around for this issue, just create a disabled menu item for the true home page (e.g. /node/1) under home link.
Comment #59
lpeabody CreditAttribution: lpeabody commentedThe patch in #52 applied cleanly on 8.7.8 and it worked for me. I cannot confirm the side effect mentioned in #57 - every menu link looks like it has the appropriate classes and only links representing the current page show as is-active.
Comment #60
Martijn de WitAdded an automated test for 8.8 and 8.9 to the patch #52.
Hopefully this can be committed soon.
Comment #61
alexpottI think #60 is missing a patch. This issue needs an automated test in order to be committed.
Comment #62
joseph.olstad@Martijn de Wit, can you please dig up the automated test that you wrote and somehow failed to upload to this issue?
Comment #63
joseph.olstadpatch #52 still works great! Just tested it manually.
@Martijn de Wit, could you please find that automated test you wrote 5 months ago and somehow failed to upload?
Comment #64
joseph.olstadHad to reroll patch 52 for Drupal 9.0.x
So to simplify I up uploading two patches, one for 9.0.x and the other for 8.9.x/8.8.x/8.7.x
patch 8 is for D7, still applies cleanly, I'll split that into a new seperate D7 issue
Comment #65
joseph.olstadFor the D7 issue:
#3119861: [D7] Class active-trail not added to li element when linking to front page
Comment #66
joseph.olstadActive class missing (illustration)
Active After Applying Active Class
Comment #67
joseph.olstadComment #68
Martijn de Witno, sry
Comment #69
joseph.olstadafter a few weeks of testing, patch 64 has regressions, it caused more than one link to have the active trail classes on the same page
I backed off the patch and removed it from our build.
As a workaround , our links for both of our languages are on one menu, internal links are fine, for external links we're using a workaround with css using a contrib module which allows specifying a class to the link. There's other ways to work around the external links I've used this method and another is to use also html lang attribute then the a element css selector on the href attribute based on a pattern to hide or show the opposite language external link.
Using one menu for both languages our active trail works fine. I'm not sure about the site default page issue haven't noticed it yet.
Anyhow, the patch 64 was derived from the others and I backed it off, no longer using it, doing a workaround instead.
previously we had menus for the default language and another different menu with another language and used block visibility on language for this, but this method proved to break the active trail for the 'other' language. So workaround is to use one menu for both our languages as I mentioned in this comment.
hope this helps.
Comment #70
xjmWe should fix this bug in D8 too, so filing against 8.8.x (which is the current bugfix support branch). The issue will be automatically updated to 8.9.x after the last 8.8.x bugfix release. Thanks for juggling all the different patches for this fix!
Comment #72
nicholasThompsonJust applied patch 64 before reading comment 69... Can confirm 64 adds the in_active_trail too generously 😉
Comment #73
nicholasThompsonAhh so the problem for me is that the frontpage is a node (node/1). So:
$validHomepageRoutes = ['', $homepageUrl->getRouteName()];
This contains and "entity.node.canonical" so every menu item that is front or a node gets flagged.
Comment #74
joseph.olstadThis being not fixed leaves us wanting to fix this.
challenges:
rather than do these workarounds, we would like to add a menu block per language allowing us to have unique weights for menu links and not have to fuss about external link translation for the url which is not always the same , often each language has an external link equivalient but for whatever reason (maybe explained above) this breaks the active-trail likely due to the bug mentionned above.
Currently we're working around this issue by using the https://www.drupal.org/project/menu_link_attributes menu_link_attributes module to provide a class attribute for links. We put this together on the same menu for both languages instead of a menu for each language (weights is another issue)
We want to make sure to maintain active trail functionality in both languages, there's the active class that indicates that the current link is what we're presently on, this is part of the menu functionality we didn't want to break and also keyboard navigation, due to wanting active trail to work in both languages we used one menu for two languages instead of two menus due to us wanting to work with what works for active trail menu links, one menu, links of all languages, internal links are no problem as they have a label that is translated and the internal link is automatically generated however external links like this one required a change in implementation to both the javascript and css, so we put a eng-only class on the english link that puts display: none on for the english link in french and a fra-only class on the french external link and there's another class attribute also, Drupal will render both links , css hides the other language external link, HOWEVER this does not fully allow wcag compliance in my recent wcag audits, SO we wrote some javascript to remove the other language link after the page is ready before the user navigates with the keyboard so when the menu link is configured as such using the specified class the javascript will also kick in and actually remove the other language link from the front end display which allows keyboard navigation to function correctly (wcag compliance, keyboard navigation is required). So the page renders, the css hides the other language link so it is never visible, then the javascript kicks in and removes the other language link from the html and thus we are able to pass wcag keyboard navigation and use the keyboard and tab key as expected.
it could be easier!
Comment #75
lpeabody CreditAttribution: lpeabody commentedI'm going to try and address this in MenuTreeStorage::loadByRoute. Reasoning for this:
Assume you have the front page set as /node/1234. Also assume you have a menu named
main
and that menu has a single link which uses<front>
.Given the above, currently if you were to attempt to invoke
MenuTreeStorage::loadByRoute('entity.node.canonical', ['node' => 1234], 'main')
, you would get no results, even though that node is the front page.I think the best course of action here is for the service to be aware of whether or not the passed route name/parameters represents the front page, and if it does then make the 'route_name' portion of the query part of an OR condition, where that condition also checks for
<front>
.EDIT: I realize I didn't explain why this actually solves this issue. Which menu items get flagged as active comes from Drupal's
menu.active_trail
service. That service uses the current page's route match to get the page's route name and parameters, and invokes the aforementioned function to get the active link, and then it gets that links parents to build the rest of the trail.The current page's route match will never, ever be the
<front>
route, it will always be what the actual route object is for that page in the system. So, we have to help MenuTreeStorage understand what the front page is.Comment #77
lpeabody CreditAttribution: lpeabody as a volunteer commentedOpened a MR with a somewhat rough implementation of where I think this needs to go. My local testing has been positive so far, and it appears to fix this issue in its entirety. I am not sure what kind of performance hit this adds. Still needs automated tests.
Comment #78
lpeabody CreditAttribution: lpeabody as a volunteer commentedYeah don't use the above branch for anything yet, lots of failed tests relating to cyclic dependencies, I'm hoping to be able to dig into it later today.
Comment #79
pfrenssenThis is the circular reference error thrown that is mentioned by @Ipeabody:
Comment #82
Neograph734I've previously also noticed some weird behavior in the active trail when the homepage contained a query string (example.org?foo=bar). Weirdest that I fgured out is that there is also JavaScript involved. If you disable Javascript the front page never gets an active trail class.
Linking as relevant issues.