I really need the active trail classes to be applied to my menus, as discussed in 219804, but I don't want to use the dev branch of the module for a production site. Issue 219804 includes a patch for 6.x-2.x (http://drupal.org/node/219804#comment-1131533) which I've backported to 6.x-1.3

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Mark B’s picture

Status: Active » Needs review
FileSize
3.07 KB
fei’s picture

Thank you! I'll drop a msg if I run across any problems with this patch.

The site I'm using this for will be going up in a week or two and this is one of the few issues left. Thanks!

peterjmag’s picture

This is exactly what I needed for a couple of sites that I'm getting ready to launch. I was even considering launching them with the 2.x dev version of nice_menus installed just for this capability, which is just bad form.

I applied the patch successfully on one of my simpler D6 sites. I'll try it out on my larger almost-launched sites and let you know if I have any problems.

Many thanks to you and add1sun!

asak’s picture

Mark, this is strange. I've applied the patch - but nothing changes. everything works as it did. cleared all caches and saved the modules page, but nothing. I would expect it not to work if something was wrong - not to work as it did before the patch ;)

Clues...?

I'll play around some more...

asak’s picture

Ok found it...

This patch successfully adds an 'active' class to menuparents - but not to menu items which are not parents.

To fix, i added these line:

 +        // check if this item is in the active trail
+        if ($trail && in_array($mlid, $trail)) {
+          $trail_class = 'active-trail ';
+        }
+        else {
+          $trail_class = '';
+        }

again just before the second output in the file, which after applying the patch above becomes line 365, so the function now looks as such:

function theme_nice_menu_build($menu, $trail) {
  $output = '';

  foreach ($menu as $menu_item) {
    $mlid = $menu_item['link']['mlid'];
    // Check to see if it is a visible menu item.
    if ($menu_item['link']['hidden'] == 0) {
      // Build class name based on menu path
      // e.g. to give each menu item individual style.
      // Strip funny symbols.
      $clean_path = str_replace(array('http://', '<', '>', '&', '=', '?', ':'), '', $menu_item['link']['href']);
      // Convert slashes to dashes.
      $clean_path = str_replace('/', '-', $clean_path);
      $path_class = 'menu-path-'. $clean_path;
      // If it has children build a nice little tree under it.
      if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below']))) {
        // Keep passing children into the function 'til we get them all.
        $children = theme('nice_menu_build', $menu_item['below'], $trail);        
        // Set the class to parent only of children are displayed.
        $parent_class = $children ? 'menuparent ' : '';
        // check if this item is in the active trail
        if ($trail && in_array($mlid, $trail)) {
          $trail_class = 'active ';
        }
        else {
          $trail_class = '';
        }
        $output .= '<li id="menu-'. $mlid .'" class="'. $parent_class . $trail_class . $path_class .'">'. theme('menu_item_link', $menu_item['link']);
        // Build the child UL only if children are displayed for the user.
        if ($children) {
          $output .= '<ul>';
          $output .= $children;
          $output .= "</ul>\n";
        }
        $output .= "</li>\n";
      }
      else {
        if ($trail && in_array($mlid, $trail)) {
          $trail_class = 'active ';
        }
        else {
          $trail_class = '';
        }
        $output .= '<li id="menu-'. $mlid .'" class="'. $trail_class . $path_class .'">'. theme('menu_item_link', $menu_item['link']) .'</li>'."\n";
      }
    }
  }
  return $output;
}
nielt’s picture

For anyone wondering, the change made in #5 by asak is included in the patch in #1.

This works fine for me. Thank you asak and Mark B!

jacobkdavis’s picture

Thanks a lot for this patch, I was surprised this wasn't included in the module.

Contrary to #6's comment, when I just patched using #1 it was still only setting the active class on menu items that were parents / had submenu items. I had to replace the entire function with the function from #5 to get it to work for menu items that didnt have any children.

Thanks again everybody!

echoz’s picture

For my use, I didn't need the additional change added in #5 because on non menuparent items, we still have the active class on the anchor tag itself, and I'm targeting the anchor tag anyway. This might explain why some users didn't need this.

rockdw’s picture

I have to apologize in advance, I don't really know the correct way to comment on this. :-)

I had to add the $trail_class to the final output to get it to work, otherwise, the info in #5 doesn't ever use it:

Old line:
$output .= '<li id="menu-'. $mlid .'" class="'. $path_class .'">'. theme('menu_item_link', $menu_item['link']) .'</li>'."\n";

New line:
$output .= '<li id="menu-'. $mlid .'" class="'. $trail_class . $path_class .'">'. theme('menu_item_link', $menu_item['link']) .'</li>'."\n";

There seems to be a discrepency between what's in the patch and what's in #5. but when I added the first part of #5 and then also the updated output line to the patched code from #1, it worked for me.

add1sun’s picture

Status: Needs review » Needs work

this needs an updated patch then

osopolar’s picture

Status: Needs work » Needs review
FileSize
3.41 KB

The patch above and the changes in #5 (and #9) are working fine for me.

I've created a patch. Please review and hopefully commit it to the next stable 6.x-1.4 release. Thanks.

amogiz’s picture

Wonderfful ! I was exactly looking for this for many weeks. Thanx :)

john.kenney’s picture

i gather other people have successfully gotten these patches to work, but i tried both tonight and neither one worked for me.

1st patch resulted in entire menu disappearing completely.

2nd patch resulted in no visible change to existing menus.

i think i am doing it correctly, but i'm no whiz at patching.

i get no hunk fails. looks to patch correctly. i checked the physical file and the changes appear to have been made (though I didn't examine every single line).

am using cygwin on windows vista 64 machine.

perhaps i will just upgrade to the dev version.

VladRM’s picture

Thanks for the patch ositoblanco. It works great. Hope to see it in 6.x-1.4.

john.kenney’s picture

tried again. worked!!

i'm a patching newbie, so must have done something wrong in earlier attempt.

nicholas.alipaz’s picture

Status: Needs review » Reviewed & tested by the community

Great patch, works wonderfully! even when using in one's own theme's template.php doing some overrides.

nicholas.alipaz’s picture

Status: Reviewed & tested by the community » Needs work

just looking a bit more, it seems that it does not work when items are more than one level deep.

Example 1:

link1
--link2
--link3
----link4
----link5

Example 2:

link1
--link2
--link3
----link4
----link5

Example 1 displays the proper active trails when the bolded item is active, but example 2 does not display correct active trails when the bolded menu item is active.

john.kenney’s picture

might be a CSS issue. i had to fiddle with mine to get it to work properly at each level, but now it does. i have 3 level menus like you.

you can look at mine in case that helps: http://www.aboutcallingcards.com/callingcard-review

as with other nice_menu css, it was a bit of a project to get it exactly right.

nicholas.alipaz’s picture

I doubt it would be a css issue as the active-trail and active classes are output via php.

john.kenney’s picture

oh, you are saying the classes are simply not present to style against. i misunderstood.

nicholas.alipaz’s picture

It is however interesting that your site seems to be outputting the classes. It may have to do with some sort of difference between placing the theme function within one's template.php file or not. I have it in my template.php so that I could add an extra span to each link.

john.kenney’s picture

i simply patched the module. perhaps try that to see if it works? putting it in template.php is well beyond my technical know how, so can't help you there.

nicholas.alipaz’s picture

well, I applied the patch to my module already. Then I copied out the modified/patched theme function and put it in my template.php. It doesn't seem to be working correctly when doing that.

greg.harvey’s picture

Status: Needs work » Reviewed & tested by the community

Works fine for me. I suspect something else is interfering with your active trail, nicholas. =/

Are you overriding any of the theme functions in template.php of your theme, by any chance? Could have old copies sticking in there?

Setting back to R&TBC, since it works for amogiz, VladRM, John and it works for me.

nicholas.alipaz’s picture

Can anyone confirm that the menuitems show "activetrail" classes when the "active" menuitem is two levels deep? Like my previous example:

Example 1 (works, all classes show within my links properly):

link1 [active-trail]
--link2
--link3 [active]
----link4
----link5

Example 2 (doesn't work, active-trail classes are not shown on any parents):

link1 [active-trail]
--link2
--link3 [active-trail]
----link4 [active]
----link5

greg.harvey’s picture

@nicholas.alipaz I can confirm Example 2 works for me. I'm going to load that on to our client preview server later - I'll send you a link via the contact form so you can see for yourself. Your problem is very strange... I don't have it at all. =/

john.kenney’s picture

likewise, #2 works for me -- as you saw on the link i gave you earlier.

i am not clear if, at this point, you have installed only the module -- or are still making modifications to template.php, as well. and i'm sure you've tried clearing caches and so forth to ensure you are seeing the thing clean. otherwise, no idea.

nicholas.alipaz’s picture

I am actually using this modified theme function, but don't really expect any help on it, since it has been pretty heavily modified. This is in my theme's template.php

BTW, wouldn't it be better if the theme function we have here did more like what I have done in my theme function? classes[] = .... instead of concatenating them together as a string as we go along?

function mytheme_nice_menu_build($menu, $trail) {
  $output = ''; //the output menu
  $i = 0; //the indexed menuitem
  $c = 0; //the total count of menuitems
  // find out the count for the non-disabled links
  foreach ($menu as $menu_count) {
    if ($menu_count['link']['hidden'] == 0) {
      $c++;
    }
  }
  foreach ($menu as $menu_item) {
    $mlid = $menu_item['link']['mlid'];
    $classes = array();
    // Check to see if it is a visible menu item.
    if ($menu_item['link']['hidden'] == 0) {
      $i++;
      // setup first/last/even/odd/active classes
      $classes[] = ($i == 1) ? 'first' : '';
      $classes[] = ($i == $c) ? 'last' : '';
      $classes[] = ($i % 2 == 0) ? 'even' : 'odd';
      $classes[] = (preg_match("/class=\"active\"/", theme('menu_item_link', $menu_item['link']))) ? 'active' : '';
      $classes[] = 'menu-path-'. mytheme_id_safe($menu_item['link']['href']);
      // If it has children build a nice little tree under it.
      if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below']))) {
        // Keep passing children into the function 'til we get them all.
        $children = mytheme_nice_menu_build($menu_item['below'], $trail);
        // Set the class to parent only of children are displayed.
        $classes[] = $children ? 'menuparent' : '';
        // check if this item is in the active trail
        if ($trail && in_array($mlid, $trail)) {
          $classes[] = 'active-trail';
        }
        else {
          $classes[] = '';
        }
      }
      else {
        // check if this item is in the active trail
        if ($trail && in_array($mlid, $trail)) {
          $classes[] = 'active-trail';
        }
        else {
          $classes[] = '';
        }
      }
      $classes = array_filter($classes);
      $classes = implode(' ', $classes);
      $output .= '<li id="menu-'. $mlid .'" class="'. $classes .'"><span>'. theme('menu_item_link', $menu_item['link']) .'</span>';
      // Build the child UL only if children are displayed for the user.
      if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below']))) {
        if ($children) {
          $output .= '<ul>';
          $output .= $children;
          $output .= "</ul>\n";
        }
      }
      $output .= "</li>\n";
    }
  }
  return $output;
}
Prasad Shir’s picture

I was facing same problem - Nice Menus not showing "active-trail" class. Spent few hours searching for solution. Patch at #1 worked like a charm!!

Thanks a lot for the solution!!

ambientdrup’s picture

Trying the patch from post #1 and then getting this error:

warning: Missing argument 2 for theme_nice_menu_build() in /mnt/stor1-wc2-dfw1/445745/www.site.com/web/content/sites/all/modules/nice_menus/nice_menus.module on line 337.

Any suggestions?

-Trevor

greg.harvey’s picture

@ambientdrup: Use the patch in #11 against the 6.x-1.3 version.

ambientdrup’s picture

Ok, I must have not applied the patch correctly first try. I tried it again and it's not throwing errors now ... however my parent elements are still not highlighting as active per this new patch. It's doing exactly what it did before applying the patch (not active).

Do I need to add something else?

-Trevor

ambientdrup’s picture

Hmmm ... ok I did that but it just loads a blank white screen on the site now. I must not be applying that one correctly?

I'll try it again.

-Trevor

ambientdrup’s picture

Ok, hmmm ... I applied the patch successfully from #11 but it's still not working. Child elements are active but the parent items are not.

-Trevor

ambientdrup’s picture

Ok, it's working now. I needed to fiddle with my CSS per #18.

Works like a charm now!

Thanks!

-Trevor

CrashNet’s picture

Subscribing.

redpuma’s picture

Applied patch from #11 and works fine. Thanks ositoblanco (*not tested against 3 level nav as yet)

dman’s picture

Thanks for #11. Works fine, I had a tree 6 levels deep.

robbertnl’s picture

Using #11 (http://drupal.org/node/465738#comment-2027028) as well. Will report errors here if I find some.

add1sun’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

Just setting status to backport so it gets of my RTBC radar. ;-) Leaving it open so that folks who want it can find it.

metaprinter’s picture

Can you please give me a little help on how I implement this patch? Thank you.

Never mind. I got it. using http://drupal.org/patch/create and http://drupal.org/node/60818

matt2000’s picture

Fixed my own hacked up version based on #11. Thanks.

[x] subscribe

yan’s picture

#11 works as intended!

pianomansam’s picture

Priority: Normal » Critical

Patch from #11 confirmed once more to solve this problem. PLEASE commit!

add1sun’s picture

Priority: Critical » Normal
Status: Patch (to be ported) » Closed (won't fix)

This will not be committed to the 1.x branch. This feature is in the 2.x version and 1.x will no longer be supported. Since the 2.x is working well and getting close to a stable, I'm not going to add features to old code. I'm going to won't fix this now since people really need to be moving on to 2.x. ;-)

redpuma’s picture

Why not patch it while 2.x is still beta?

Nobody wants to put a beta onto a production site. Save all the people that use the official release having to go through the same rigmarole as everybody above?

dman’s picture

> This feature is in the 2.x version

redpuma’s picture

> 2.x is still beta

Danny_Joris’s picture

I agree on the beta sentiment. If I had known that the beta version had all the newest bug fixes and the official 1.6 release doesn't, I would have downloaded the beta version.

mandclu’s picture

If you want people moving to 2.x, mark it as stable.

+1 for committing this to the 1.x branch.

Anonymous’s picture

Commit this! Seriously, it's pretty annoying having to dig around for the solution in the issues cue and apply a patch when it's existed for over a year now.