The class for the "My Account" menu item in the User menu is not showing up as "active" or "active-trail" when I go to that page.

Steps taken:
1. I enabled My Account menu in the User menu.
2. Go to the page, but the menu item is not active.
3. The html is not showing the list item as "active-trail" or "active". Same goes for the anchor tag.

Comments

Nick_vh’s picture

We (together with randy) reproduced this issue and I tried finding documentation about how to set a certain item as active
http://api.drupal.org/api/drupal/includes--menu.inc/function/menu_set_ac...

Also, in further investigation we found that the active link logic happens in function menu_navigation_links
http://api.drupal.org/api/drupal/includes--menu.inc/function/menu_naviga...

The output of the debug session we held gave the following results :

The logic is expecting the path to be

user/1 (user/%user)

But instead the link that we click on to go to my account is

user

There is some automatic magic going on in redirect /user to /user/%user and therefore I guess the active link is not set

jeckman’s picture

Reproduced with fresh install of Drupal 7.8, no additional modules, Bartik theme.

Same symptoms as described. Tried with clean urls enabled and disabled.

The classes assigned are:

<li class="menu-2 first"><a href="/user">My account</a></li>
pandersb’s picture

Had to add a module with hook_menu_alter() and then enable the menu link. The active trail finally came through.

function mymodulename_menu_alter(&$items) {
    $items['user/%user'] = array(
    'title' => 'My Profile',
    'title callback' => 'user_page_title',
    'title arguments' => array(1),
    'page callback' => 'user_view_page',
    'page arguments' => array(1),
    'access callback' => 'user_view_access',
    'access arguments' => array(1),
    // By assigning a different menu name, this item (and all registered child
    // paths) are no longer considered as children of 'user'. When accessing the
    // user account pages, the preferred menu link that is used to build the
    // active trail (breadcrumb) will be found in this menu (unless there is
    // more specific link), so the link to 'user' will not be in the breadcrumb.
    'menu_name' => 'user-menu',
  );
}
Ghostthinker’s picture

#3 not working for me, did you change your custom module weight?

netboss’s picture

Adding this function to my template.php worked for me:

function MYTHEME_preprocess_page(&$vars) {
    if(!empty($vars['user'])) {
        if ($_SERVER['REQUEST_URI'] == '/user') {
            menu_set_active_item('user');
        }
    }
}

I know this is kind of "hacky", but it solved my problem.
Hopefully this help!

Ghostthinker’s picture

@netboss thank you. Works for me. Here is a slighly modified version that also respects user/*

   if(!empty($vars['user'])) {
        if (drupal_match_path(current_path(),"user") ||
            drupal_match_path(current_path(),"user/*")){
            menu_set_active_item('user');
        }
    }

This works for me. Maybe there should be a menu_set_active_trail somewhere if you have a user submenu (not tabs).

Abbass’s picture

none of the above works for me, however setting in the css header-menu ul.menu li.active-trail works for the other tabs of my Main menu

inventlogic’s picture

#6 combined with #5 placed in template.php works for me.

Anyway of adding the .active-trail as well?

ñull’s picture

Version: 7.8 » 7.15

Confirming the same bug with Sasson theme. Subscribing and bumping.

#5 nor #6 are working for me.

chanderbhushan’s picture

You can put this css :--

#header-menu .content > ul > li.active-trail > a,
#header-menu .content > ul > li > a.active {
color: #1a272e;
background-color: #f8f8f8;
font-weight: 700;
}
OR

You can add unique class through php code :

function YourThemeName_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
  $class = ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf'));
  if (!empty($extra_class)) {
    $class .= ' '. $extra_class;
  }
  if ($in_active_trail) {
    $class .= ' active-trail';
  }
  $id = preg_replace("/[^a-zA-Z0-9]/", "", strip_tags($link));
  return '<li id="'.$id.'" class="'. $class .'">'. $link . $menu ."</li>\n";
}
sun’s picture

Status: Active » Closed (duplicate)

I may be mistaken, but this sounds like a duplicate of #1564388: "My account" link is never in the active trail

warmth’s picture

On #5 you can add
<?php global $user; ?>
and replace
<?php drupal_match_path(current_path(),"user/*") ?>
with
<?php drupal_match_path(current_path(),"user/".$user->uid) ?>
to only get that behavior for the current user on his profile.

WorldFallz’s picture

Issue summary: View changes

Just an update that combines 5,6, &12 (you don't need the global $user):

if(!empty($vars['user'])) {
  if (drupal_match_path(current_path(),'user') ||
      drupal_match_path(current_path(),'user/' . $vars['user']->uid)){
    menu_set_active_item('user');
  }
}

This shows "My Account" on the user's profile page, but leaves it alone for other user profiles.

hkirsman’s picture

#13 worked for me!

danabel’s picture

#13 worked great for me until I added a sub page to the menu item and it didn't show up as a child when the menu item was active.

As a solution I have installed the menu_token module and set the URL to user/[current-user:uid]. This way everything works as expected including sub-menus. Also if the current-user token doesn't exist the menu item isn't displayed which is perfect for my needs.

jweirather’s picture

Version: 7.15 » 10.0.x-dev

#13 worked great for me to highlight the menu item for /user for the user path with the active menu classes for active or active-trail, such that when a user visits the /user page, the corresponding menu item for the user account is highlighted as active.

In template.php, wrapped in a hook like in earlier posts. Adding my final code and some keywords ^^^:

function MYTHEME_preprocess_page(&$vars) {
  if(!empty($vars['user'])) {
    if (drupal_match_path(current_path(),'user') || drupal_match_path(current_path(),'user/' . $vars['user']->uid)){
      menu_set_active_item('user');
    }
  }
}

It seems odd to me that this doesn't happen OOTB.

jweirather’s picture

Version: 10.0.x-dev » 7.x-dev

Did not mean to update version. 7.x dev is all that's showing available now.