Needs review
Project:
ZURB Foundation
Version:
7.x-5.0-rc6
Component:
Code
Priority:
Normal
Category:
Support request
Assigned:
Reporter:
Created:
3 Oct 2013 at 13:59 UTC
Updated:
9 May 2017 at 05:49 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
mazze commentedMy problem is just the other way round... I use custom build menus for both primary and secondary, and the theme DOES assign an active trail (since it's NOT the main menu, as you mention). But having .active-trail assigned stops the top-bar working you navigate from a sub page – the respetive 1st level can not be opened anymore, the triangle does not appear anymore.
e.g.
Section 1
- Section 1 Sub 1
- Section 1 Sub 2
- Section 1 Sub 3 (current page – the section 1 first level button is not active anymore, the section 2 first level works)
Section 2
- Section 2 Sub 1
- Section 2 Sub 2
Comment #2
museumboy commentedI'm using this as a main menu navigation. Is it standard that Foundation does not add this class in for main menu navbars?
Comment #3
mazze commented@museumboy: as far as I see: yes. And in case it does (non-main menu), the menu entry is not clickable anymore.
Comment #4
museumboy commentedIs there anyway in the template.php to add the active-trail class to this main menu item?
<li class="first leaf"><a href="link" class="active">Visit Us</a></li>Comment #5
raychaser commentedGot a solution where I use menu_get_active_trail() inside the _zurb_foundation_render_link() function to plunk the active trail in there.
Comment #6
museumboy commentedHow can you set this in a sub theme if you have no access to patch the main theme?
Comment #7
kevinquillen commentedYou can anonymously check out the code, I believe.
Comment #8
kevinquillen commentedComment #9
museumboy commentedI can see the code, how can I add this scripting into my sub theme? Adding the function into my template.php does not work.
Comment #10
ergophobe commentedHi museumboy,
What do you meant that you "have no access to patch the main theme"? You don't need any special access unless you're on a system running on multisite or something and you only have access to the subtheme.
Do you know how to apply a patch to your Drupal project?
- https://drupal.org/node/620014
- https://drupal.org/patch/apply
If you have to do this in your subtheme, you have to be aware that the patch affects helper functions that are called by hook functions.
I think you'll need to copy all the following functions from the base theme template.php into your subtheme template.php.
Then you'll have to manually apply the patch code, because it won't automatically apply since you're applying to a different file then it's intended for.
Then you'll need to rename theme replacing "zurb_foundation" with the name of your subtheme. Finally, you'll have to look for any places where the helper functions (starting with underscore) are called and then update the names there as well.
Then clear the cache and try again. I think that will do it, though of course I haven't tested it at all.
Comment #11
ergophobe commentedI tested raychaser's patch and it works fine. It has some differences from the markup you get with the core Bartik theme, but some of that markup makes no sense to me. I have a different patch that takes a different approach based on the way the core Toolbar module does it. It is a fair bit longer, but it has what I think is a bit closer to Bartik with the correction that it doesn't give an 'active-trail' class to an item that is actually active.
I'm not particularly attached to any given approach, either in terms of code or output. Here's me table with differences from Bartik bolded in each case. I originally was adding 'active-trail' in more places, similar to Bartik and changed my mind and that explains why the function _zurb_foundation_in_active_trail() creates a $classes array and uses return statements to stop at certain points. It's perhaps unecessarily complex for what it does now, but it's flexible.
I'm sure there are better ways to do this, but FWIW a patch is attached.
Comment #12
ergophobe commentedI seem to be physically incapable of getting a patch right the first time. I forgot to check out a new branch after my last patch. So #11 is all messed up.
Comment #13
ishworthapaliya commented#12 patch is working perfect. Thanks!
Comment #14
museumboy commentedEveryone's help has been great! Thank You. I finally see some evidence of the active-trail in my topbar. Right now I'm getting this warning: Notice: Array to string conversion in nysm_render_link()
on this line:
$link['#attributes'] = array_unique(array_merge($link['#attributes'], $link['#localized_options']['attributes']));The other question is can I set the active-trail in the topbar based on path? So anything this in /about will activate the about tab regardless of how deep it goes under that directory.
Comment #15
museumboy commentedJust an FYI, I tried the original line in the code
$link['#attributes'] = array_merge($link['#attributes'], $link['#localized_options']['attributes']);and that seems to work without errors.
Comment #16
ergophobe commentedI think I can get rid of the array_unique() call - I had that in there from before doing the chart comparing Bartik to other options and I mistakenly thought that Bartik had "active active-trail" in the "active child with parent" condition, so I was ending up with two "active" class mentions in some cases.
In the worst case, removing the array_unique() results in the doubling of the active class, but I don't think with the current version that would happen. If it does, it's just minor bloat as opposed to a PHP Notice.
I'll reroll the patch as soon as I can (but probably not today).
Comment #17
museumboy commentedThis seems to work perfectly, but not on any content types that have patterns set in the URL aliases screen.
Comment #18
ergophobe commentedCan you elaborate? Are you referring to a menu item that points to a piece of content that has an alias, or to the menu that displays if the current piece of content you're looking at has an alias?
Also, what does it mean to not work? What behavior are you seeing (no classes added, wrong classes added, etc?)
Comment #19
museumboy commentedThis is a topbar that is the main nav on the site. The menu doesn't change no matter where you go in the site. I only needed to insert an active-trail in each LI if you were in that particular section of the site. If you goto a page or a view or a panel like www.mysite.org/education the Education LI link in the main topbar would get an active-trail class. Nothing special. All of the site follows a consistent directory structure.
About mysite.org/about
Education mysite.org/education
Exhibitions mysite.org/exhibitions
Research mysite.org/research
and so on.
With your code the active-trail class is now in the correct LIs throughout the site with exception to any content type that has a defined alias pattern (pathauto module). In all cases these aliases are using Tokens, which I've seen create active-trail issues in other modules. For example the exhibition content type alias pattern is something like exhibitions/[node:title]
In these cases no active-trail is added to the topbar menu item. That's what I mean by it doesn't work.
Comment #20
ergophobe commentedThanks museumboy - that's plenty of detail for me to try to replicate and fix. I think I'll have some time this week, so hopefully I'll be able to figure this out and fix my patch.
Comment #21
ergophobe commentedPS - the patch needs to be fixed one way or the other,but I wonder if your situation wouldn't be helped for the time being by https://drupal.org/project/menu_trail_by_path
It sounds like your site has the right kind of structure to use it. I'm not sure what would happen, because that results in a lot of irons in the fire messing with those menu items, but it might be worth a try as a stopgap.
Comment #22
museumboy commentedI have this module already installed. I don't have this problem with superfish or menu block. It's only the zurb topbar that doesn't include the LI.
Comment #23
ergophobe commentedThanks for that - I can look at Superfish and Menu Block and see how they handle it. Should have thought of that earlier anyway.
Comment #24
chrisjlee commentedThanks @ergophobe
Comment #25
ergophobe commentedSorry for letting this drop. I just got back from 7 days with no internet and hope to put together a proper patch for this once I get dug out of the backlog.
Comment #26
museumboy commentedThank you! I was wondering if it died on the vine.
Comment #27
ergophobe commentedmuseumboy, I am unable to reproduce your issue. Here's what I did
- set a pathauto pattern as "content/testing/[node:title]"
- create a top-level menu item "Testing"
- create a node with a menu entry as "Tokenized Path" nested under "Testing"
When I visit the new node, it has the "active-trail" class on "Testing" (both A and LI) and the "active" class on the "Tokenized Path"
Do you have a sandbox install to play with? On a bare install with just Drupal, Foundation, Token and Pathauto running, can you set up a simple test case and see if you still have the issue? There might be something else stepping in there.
And for reference, there are a lot of issues related to active-trail, usually when using a custom menu. Are you using a menu that you created yourself or Main?
Some references (mostly notes to myself for future reference):
Comment #28
ergophobe commentedAdding a new patch, but all it does is get rid of the array_unique() call.
I looked at how things are done in Menu Block and Superfish, but this ends up being a fairly different situation so I fell back to the way the core Toolbar module does it.
This fixes it for me and ishworthapaliya.
museumboy - if you could test on a clean install, that would help because it works in every test I can think of on a clean install.
So I would probably commit this which should fix it for most people and then have museumboy open a new issue for his specific situation.
Comment #29
ergophobe commentedAnd just noticed this is marked as major. Changing to normal. See https://drupal.org/node/45111
Comment #30
kevinquillen commentedergo, you're last patch has changes to a .gitignore in it - not sure if that is intended to be in there or not.
Comment #31
kevinquillen commentedI have no idea why the previous comment says I deleted a file - I didn't.
Comment #32
kevinquillen commentedThe patch(es) are hard to navigate in the context of Foundation template.php. The dev version already has the line with $rendered_link being set - where/what are we actually changing? Can we generate a clean patch?
Comment #33
museumboy commentedI concur, having a clean patch would help me make sure I have everything correct.
Comment #34
museumboy commentedI installed a fresh drupal instance. Added ctools, zurb_foundation, Views, path auto, and tokens.
I created 2 nodes; an article and a page. I set the pathauto settings so that the article url will default to /articles/[node:title] and the page url will default to /pages/[node:title]
I then created two page views, one that shows a list of articles (localhost:8082/articles) and one that shows a list of pages (localhost:8082/pages).
I add those pages to the main menu
I apply the patch in #12 to the template.php.
I see exactly the same thing as I do in my other site. When I visit the view of pages or articles, the menu item receives an active-trail class. When I click on the actual article or page, the active-trail class is lost in the main menu.
Comment #35
ergophobe commentedAh, sorry. I missed that this was with views and panels pages that you're having the issue.
Sorry about getting the .gitignore in there too. I ended up doing that because every time I work with it, if the starter theme is involved, you have to change STARTER.info.txt to STARTER.info for testing... separate concern.
As for the rest
$rendered_link is actually set three times in the current version and three times in my patch. The only difference is that on the third occurrence it is set later.
The problem with the $rendered_link in the current version is that it is set before figuring out whether or not the current menu item is in the active trail and before merging #attributes into the #localized_options array.
In other words this call
$rendered_link = theme('zurb_foundation_menu_link', array('link' => $link));was coming before the $link variable had the needed classes added, so it needed to come later in the flow, after checking the active trail and after merging with $link['#localized_options']
So basically what's changing (in order) is
- postpone the theme() call that sets $rendered_link
- find out if we need to add a "active" or "active-trail" class and store it in an array
- if there are other classes in the localized options, merge the localized options array with our new values
- new set the $rendered_link with a call to theme().
I hope that clarifies.
Let me see if I can figure out why Views cause a problem.
Comment #36
museumboy commentedI don't think views causes the problem. The active-trail is added in the views screen, it is not added in the Node view.
Comment #37
ergophobe commentedOK, I still can't replicate that, but I did change the way I get the active trail items that I hope will be a bit more robust.
Thanks for all your help testing patches. I hope you have the patience for at least one more go 'round. To simplify it a bit for you for testing, I also attached template.php.txt so you can just download it instead of applying a patch in case you're not testing with the latest version from git.
Also kevinquillen, I added a lot of comments. The patch doesn't change very much except as noted above, but hopefully the added documentation makes that more clear rather than less (because of course it increases the size of the patch - I think there's now about 2x comments to code!).
Comment #38
museumboy commentedI cut and pasted your entire template.php into the main zurb_foundation theme template.php on my fresh local install. I still do not see an active or active-trail class in the node.
site.com/articles (views page) gets an active class on the articles tab
site.com/articles/new-article (article node) does not.
I have tried turning off the URL patterns and entering the urls manually. Nothing gets me an active trail.
Comment #39
ergophobe commentedDo you have a live URL I can look at?
I have broadened my tests and I still cannot get it to fail. Works 100% for me.
Do you work on an IDE that allows you to set breakpoints? If you set a breakpoint on line 257 of template.php in my current version:
$output .= '<li' . drupal_attributes($link['#attributes']) . '>' . $rendered_link;Is the active-trail class present in the $link variable?
If you don't have an IDE that lets you look at things like that, you could use the devel module and do the following just before line 257
dpm($link['#attributes']['class'], $link['#title']);Do the right ones have an array element for 'active-trail'?
Comment #40
museumboy commentedUnfortunately all of my current installs are behind a firewall.
Comment #41
museumboy commentedAs you can see (sorry about the shitty color scheme, I don't know where that came from) the article node does not get an active class. I'm trying to find a public spot to display this.
Comment #42
ergophobe commentedI may be misunderstanding, but it looks like the target page in question ("new-article") doesn't have a menu entry, so it won't have an active trail generally speaking unless you do something based on path. Does "Articles" have a dropdown submenu that includes a link to the page in the /articles/new-article page?
If not, then you wouldn't normally have an active-trail class in D7.
src: http://designhammer.com/blog/active-trails-menu-items-drupal-7
AFAIK, you would have to do something based on path/breadcrumb. Probably easiest would be Menu Trail by Path, which lets Drupal search the URL parts to see if they correspond with a path for a displayed menu item
https://drupal.org/project/menu_trail_by_path
You can also take the Menu Position approach
I could change my patch to recurse through the path and set as active based on path, but I don't think that belongs in a theme distribution since it depends on people having a hierarchical URL structure that matches the menu structure, which is not necessarily the case and who knows what URL structure people might end up with using Pathauto and how those relate to menu structure. It could create a lot of headaches for people. So if I understand your use case (and I'm not sure I do from the screenshot, since I can see if the menu has hidden subitems that I'm not seeing), I think it would be bad to make the Zurb Foundation theme handle that out of the box.
Please do the following tests
Comment #43
ergophobe commentedJust to give you an example, imagine a site where you have content types posts, galleries, and events and you have pathauto set to create paths like
post/first-post
event/test-event
gallery/vacation-pics
But the navigation has
Home
Ergophobe's Blog
Museum Boy's Blog
If you think of a structure like that, I think you can see why you can't force the behavior where an article automatically makes the Articles menu item part of the active trail.
Comment #44
ergophobe commentedI just did some additional tests.
In my tests, my patch works substantially the same as the Bartik and Garland themes with the differences noted in #11 .
None of the core themes have an "active" or "active-trail" class on menu items unless the current path is also present in a menu item. If the path of the current page is represented by a menu item, then the parents of that item will be in the active trail
Supposedly both menu_position and menu_trail_by_path will set the active-trail class on menu items if the menu item path is part of the path for the current page, but neither one worked for me. However, if *both* are installed, they do work and apply the active and active-trail classes in the type of situation described by museumboy. I didn't look further into it b/c why those modules both need to be enabled is not relevant here since the behavior is the same in Bartik, Garland and ZF.
So in short, to the best of my ability to test it
- Zurb Foundation with this patch works substantially the same as Garland and Bartik, and so I would consider that "fixed"
- Menu Position + Menu Trail by Path should address museumboy's needs. You would just create a Menu Position Rule for articles/* for example and it should work like you're thinking.
At this point, this has been tested by me (extensively!) and reported to work by ishworthapaliya in #13.
So once museumboy confirms that I've understood his situation correctly, I would move this to RBTC.
Comment #45
kevinquillen commentedComment #46
kevinquillen commentedSorry, moving to needs review.
Comment #47
museumboy commentedUsing Menu Position I do see the active-trail in the LI of the top navbar, however it also adds the node page link as a dropdown of the parent navlink. I don't want that. Is there a way to remove that? I'm sure it's possible with CSS, but removing the code itself would be better.
Comment #48
ergophobe commentedCan you post code and show the observed behavior and the expected behavior? I don't understand the verbal description.
Comment #49
museumboy commentedIn menu position i've set a rule for pages with the path articles/* to have a parent menu item of "articles".
When I'm in mysite/articles I see the Articles tab in the top bar is highlighted. This is the code:
<li class="first leaf active" title=""><a href="/articles" title="" class="active">Articles</a></li>When I go to a node under the articles directory, for instance mysite/articles/another-article I see there is now a drop down on the "articles" tab with a link to the article page that I'm on. Here is the code for that:
I don't want that drop down menu, I only want the Articles tab to get an active-trail in its LI declaration.
Comment #50
ergophobe commentedOK, now I understand.
That's a separate issue entirely and not really a Zurb Foundation issue at all. Menu Position is injecting that menu item. ZF is rendering the menu exactly as it should given that menu structure, and my patch correctly adds the active and active-trail classes given that structure. So the issue is really how to generate the structure you want.
You might be able to get help as a Menu Position support issue or handle it yourself in your own subtheme as a sort of custom thing specific to your site.
It would be possible to handle as a Foundation feature request too, but it's something that people should default to off since most people probably do not expect that behavior.
So the initial issue reported is fixed and I'm marking this as RTBC and suggest that for the specific issue of not actually having the dropdown, you either do some custom overrides in your subtheme or see if that behavior is available in Menu Position.
Comment #51
ergophobe commentedI created a feature request for you, though realistically it's probably not going to happen until someone has a need or some spare time.
#2207095: Set active-trail class if menu item path included in current page path
[edit]Truly, though, this should be handled Menu Trail by Path - see my comments in the linked Feature Request issue[/edit]
Comment #52
ergophobe commentedAccidentally replaced array_merge_recursive() with array_merge() which meant that the 'class' array element could get blown away.
So this is the same patch, except for using array_merge_recursive when merging $link['#attributes'] and $link['#localized_options']['attributes']
Comment #53
museumboy commentedI found a good fix for the issue I need help with. This patch turns off the child menu item created by menu position but keeps the active-trail bit. Thanks.
Remove Child Menu
Comment #54
ergophobe commentedGreat. With my last bug fix and your confirmation, I'm moving this back to RTBC
Comment #55
ergophobe commentedI've been using this patch for quite a while on a couple of sites. I found that in some circumstances the 'link_path' element of the $active_trail array item is not set, so it needs a simple check for !empty($item['link_path'])
Patched against a git pull from today on the 7.x-4.x branch.
Any reason this has not been committed?
Comment #56
shauntyndall commentedI'll take a look at this and get it into the 4.x branch. Do you know if this affects the 5.x branch?
Comment #58
shauntyndall commented@ergophobe I've applied #55 to the dev branch. Thanks for the contribution. If you have a chance it would be awesome if you could take a look at the 5.x branch for the same issue.
Comment #59
ergophobe commentedI queued it up in my task manager so it will keep nagging me every few days. Ditto with the other issue you flagged.
Comment #61
nickbumgarner commentedI'm opening this issue back up. I ran into this same issue on the 7.x-5.0-rc5 release branch. The patch from #55 appears to be working without issue.
I would like to have others confirm that the patch works for them on the Foundation 5 theme before committing. Any feedback would be greatly appreciated.
Comment #62
mecano commentedWhat is the point of this?
Do you mean
$link['#localized_options']['attributes']['class'][] = 'active-trail';or
?
Comment #63
sachit.thapa commentedI am not able to patch the file on 7.x-5.0-rc6. I am getting following errors:
Patching file template.php
Hunk #1 FAILED at 223.
Hunk #2 succeeded at 303 (offset 48 lines).
1 out of 2 hunks FAILED -- saving rejects to file template.php.rej
Could you help me out with this?
Thanks