Dynamic taxonomy-based menus
There are many modules (like Taxonomy Blocks and Taxonomy Menu) that will take a taxonomy and make one kind of menu or another out of the terms in that taxonomy. However I have not found a module, or thought of an approach using Views or the like, that will allow me to create a dynamic menu that will create a menu of related content. For example, if I'm on a page where the current taxonomy term is "popsicles" then I want a menu in a block in a sidebar that looks something like this:
FAQ
Recent Stories
Rules
All three of these would link to views or pages with an argument of the tid for popsicles. But if I switched to "lollipops" then I'd want the same menu, but now all the options would require an argument of the tid for "lollipops"
Thoughts? Ideas? Suggestions? Or am I writing a custom module for myself.


Ical feed
The few places I've need
The few places I've neeedd similar things, I've settled for creating a block and theming it like a menu, but it's not an optimal solution. Messes up breadcrumbs, active trails, and more.
I'll be interested to know how other people handle it.
What's the exact relationship?
Hello, Grant,
Exactly what type of relationship will exist between the node being viewed (that is in Term X, or "Popsicles") in your example, and the menu items in the dynamic block?
If that relationship can be made explicit, and consistent, then some type of views argument (along with some handling code) will be your friend. Possibly, it might be necessary to add in a dollop of Relationship mojo to give the proper flavor to the argument -- but the starting point will be to precisely define the relationship between the node being viewed and the content of the dynamic block.
FunnyMonkey
Click. Connect. Learn.
Using Drupal in Education
The only relationship is that
The only relationship is that they all share the same taxonomy term. However I see no way to use views as I do not want to point to individual nodes, I want to point to a collection of nodes. I.e I don't want to dynamically produce a list of related nodes, only a dynamic pointer to that list. In a sense I want to point to the page for the view, not the nodes in the view. So in my example FAQ, Recent Stories and Rules would all point to page views, but the pointers would dynamically have an argument to determine which taxonomy the page views need to show. So while getting related content I know how to do, dynamically creating pointers to these views is not something I know how to do with Views or any other module I could find. It does seem like something I'm surely not the first to need.
Grant
Create 2 views
The first view will collect nodes in a specific taxonomy term; you will need a page display for this, and it will need to take an argument for the taxonomy term.
Then, create a second view that lists the taxonomy terms connected to the current node -- create a block display. Then, rewrite the terms as a link, and grab the tid (generated from this view) and rewrite the link to point to your first view (use the path from first view, and the tid from the second view).
If I'm understanding you correctly, that'll get this done.
FunnyMonkey
Click. Connect. Learn.
Using Drupal in Education
Braining is hard for me!
Hrm. My views-fu seems to be too weak and I doubt I am explaining myself adequately. Thanks for sticking with me. As I understand it, your suggestion would see me create a menu item line for each taxonomy term. In my case I want to build the entire menu from just a single term in a single vocabulary. There will only be one of these terms attached to my node in focus. Whenever a given node of a specific type is in focus, I would expect to always find one and only one term from this given vocabulary on this node. So there is no list of terms to produce, but rather just a single term upon which the entire menu is based. Every item in the sidebar menu must now point to the same tid.
In other words, per my example, if the following are all true:
Then the resultant final urls within the menu might look like this:
http://mysite.coml/faq/101http://mysite.coml/recent-stories/101
http://mysite.coml/rules/101
If I change to a different node (page/panel) with the term lollipops with tid 202, then the resultant final urls within the menu might look like this:
http://mysite.coml/faq/202http://mysite.coml/recent-stories/202
http://mysite.coml/rules/202
If I wrote this as a custom module (as now seems likely) I would have a list of descriptions (FAQ, Recent Stories and rules) with urls missing only the taxonomy term tid (http://mysite.coml/faq/#REPLACE#, http://mysite.coml/recent-stories/#REPLACE# and http://mysite.coml/rules/#REPLACE#) and I would determine what that one taxonomy term tid was for the current node and then I would build my menu from this. It just seemed like the kind of thing that someone would have done before and I'd mostly rather use a decent module than write a new one (thus preserving the collaborative edge of Drupal).
Grant
Subscribe
I've often wanted the ability to make dynamic menu items through config as well, but like you I have always had to resort to custom module building. Your idea is the same one I have used in a similar situation.
(though mikey_p is right, if you don't absolutely need a menu, it becomes easier.)
I think calling it a menu (at
I think calling it a menu (at least as Drupal defines menus) is a bit of a misnomer as far as I can tell.
It seems that you just need to generate three links on each page, with the last href destination dynamically replaced by the current node's taxonomy term. I'll take a crack at this in pseudo code here:
<?php
$node = menu_get_object();
if ($node->taxonomy) {
$terms = array();
foreach ($node->taxonomy as $term) {
$terms[] = array(
'tid' => $term->tid,
'name' => $term->name, // Keep in mind this is not a safe string, make sure it is escaped before outputting
);
}
$items = array();
$items[] = l(t('FAQ about @name', array('@name' => $terms[0]['name'])), 'faq/'. $terms[0]['tid']);
$items[] = l(t('Recent stories about @name', array('@name' => $terms[0]['name'])), 'recent-stories/'. $terms[0]['tid']);
$items[] = l(t('Rules about @name', array('@name' => $terms[0]['name'])), 'rules/'. $terms[0]['tid']);
return theme('item_list', $items);
}
?>
Pasting this code into a block, or into a module to be used as a block should be pretty close to what you are looking for, if I am understanding you correctly.
On menus, I could go either
On menus, I could go either way, but overall, I believe this scenario would actually be best served if it created actual Drupal menus (similar to the taxonomy_menu module), which would bring a host of menu-handling modules into play (like nice-menus), but in my case I could get away with something simpler, like your thoughtful code sample.
On the code, agreed, though I usually resort to custom modules just to keep most of my code in SVN and out of the database, and in this case it might be better with options that can be different based on different scenarios (i.e. build different menus for different taxonomies/nodes/panels/etc). It just seemed like someone would have already written a module like this, or found a better way than just a custom chunk of code every time you need a menu like this. There are a host of modules that with build menus from your taxonomy terms, in one way or the other, but I found none that did something like this. I thought I might just have failed to adequately search for an existing solution, but if there is none then I certainly think there is a gap for it. Heh, I know, I know, it's a chance for me to maybe get my first shared module out there. Maybe... if I get the time.
Grant
On the menus thing, I should
On the menus thing, I should add that I have no idea how that would be accomplished, given that it would have to store a menu that only worked when the required taxonomy is in play. So yes, maybe a true menu module is a bridge to far.
My head hurts :P
Grant
Yeah, dynamic menu items
Yeah, dynamic menu items would be nice, but those were pretty much eliminated in the new Menu API in Drupal 6*.
The only exception to this would be if you wrote a module that defined some menus like faq/% and then defined a callback that would validate the appropriate argument, and embed a view on that page. Then you could use the menu_link system to reference paths like faq/123 or faq/456 without bloating the menu_router table. Getting those menu_links to display properly on each page could be extremely problematic however, and I can't think of a good way to do that, or automate the creation of all those menu_links.
Another option might be to implement some standard menu items, and then use hook_translated_menu_link_alter to make the argument a query/$_GET param that you parse out in your own module on the other end. You would still need a help function to get the correct term ID and you would need to make a module with the views embedded and parse the arguement there before passing to view_embed_view, but this *should* work.
*If a solution did exist the CSRF logout bug would have been fixed already.
Been thinking about this and
Been thinking about this and chatting with some folks in IRC, apparently this is possible, with the only caveat being that you can't edit the menu items with menu.module after creating them (i.e. you can't edit the item with the UI). I'll play with this some more and get back to you.
One more thing. Thanks for
One more thing. Thanks for the code sample. I always find something of interest in your code samples and I appreciate your generosity with your time.
Grant