I have Drupal 4.7 installed and a primary links menu created. I've created a link to the home page in the primary links menu using a path of <front> to link to the front page.

For all other pages in the menu, the menu anchor tag is given a class of "active" when on the active page, but this does not hold true for the menu link to the home page.

Any ideas how I can give the home page link class="active" when on the home page?

Comments

court-jus’s picture

I'm also looking for that stuff, I'll let you know if I find it

court-jus’s picture

Here it is :

You just have to modify the l() function in common.inc this way :

function l($text, $path, $attributes = array(), $query = NULL, $fragment = NULL, $absolute = FALSE, $html = FALSE) {
  if (($path == $_GET['q']) || (($path == "<front>") && ($_GET['q']=='node'))) {
    if (isset($attributes['class'])) {
      $attributes['class'] .= ' active';
    }
    else {
      $attributes['class'] = 'active';
    }
  }
  return '<a href="'. check_url(url($path, $query, $fragment, $absolute)) .'"'. drupal_attributes($attributes) .'>'. ($html ? $text : check_plain($text)) .'</a>';
} 

And it will work. We check that the path is "" and that the url contains no query (q=node) and set the active class.

bjaspan’s picture

Here is a fix that does not require modifying core. Using PHPtemplate and the theme named 'mytheme', add this to your template.php file:

/**
 * Generate the HTML representing a given menu item ID.  This is
 * copied from Drupal 4.7 core and modified to set the 'active' class
 * on links to the front page.
 *
 * @param $item
 *   The menu item to render.
 * @param $link_item
 *   The menu item which should be used to find the correct path.
 *
 * @ingroup themeable
 */
function mytheme_menu_item_link($item, $link_item) {
  $attrs = array();
  if (isset($item['description'])) {
    $attrs['title'] = $item['description'];
  }
  if ($link_item['path'] == '<front>' &&
      $_GET['q'] == variable_get('site_frontpage', '!!! NO DEFAULT !!!')) {
    $attrs['class'] = 'active';
  }
  return l($item['title'], $link_item['path'], $attrs);
}
OpenChimp’s picture

This is a great fix. I want something a little more complicated. I would like for my primary navigation bar to highlight the current section that the user is in. Right now, the bar is only highlighted when the user is on the actual page that that link goes to.

Do you know how I could modify this function to check whether the link is a top level menu link within the Primary Links menu and if so, check whether the current page is a subpage of that menu item?

Thanks

Michael
www.webemulsion.com

scottrussell-1’s picture

I had the same problem, and the tip on modifying the common.inc file worked like a charm. (The fix involving the template.php file didn't work for me, but perhaps I didn't do it right.)

I've got the identical problem with the search page. I can't figure out how to assign it a "class=active" css tag when it's in my PRIMARY LINKS. Any tips? Thanks...

scottrussell-1’s picture

A drupal user (hswong3i) helped me figure this one out in another drupal forum thread. I had manually entered in the path for the search page as "search" instead of "search/node". Although the page would load my way, it wouldn't allow the css to function correctly. After changing this path in "admin -> menus -> primary links", everything's working as expected.

sodani’s picture

Anyone figure this one out? If you add the primary link with the path "user", that will get users to their user area, but an active class doesn't get displayed...

bjaspan’s picture

I submitted a patch for this bug.

http://drupal.org/node/85204

isaac77’s picture

for those without themeing experience, here is a simpler but less elegant solution:

* create a "Home" link in your navigation menu, and set its path to the actual node that you are using for the front page (e.g. "node/50")

* create a url alias from the front page node ("node/50") to the path "home"

-> the menu item should now highlight properly. In some situations, the url of your home page will appear as /home .