What would be good way to keep taxonomy menu tree open when clicking on node? For example lets say I have:

computers -> laptops and under that product (node) mylaptop.

So I would like to keep taxonomy menu open on computers -> laptops when I click on the mylaptop node.

My current solution is to put following php code in view in header section using php input filter.

$terms = taxonomy_node_get_terms(node_load(arg(1)));
$term = array_shift($terms);
$parents = taxonomy_get_parents_all($term->tid);
$parents = array_reverse($parents);
foreach($parents as $parent) {
  $parents_str .= $parent->tid . "/";    
}
$path = 'category/'.$term->vid.'/'.$parents_str;

menu_set_active_item($path);

This works but I want to know if there is maybe a better way to do this.

Comments

soliaris’s picture

Hello,

two questions:

1. Could you please be more specific where to put this code you proposed?
2. Will it work with nodes with multiple vocabularies? In case node belong to one vocab I suppose it is clear which menu to keep open, but if I use one more vocab?

best regards,
Soliaris

checker’s picture

menu_set_active_item() is not a good way. this function changes just $_GET['q'] with all following problems.

soliaris’s picture

Hello,

I have tried menu_trails: http://drupal.org/project/menutrails
It seems it is working as described, except in situation, node has two or more taxonomy terms. In such a case only one menu is open, and it is not based on user navigation history.

BR,
Soliaris

phade’s picture

Been busy :)

First to clarify what I need:
Term needs to appear automatically into tree menu (currently using taxonomy menu module for that).
Tree menu must keep its expanding level (path) when in node page (node page overrided with Views module)
Breadcrumbs must appear correctly on pages that list terms (when I click on taxonomy menu item) and also on node pages (being correct path representing terms tree).

So for example if I have:

Categories -> Fruits

When I am in fruits list pages I need to see tree menu opened and breadcrumbs set at Categories -> Fruits and when I click on specific fruit breadcrumbs must remain Categories -> Fruits and tree menu must remain open.

Soliaris: I put it in Views module Page: Header, changed input filter to php first.
Menu trails does not seem to do this automatically, I have to set it up by hand when I add new term. Am I mistaken?

checker: Could you clarify why this is not a good way and what alternative way you recommend.

I got everything to work, including breadcrumbs with hacking in some php code, everything in views header.


$node = arg(1);
$catname = 'category';

if($GLOBALS['user']->uid > 0)  {
  $block = module_invoke('block', 'block', 'view', 3);
  echo $block['content'];
}
$terms = taxonomy_node_get_terms(node_load($node));
$term = array_shift($terms);
$parents = taxonomy_get_parents_all($term->tid);
$parents = array_reverse($parents);

//krumo($parents);

$names = array(l('Home', '/'), l('Categories', "$catname/$term->vid"));
foreach($parents as $parent) {
  $parents_str .= "$parent->tid/"; 
  $names[] = l($parent->name,  "$catname/$term->vid/$parents_str");
}
$path = "$catname/$term->vid/$parents_str";
//echo $path;
drupal_set_breadcrumb($names);
menu_set_active_item($path);

module_invoke is needed to circumvent the problem of node edit tabs disappearing when fiddling with menu_set_active_path. Just created my own custom php block that I show with module_invoke. Following is the block code.

<?php
$node = arg(1);
?>
<div style="overflow: hidden">
<ul class="tabs primary">
<li class="active" >
   <a href="/drupal/node/<?php echo $node ?>" class="active">View</a>
</li> 
 <li >
   <a href="/drupal/node/<?php echo $node ?>/edit">Edit</a>
</li> 
<li >
    <a href="/drupal/node/<?php echo $node ?>/devel/load">Dev load</a>
</li>
 <li >
    <a href="/drupal/node/<?php echo $node ?>/devel/render">Dev render</a>
</li> 
</ul>
</div>

I still would be interested to know how to do this with existing modules.

indytechcook’s picture

Hello phade,

Thank you for using Taxonomy Menu. The reason you have so many issues is because current stable version using a custom menu path as the page callback instead of the default taxonomy module's page. If the path you go to is a path, then the menu item of that path is automatically opened and selected at the correct spot. It doesn't matter where you clicked it from. One way that might help is by using taxonomy redirect (http://drupal.org/project/taxonomy_redirect). I haven't used it myself, but in theory it changes the default path of the taxonomy term, so where ever it displays on your site it will have a consistent path. Another options would be to try out version 2 of taxonomy menu.

In the newest version of taxonomy menu there is a cleaner way to resolve this. You will be happy to know that there have been 5 new hooks added to taxonomy menu (See here for complete documentation.) Taxonomy Menu will no longer be handling any of the page callbacks. By default, the path will use taxonomy/term/$tid and be handled by the taxonomy modules. Views are used by setting the page path of the view to taxonomy/term/%. The module is extendilbe by using hook_taxonomy_menu_path where you can specify your own path, they use a view or hook_menu to handle the page call back.

What you want to do can be handled using a custom module an implementing hook_menu. You could stick the code at the beginning of the page callback function, then call views_embed_view to display the view.

If you are happy using taxonomy/term/%tid as the path, then the default taxonomy menu path will resolve most of your needs. The only issue I see are the breadcrumbs. You might look into Taxonomy Breadcrumb. I hear it's a little buggy so defining your own breadcrumb settings you our module might be a better idea.

Thanks,
Neil

indytechcook’s picture

Version: 6.x-1.x-dev » 6.x-2.0-beta1
Status: Active » Fixed

This is possible on the latest version

phade’s picture

Version: 6.x-2.0-beta1 » 6.x-2.0-beta3

Thank you for the information and help.

I tried out the new version of the module and got it to work. For some reason I had to recreate my whole vocabulary manually, I could not get my old vocabulary to work.

There was one part of your answer that confused me though. You said:

What you want to do can be handled using a custom module an implementing hook_menu. You could stick the code at the beginning of the page callback function, then call views_embed_view to display the view.

You mean this as answer to my original problem to keep taxonomy menu opened when inside node view? I understand about views_embed_view but how can that help me to keep taxonomy menu open (like I currently do with menu_set_active_item()).

Keep up the good work.

indytechcook’s picture

Reading over your need again, I think i misread it. I was thinking that if you are on the view then the menu should stay expanded and I thought that was what it already did. You want that if you click on a node from the view that the menu still be selected.

The advice could still be used to create a custom module and page callback to create a custom module and place your code in that module instead of the view, but if you have it working already, I won't change it unless you run into any performance issues. Neat Stuff thought.

phade’s picture

Hello,

I kind of thought that maybe you misread :). Anyway since nobody suggested better way than my code currently does here is the revised code that works with new taxonomy menu 2.0 default menu path type (drupal core of taxonomy/term/{node_id}). I put this code inside view that overrides node view.

- Keeps taxonomy menu expanded when viewing node.
- Keeps breadcrumbs path
- since menu_set_active_item() changes the path, local editing tabs no longer work, so we have to reinsert them.
This is the part that I most dislike about this solution

The code that goes inside view:


$path_prefix = 'taxonomy/term/';
$node_id = arg(1);
$node = node_load($node_id);
if(is_array($node->taxonomy)) {
     if($GLOBALS['user']->uid > 0)  {
          $block = module_invoke('block', 'block', 'view', 3);
          echo $block['content'];
   } 
    $term = array_shift($node->taxonomy);
    $parents = taxonomy_get_parents_all($term->tid);
    $parents = array_reverse($parents);

   //krumo($parents);

   $names = array(l('Home', '/'));
   
   foreach($parents as $parent) {
         $names[] = l($parent->name,  $path_prefix.$parent->tid);_
   }
   drupal_set_breadcrumb($names);
   menu_set_active_item($path_prefix.$term->tid);
}

Code to put in block that creates node editing local tabs

<?php
$node = arg(1);
?>
<div style="overflow: hidden">
<ul class="tabs primary">
<li class="active" >
   <a href="<?php echo $node ?>" class="active">View</a>
</li> 
 <li >
   <a href="<?php echo $node ?>/edit">Edit</a>
</li> 
<li >
    <a href="<?php echo $node ?>/devel/load">Dev load</a>
</li>
 <li >
    <a href="<?php echo $node ?>/devel/render">Dev render</a>
</li> 
</ul>
</div>

All suggestions how to improve the code, implement this with existing modules (like how to do this with menu trails without manual configuration after adding new term) are very welcome. And special thanks for indytechcook for getting this module back on the game.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

m4t’s picture

Version: 6.x-2.0-beta3 » 6.x-2.3

I'm trying to have 'active' item menu when I navigate into a node. Unfortunately, I can't...are there any solutions? I read all posts, but nobody works for me!

m4t’s picture

Status: Closed (fixed) » Active
indytechcook’s picture

Status: Active » Closed (won't fix)

Hello m4t,

As you see by phade, it requires a lot of custom code and is out of scope for the taxonomy menu module.

knalstaaf’s picture

As you see by phade, it requires a lot of custom code and is out of scope for the taxonomy menu module.

That's kind of sad, because I have the impression this is a repeatedly requested feature after browsing through the issues. I'm having trouble combining Menu Block with Taxonomy menu. Once I click a term from the Taxonomy Menu, the whole Menu Block disappears, but the right (custom) View with only the node titles is shown correctly (paths are all consistent by using "custom path" in the vocabulary settings: menu-item, menu-item/term, menu-item/term/node-title).

So after hours of configuring I thought I was really close to the solution (tried Taxonomy Menu Trails, Menu Trails, Taxonomy Redirect and Menu Trail by Path to no avail). Now it seems that solution (maintaining the appearance of the menu and its active trail), being the only missing link, will never be available for D6 whatsoever.