I have installed the plugin Menu block and I am very happy with the result, but I'd like to change some classes in the structure.
This is what I get right now:
<div class="content">
<ul class="menu">
<li class="first last expanded">
<a class="active" href="http://example.com">News</a>
<ul class="menu">
<li class="first last leaf">
<a class="active" href="http://example.com">Test</a>
</li>
</ul>
</li>
</ul>
</div>
..and this is what I want:
<div class="content">
<ul id="nav-list">
<li class="first last expanded has-submenu">
<a class="active" href="http://example.com">News</a>
<ul class="sub-menu">
<li class="first last leaf">
<a class="active" href="http://example.com">Test</a>
</li>
</ul>
</li>
</ul>
</div>
So I basically want to..
- ..add/change/remove the
<ul>
's class and id attributes (level specific, so not the same for the primary/secondary<ul>
). - ..add a
has-submenu
class to an<li>
if it contains another submenu.
What I've tried:
As explained in Menu Block's documentation, a hook function hook_menu_block_tree_alter()
can be implemented. To start 'somewhere, I tried implementing this in template.php without any success (but doesn't even get called):
function THEME_menu_block_tree_alter(&$tree, &$config) {
foreach ($tree as $key => &$value) {
if ($tree[$key]['link']['access'] && !$tree[$key]['link']['hidden']) {
$items[] = $tree[$key];
}
}
$num_items = count($items);
foreach ($items as $i => &$data) {
$class = array();
if ($data['link']['has_children']) {
$class[] = 'has-submenu';
}
$element['#attributes']['class'] = $class;
}
}
Any help would be much appreciated as I don't really have deep understanding of how and where to implement this hook function. Also my impression is, that some more detailed documentation is missing for this specific hook function..
THEME
with the short name of the theme? If THEME is not the short name of the theme, that hook will never get invoked. – kiamlaluno♦ Jan 7 '15 at 7:05var_dump($example)
ordrupal_set_message("Example")
.. I decided to go the way @sareed explained in his answer below, but still..shouldn't this be working? And is template.php the place to put it or am I doing something wrong? – James Cazzetta Jan 7 '15 at 19:39