I'm working on my main tabs and I just noticed that my home page tab does not activate.

On each of the other tabs, theme_links returns the 'active' class when you are on the page represented by the link. This does not seem to apply to the home page.

I made the home page by creating a page type, giving it an index.html path alias (because I could not assign a blank) and then assigning "index.html" as the Default front page in Administer/Site Configuration/Site information.

In Administer/Site Building/Menus I assigned the first menu link to .

Now the menu link works correctly but because "/" does not match up with "index.html", it does not return the "active" class on the menu item.

What is the correct way to assign a home page without any path alias at all? The home page should be "/" or base_url, not "index.html".

Thanks,

Chadj

Comments

chadj’s picture

Sorry, line should read:

In Administer/Site Building/Menus I assigned the first menu link to <front>.

mholloway’s picture

I had the same problem today. I got round it as follows:

- in Site Information set your default homepage e.g. node/1 using whatever the node number is for the page you want to be the homepage

- ensure none of your pages is promoted to front page

- set your menu "home" link to the address of your homepage e.g. node/1

When I did this the homepage link had the active class, and navigating to www.mydomain.com with no filename specified in the url takes me to the homepage. I assume it is promoting the page to frontpage that causes the active class to not be added but I haven't looked at the code so this is just a guess.

HTH

MichelleBlum’s picture

Great suggestion. It worked great for me... 4 years later. lol

chadj’s picture

Looks like this is actually a minor pending bug in Drupal.

I was only able to work around problem with a template hack. Since all other links are working correctly (being assigned the 'active' class when the current page matches the link) I only needed to worry about the home page. So, I added the 'active' class manually on the first menu item when the URI is empty (ie, at the home page). I'm posting the code here in case anyone else has this problem:

Here's an excerpt of my phptemplate_links function in templates.php.

The code section originally read:

      // Add first and last classes to the list of links to help out themers.
      $extra_class = '';
      if ($i == 1) {
        $extra_class .= 'first ';
      }
      if ($i == $num_links) {
        $extra_class .= 'last ';
      }
      $output .= '<li class="'. $extra_class . $class .'">';

I inserted one line to force the extra 'active' class:

      // Add first and last classes to the list of links to help out themers.
      $extra_class = '';
      if ($i == 1) {
        $extra_class .= 'first ';
// this line added to allow for the first (<front>) link to be highlighted when on the home page (URI empty)
if(!(trim($_SERVER[REQUEST_URI], '/'))) $extra_class .= 'active '; 
      }
      if ($i == $num_links) {
        $extra_class .= 'last ';
      }
      $output .= '<li class="'. $extra_class . $class .'">';
marcvangend’s picture

Chadj, thanks for the hack.
However I can't get it working because the template.php (I think you meant 'template.php', not 'templates.php') which came with my theme doesn't contain a phptemplate_links function. I'm only just beginning to understand theme overrides, so if you can post the complete function, that would be a great help.
Marc

mlncn’s picture

This workaround probably works here too.

If you are using a static front page ( node/23 ) or a view or a panel, or anything but the default 'node' list, you can call the path directly in your menu item, and it will work without any hacks. That is, make your menu a link to what you have set to in site settings, if it's anything but /node

~ben

People Who Give a Damn :: http://pwgd.org/ :: Building the infrastructure of a network for everyone
Agaric Design Collective :: http://AgaricDesign.com/ :: Open Source Web Development

benjamin, Agaric

Petra’s picture

If you are using a static front page ( node/23 ) you have two urls for the same content.
I override the theme-functions:

For homepage-link in primary-links I use this function in template.php:

/**
* Implementation of theme_menu_links().
*
* Add active class to current primary-menu item links.
*/
function phptemplate_menu_links($links, $attributes = array()) {

  if (!count($links)) {
    return '';
  }
  $level_tmp = explode('-', key($links));
  $level = $level_tmp[0];
  $output = "<ul class=\"links-$level ".$attributes['class']. "\">\n";
  foreach ($links as $index => $link) {
    $output .= '<li';
    if (stristr($index, 'active')) {
      $output .= ' class="active"';
    }// frontpage AND current-link in menu is <front>
    elseif((drupal_is_front_page()) && ($link['href']=='<front>')){
      $link['attributes']['class'] = 'active';//add class active to <li
      $output .= ' class="active"';//add class active to <a
    }
    $output .= ">". l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']) ."</li>\n";
  }
  $output .= '</ul>';

  return $output;
}

In page.tpl.php for primary- and secondary-links e.g. in zen-theme :

<?php print theme('menu_links', $primary_links); ?>
<?php print theme('menu_links', $secondary_links); ?>

- or in garland and barlow.:

<?php print theme('menu_links', $primary_links, array('class' => 'links primary_menu')) ?>
<?php print theme('menu_links', $secondary_links, array('class' => 'links secondary_menu')) ?>

To make a homepage-link active in other menus I use this function in template.php (http://drupal.org/node/87902):

/**
* Add frontpage 'active' state handling to menu links
* and section 'active' state handling when child is active
* and menu-[title] as css id
*
* modified from code originally posted at
* rogerlopez.net/handbook/primary_menu_tweaks
*
*/
function phptemplate_menu_item_link($item, $link_item) {
  static $menu;
  if (!isset($menu)) {
    $menu = menu_get_menu();
  }
  $mid = $menu['path index'][$link_item['path']];
  $front = variable_get('site_frontpage','node');
  $attribs = isset($item['description']) ? array('title' => $item['description']) : array();
  $attribs['id'] = 'menu-'. str_replace(' ', '-', strtolower($item['title']));
  if((($link_item['path']=='<front>') && ($front==$_GET['q'])) ||
    (menu_in_active_trail($mid))){
  $attribs['class'] = 'active';
  }
  return l($item['title'], $link_item['path'], $attribs);
}

Works for <front> in other menus. Works in garland, barlow, nonzero and zen (in some themes you must change the css - I can poste this too, if desired).

David Vespoli’s picture

Thanks for posting this overrides phptemplate_menu_links function Petra. This worked great in my zen based setup.

Cheers!

JuliaKM’s picture

This was a big help. Adding function phptemplate_menu_links and changing print theme worked perfectly.

sudomaster’s picture

I have tried to make this work with a fresh install of zen and garland (and other themes). I'm not sure what I'm doing wrong... I have followed your instructions exactly and I still cannot produce an "active" state for a primary link item. It is almost as if drupal is not "reading" template.php while loading the page. I have deleted my cache and done all the normal things to make sure that everything is as it should be, but no go.

FYI, I am running Drupal 5.2 on Ubuntu LAMP install. Another thing, after making the changes in garland, the primary links have disappeared completely....

Ok, I've been trying some things out to test and found that when I assign the primary links to "Navigation", the active status works, but when the primary links are assigned to a menu I've built, they don't work...??? Help!

This has been fixed.

tantotea’s picture

Hi Petra,

I am using drupal 5.7.
It works to me but only for the home page, while my other primary link cannot be class-ed 'active'.

To check, I print out $index variable at each loop:

...
foreach ($links as $index => $link) {
    $output .= '<li';
    
    /*** Here I check the value of $index ***/
    drupal_set_message("index=".$index." link=".$link['href']);

    if (stristr($index, 'active')) {
        ...

but no 'active' word in each $index, so the primary link cannot be class-ed active.

I have tried to change the function name "phptemplate_menu_links" to "phptemplate_links" (as someone suggested), but still doesn't help.

What else could be the reason?

Thanks in advance.

__
tantotea

tantotea’s picture

Ok.. pheww.. it works now.

I use Petra's solution.
Previously, in the menu administration page, I set the URL of my primary links to their alias (using URL Alias functionality). It didn't work.
Now, after setting the URL of my primary link to its original URL (in the menu administration page), it works.

It seems like Drupal will auto generate the 'active' class if the URL of a menu item, which is set in the menu administration page, is the original URL (not its alias).

__
tantotea

mediamash’s picture

How do you print out a non standard menu other then the primary and secondary links?

For example: we have a 3rd menu called 'menu_three' and a 4th menu called 'menu_four', what parameters should be alterd in the code

function phptemplate_menu_links($links, $attributes = array())
and
<?php print theme('menu_links', $primary_links, array('class' => 'links primary_menu')) ?>

keesje’s picture

Possible workaround:

Yes, this is working. In my opinion it has one major drawback in SEO: duplicate URL's for the same content. www.yourdomain.com/ will return the same content as 'www.yourdomain.com/node /#' where node /# matches your homepage. You really want your homepage to be known by searchengines only by the full domainname without trailing someting.

webdesign nijkerk, putten, amersfoort

josiahdajo’s picture

You are right about Drupal's creation of duplicate content. However, there are many ways of fixing this from an SEO standpoint.

I just found a post about ways of fixing this here: Changing a page's URL alias breaks existing links. "would be nice" if a module could track and provide 301 redirects.

Here's an excerpt I found particularly helpful.

I use on all of my sites Path Redirect and Global Redirect.

Global Redirect redirects your visitors coming from node/123 to the corresponding alias - with 301.
Path Redirect allows you to specify your old path and its new target.

Dave Cohen’s picture

This patch should also solve the problem:
http://drupal.org/node/173459

Gina G’s picture

Were you able to solve the problem?

Gina, Web Designer currently working on the Cancer Treatment Online Pharmacies project.

Igbonine’s picture

Its all so confusing.