Active trails between two different menus

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
kayograco's picture

What I have:
A primary menu (one level depth) at the top of the page.
Some secondary menus to the right, that changes depending on primary links (controlled via Visibility).

What I need:
Primary links to stay active when secondary links activated.

You can see the project I'm working on at nexos

I'm a graphic designer, trying to evolve to serious web design with Drupal, and think I accomplished the theme task very well, but this challenge of active trails...

I've tried so many modules like Menu Trails, Local Menu, Node Hierarchy, and I couldn't accomplish what I'm looking for with any of those.

As you can see, the first primary link, AI (Arquitectura de la Información), is built based on taxonomy, so, I couldn't style the .active class. As for the others, are pages, so .active class is easy to style on the css.

Muchas gracias de ante mano!

Comments

Hola, I have a similar issue,

leofishman's picture

Hola,

I have a similar issue, maybewe can work it out together?
I have views with different path and sub path, when in the path, the active trails works fine, but I also need it in the subpath.

cines multiplex
When in cartelera, works fine, but in cartelera/belgrano doesn't

Have tried Menu Trails, but nothing happen.

Muchas Gracias,
leo

Throw the path into an array, set class by that array

mcskipperson's picture

I've had to do this a number of times, (so I really should just build a module for it...) and what I've found useful is to add the following at the top of the page.tpl.php doc:

<?php

  $pathalias
= explode('/', drupal_get_path_alias($_GET['q']));
 
  
$whichPage = '';

   if (!
$is_front) {
     
$whichPage = 'path-'.$pathalias[0];
  } else {
      
$whichPage = 'front';
    }
 
  
?>

This breaks the URL into an array delimited by slashies '/', and if you're using something like pathauto, you'll be able to determine where your page is categorized, then pass that into a menu.tpl.php override file located in your theme directory, and redefine how your menus are rendered, adding classes based on path, for example.

Hope this helps,
Jason Slade
chicagodrupalauthority.com

Is pathauto needed?

kayograco's picture

Hi Jason, I wonder if this module is needed to achive it. I want the project to work with the minimum possible modules, so I tried pasting your code as you suggested (becouse I think that's the way), even in template.tpl.php file, but it didn't work.

I don't like when people say "what am I doing wrong?", guess there's always a first time!

PS: Thanks for helping us out!

Kayo Graco

This looks partially like a

thomas4019's picture

This looks partially like a remake of the Drupal arg function. http://api.drupal.org/api/function/arg.

Thomas Hansen
www.ThomasHansen.me

pathauto not required for this

apperceptions's picture

This will work as long as you have slashes in your urls

for example, your page at

http://www.kayograco.com/nexos/comment/reply/19#comment-form

will yield a pathalias array in Jason's code that looks like array('comment', 'reply', '19').

Note that /nexos is part of your $base_url as so is not included.

In this case, $pathalias[0] is 'comment' and 'reply' is like your submenu item.

BTW, as a best practice, don't do this in your page.tpl.php. Instead, place the code in a page preprocessor function in your theme's template.php and pass the result back to page.tpl.php using a variable. Here's an example that adds 'comment-reply-19' as a body class and could easily be used to help select multiple elements as active.

function MYTHEME_preprocess_page(&$variables) {
$pathstr = check_plain(str_replace('/', '-',drupal_get_path_alias($_GET['q'])));
$variables['body_classes'] .= " ". $pathstr;
}

Have you tried DHTML Menu? It

design_dolphin's picture

Have you tried DHTML Menu?

It adds an active trail in the menu tree.

Also take a look at Menu Trail By Path.

An easy way to get taxonomy in menu's is Taxonomy Menus.

How to put active trail from preprocess_page?

mlargo's picture

Hi. I've been working on make a primary link active when a specific URIs is showed. I use the code:
$pathalias = explode('/', drupal_get_path_alias($_GET['q']));
to check the first path element.

And from template.tpl I use the function preproccess_page to get the primary_links' menu variable. I need to read the items of that variable and append an 'active-trail' class to the item whose href attribute have the wordkey into it. My code is like this:

function MYTHEME_preprocess_page(&$variables) {
$pathalias = explode('/', drupal_get_path_alias($_GET['q']));
$wordkey=$pathalias[0];

$primary_links=$variables['primary_links'];

foreach ($primary_links as $key => $link){
if (substr_count(strtolower($link['href']),$wordkey)>0){
$class = $attributes['attributes']['class']." active-trail";
$primary_links[$link]['attributes']['class'] = $class;
}
}
$variables['primary_links'] = $primary_links;
}

but it doesn't work. What's wrong? Any ideas?

Tnks for your help!

Re:How to put active trail from preprocess_page?

mlargo's picture

ufffs....sorry my code is:
function ueb_preprocess_page(&$variables) {
$pathalias = explode('/', drupal_get_path_alias($_GET['q']));
$wordkey=$pathalias[0];

$primary_links=$variables['primary_links'];

foreach ($primary_links as $key => $link){
$class = trim($link['attributes']['class'])." myclase";
$primary_links[$key]['attributes']['class'] = $class;
}
$variables['primary_links'] = $primary_links;

}
I am trying to append myclase' class to each menu item in the primary links. But it doesn't work.

Marius!!

Theme development

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: