Hi all!

I need to set a menu item as "active" depending on 3 factors:

- Content type: for example, "news" items would activate the "News" primary menu item and "All" secondary menu item.

- Vocabulary: all taxonomy pages related to a certain vocabulary would activate a specific menu item. For example: vocabulary "Apples", term "Green" -> When viewing the taxonomy term "Green", the primary menu item "Fruits" must be active, and also the secondary menu item "Apples".

- Term: different terms into a certain vocabualry would activate a specific menu item. For example: vocabulary "Cars", terms "4x4" and "SUV". When viewing the taxonomy term page "SUV", the primary menu item "Cars" must be active, and also the secondary menu item "SUV". And, when viewing the taxonomy term page "4x4", the primary menu item "Cars" must be active, and also the secondary menu item "4x4".

How can this be done? Thank you!

Comments

ToshoFreny’s picture

am also looking for the same feature for one of the sites I build. Looking for some answers here.

Loving Life,
Tosho Freny,
Reading Sex and the Osho and other Dangerous Posts at BelovedOsho.com.

Xabi’s picture

Anybody knows a PHP snippet to add in template.php or in page/node templates?

mkalbere’s picture

in menu.inc, you have the following fct. You can override it in phptemplate and make some extra detection based on vocab/terms etc ...

function theme_menu_local_task($mid, $active, $primary) {
  if ($active) {
    return '<li class="active">'. menu_item_link($mid) ."</li>\n";
  }
  else {
    return '<li>'. menu_item_link($mid) ."</li>\n";
  }
}

Xabi’s picture

Thank you very much for pointing us in the right direction. But I'm not a coder, and I would need some extra guidance, maybe an override example...

Xabi’s picture

Any more clues please?

coworksbe’s picture

http://drupal.org/project/menutrails is supposed to do this, although I don't get it to work for now...

Xabi’s picture

Right, "is supossed" to do this, but it doesn't work properly.

shawn.sh’s picture

I have hacked around a bit in the past to solve the much simplier problem of 'menu items active depending on content type'. After adding the snippet to the template.php and configuring the module within the admin panel it worked like a charm.

mkalbere’s picture

Some thing like (But if you have really no php experienece, it will be a bit difficult ...)

function phptemplate_menu_local_task($mid, $active, $primary) {
If (arg(0)=="node" && is_numeric(arg(1))){
    $current_node=node_load(arg(1));
    $item = menu_get_item($mid);
    $menu_tile=$item['title'];
    // By Title
    $node_title=$current_node->title;
    if ($node_title=="Apple" && $menu_tile=="Fruit") $active=true;
    
    //By vocabulary
    $terms=taxonomy_node_get_terms_by_vocabulary(arg(1),FRUIT_VOCAB_VID);
    if (count($terms)>0 && $menu_tile=="Fruit") $active=true;

    // By terms
    if (isset($current_node->taxonomy[APPLE_TERM_ID]) && $menu_tile=="Fruit") $active=true;
}

  if ($active) {
    return '<li class="active">'. menu_item_link($mid) ."</li>\n";
  }
  else {
    return '<li>'. menu_item_link($mid) ."</li>\n";
  }
}

But making comparaison on the "Titles" is not very clever since your title could change,

  return '<li>'. menu_item_link($mid) ."</li>\n";
to     return '<li>'.$mid.menu_item_link($mid) ."</li>\n";

so you can identify the mid and make the test on it.

Maybe you could try http://drupal.org/project/taxonomy_menu , that generate menus from your taxonomy strcuture ...

Blueeeeie’s picture

How do you do it for Drupal 6.x?

alienzed’s picture

this doesn't work in Drupal 6, what am I missing?

mkalbere’s picture

replace function by
function phptemplate_menu_local_task($link, $active = FALSE) {
...
}
and it should work
Rem: Dont forget to empty your cache ... otherwise the function wont be called