3

I am on Drupal 7. On the body tag, I need to create a new class that will detect the current page's menu depth. The class could carry the ID of the menu, then something that would detect the current page's menu depth. It could be something like: "primary-depth-0" for the top menu items, then "primary-depth-1", "primary-depth-2" and so forth... Is it easy to code that on template.php? I assume I have to add a new item into the $classes array...

Thank you.

0

1 Answer 1

3

I did something similar in the past, but I can't remember on which project, so I don't have working code.

Inside a template_preprocess_html, you get the breadcrumb via menu_get_active_breadcrumb. You then walk the breadcrump keeping track of depth, comparing the paths against the current path. When you find a match, you end the walk and then

$variables['classes_array'][] = "menu-depth-" . $depth; 

I am pretty sure this is what I did.

3
  • Thank you! This works. Actually, I think $depth = sizeof($breadcrumb) will give me the depth number I need where $breadcrumb = menu_get_active_breadcrumb()... Is this right? I am using this and it works for me. Commented May 2, 2012 at 4:53
  • If it works, then it is right. Couldn't quite remember exactly what menu_get_active_breadcrumb() returned at the $breadcrumb. Commented May 2, 2012 at 10:42
  • It appears it returns an array with all the elements of the breadcrumb array. Commented May 2, 2012 at 21:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.