12

I have a main menu, with a top-level structure similar to the following:

Home - Categories - Something Else - ... - Contact

Each of those top-level links goes to an appropriate node page.

I have a vocabulary for Categories (multi-level), and I need to get those taxonomy terms in a sub-menu under the "Categories" item. I'd like to avoid hard-coding the items into the main menu, as the sub-menu should grow/shrink as the vocabulary does.

In the past I've written over-elaborate code to accomplish this, which hooks into taxonomy operations and manages a particular menu's sub-items based on that (very similar to what Taxonomy Menu does, but without having to have a different menu for each vocabulary).

While that method works, it's not ideal and certainly not generic. I can't just lift that code from the old project into the new one, it would require a reasonable amount of customisation.

I can't help feeling there must be a 'standard', module-based solution to this.

I've seen some suggestions out there to use a bunch of different modules in combination (even some suggesting to use Views-based modules, weirdly enough). I'm open to any suggestion, but if it involves installing more than 1 or 2 modules, and a load of hard-coded, context-specific configuration, it's probably not going to help too much in this case.

  • 1
    I think I've seen it... Similar topic contains idea about using rules to create a taxonomy-based items in menu, but that seems far from ideal, too. – Mołot Jun 13 '13 at 11:29
  • If i understood correctly, are you looking similar like this module drupal.org/project/taxonomy_treemenu , it can integrate with nice menu but for D6 – Bala Jun 18 '13 at 11:21
  • Do you have to have actual menu items or is it viable to add the tree of taxonomy terms into the menu when it is rendered? – rooby Jun 18 '13 at 11:39
  • Thanks @Bala, need a D7 solution though – Clive Jun 18 '13 at 12:34
  • 1
    Clive I have one suggestion. As I know you can generate taxonomu menu with vocabulary name included and then just add other menu links directly in into generated menu. Taxonomy items might be updated authomatically... I don't know if it'll work, just try it. – milkovsky Jun 19 '13 at 14:00
13
+200

Option a) Unless I am mistaken, can you not just pick where your Taxonomy Menu will be parented (Menu Location) when you create it? The only problem I think you might get with this is I think it requires you to rebuild the menu every time the taxonomy is updated

Option b) I have done something reasonably similar to this on my site, I used Menu Views module, to allow me to put a view into my primary menu.

If you create a new view which lists all of your taxonomy terms, as a block display, apply any additional filtering/modification in the view if necessary (you may not but in my example I wanted to). Then when you add a new link to your main menu, you can select to use a view rather than a link. From there you pick which view you want to use, select the 'Category' menu item as its parent and save (after saving you then have to enable it in the menu, which appears to be a bug with the module itself)

I can't guarantee that this is the best solution for you, but it worked when I just tested it

  • 1
    Yes, turns out I've been rather stupid - Taxonomy Menu has this exact functionality built right in (your option a). The particular menu I'm dealing with is pretty small, and the categories won't change often, so rebuilding isn't a problem. Many thanks for the sanity check – Clive Jun 21 '13 at 15:55
3

Menupoly has an API for custom "menu tree sources". I have never really made use of this for something other than menu links, so there might be issues we have to fix on the way. But the basic architecture is there.

If you go this route, you would

  • Use your existing main menu, without any taxonomy term links beyond the "Categories" page.
  • Create your taxonomy tree independent of that.
  • Write a custom "menu tree source" that fetches from menu_links as normal, but when it finds a taxonomy term, it fetches from taxonomy instead.
    So, somehow you need to be able to detect that the "Categories" link is taxonomy-related.
  • If a menu link is taxonomy-related and at the same time has a regular submenu, you need to somehow mix the two types of children together.

This route would be some work, but it might be useful for others, and it is probably more elegant and reusable than your site-specific logic.

Currently not even a taxonomy-only "menu tree source" exists, so maybe that would be a reasonable first step before starting with a mixed plugin.

  • this being said, the accepted answer by Andrew Morris might be perfectly good enough. And it means less work for me :) Still wanted to post this for anyone who might find it useful. – donquixote Jun 22 '13 at 13:52
  • V. interesting, thanks. The accepted answer is doing the job just fine but I'm very interested to get code solutions too. Can I assume if it's one of your modules that it's all built on PSR-0 goodness? :) – Clive Jun 22 '13 at 13:54
  • This one uses the PHP 5.2 compatibility mode of xautoload, which is somewhat like PEAR but with shallow directories. So, not PSR approved. I might go PSR-0 some day.. – donquixote Jun 22 '13 at 14:27
  • although i am very interested in PSR-X (which might never get into D8, sadly) – donquixote Jun 22 '13 at 14:30
2

Metro Menus will do your requirement and even it have the built in skins and can create your own which act same like nice menus. With this module you can select any sub items with any depth from menu or taxonomy and I'm not using this before.

Compatibility with touch screen (iPad, iPhone and etc.).

enter image description here

2

You can use Om Maxi menu module for the same. It will allow admin to create menus with blocks you want as attachments, modules like views, slideshow, menu, user, nice menus, quicktabs, and custom blocks with tables, lists, images, videos, etc.

You can create a separate menu using Taxonomy Menu and can attach this menu as sublinks to menu generated by om maxi menu.

Thanks

2

I have recently done something similar for a product category menu. This was only for 1 level deep, but could easily be modified to show further levels.

/**
 * Implements theme_menu_link().
 */
function THEME_menu_link__main_menu($variables) {

  $sub_menu = '';
  $element = $variables['element'];
  $menu_string = $element['#title'];

  if (module_load_include('inc','pathauto','pathauto') !== FALSE) {
    $menu_string =  pathauto_cleanstring($menu_string);
  }

  if ($menu_string == 'categories') {
    $sub_menu = get_categories_menu();
  }

  if ($sub_menu) {
    $element['#attributes']['class'][] = 'haschildren';
  }

  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

function get_categories_menu() {

  $current_category = arg(2); // Get current category from path
  $volcab = taxonomy_vocabulary_machine_name_load('product-categories');
  $tree = taxonomy_get_tree($volcab->vid,0,1);

  $output = '<ul class="menu product-category">';
  foreach ($tree as $term) {
    $safe_term = pathauto_cleanstring($term->name);
    $class = ($current_category==$safe_term) ? ' class="active"' : '';
    $output .= '<li><a' . $class . ' href="/path/to/' . $safe_term . '">' . $term->name . '</a></li>';
  }
  $output .= '</ul>';

  return $output;
}

The get_category_menu function is better off as a block, but shown here as another function in the template.php for convenience.

Hope it helps or is at least a step towards a solution.

Paul

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.