Drupal trick: Returning a themed menu tree with nothing more than the system path

12.05.2007

I remember something a long time teacher said, "Nick, if you make a suit out of a gorrilla, the arms are too long." I forgot why that was relevent to the topic of theming menu trees.

Moving on, here's a nice little function I wrote to return a themed menu tree by path.

<?php
// will return all menu items under "administration".
print theme('menu_tree_by_path','admin');

// will return links to all node submission forms
print theme('menu_tree_by_path','node/add');

// return the correct menu array by path
function menu_get_mid_by_path($path) {
// oddly, menu_get_item accepts a path, but returns the parent id.
 
$menu = menu_get_item(null, $path);
  if (isset(
$menu['children'])) {
// so we have to extract the mid for theme_menu_tree from one of the child items
   
if ($pid = end($menu['children'])) {
     
$menu = menu_get_item($pid);
      return
$menu['pid'];
    }
  }
}

//theme the crap out of it
function theme_menu_tree_by_path($path) {
  if (
$mid = menu_get_mid_by_path($path)) {
    return
theme('menu_tree', $mid);
  }
}

// throw a sexy party....
?>

Comments

I've stumbled upon this post

I've stumbled upon this post a couple of times from different websites..
I'm looking for a solution to a simple problem in drupal 6, that i just can't fix.

I'm trying to generate a doormat navigation for my website
http://www.welie.com/patterns/showPattern.php?patternID=doormat

using primary links, but for the life of me i can't get anything to display part of the menu tree. i've tried various methods and keep coming up blank. the CSS i can handle no problem.

My primary links are as follows

  • item 1.0
    • item 1.1
    • item 1.2
    • item 1.3
    • item 1.4
  • item 2.0
    • item 2.1
    • item 2.2
    • item 2.3
    • item 2.4
  • item 3.0
    • item 3.1
    • item 3.2
    • item 3.3
    • item 3.4

I can get the primary menu to display the top level hierarchy, but never the expanded list.
any ideas?

Yeah, go to that

Yeah, go to that admin/build/menu page and edit items X.0 and select "expanded". That will cause the menu to always show expanded.

Drupal 6...

I wish that returning an un-themed menu structure based on a path would be possible with the new menu system in D6... admin_menu needs your help!

Nice bit of code. I never

Nice bit of code. I never really comment my code as detail as that, may be I should start doing that. Thanks

Always comment on your code.

Always comment on your code. For yourself and for the others.

Nice!

Thanks for the tips :)

Nice tip.

The gorilla suit would fit me though. The girl who measured me for a tuxedo kept saying I had the longest arms she'd ever seen.

Hmm

Well that might be a "Good Thing". Long arms at a sexy party means you don't have to bend *completely* over to do up your shoelaces, if you get me.

True, but gorilla arms still

True, but gorilla arms still cause some pretty bad lower back issues.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.