Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By modjodandy on
Drupal generate menus like this :
<ul class="menu">
<li>
<a href="#" >ITEM 1</a>
<ul>
<li><a href="#" >subitem 1</a></li>
<li>
<a href="#">subitem 2</a>
<ul>
<li><a href="#">subsubitem 1</a></li>
<li><a href="#">subsubitem 2</a></li>
<li><a href="#">subsubitem 3</a></li>
</ul>
</li>
<li><a href="#">subitem 3</a></li>
</ul>
</li>
<li>
<a href="#">ITEM 2</a>
</li>
<li>
<a href="#">ITEM 3</a>
</li>
<li>
<a href="#">ITEM 4</a>
</li>
</ul>
Is it possible to add a hook in my template.php in order to add a div around menu's children?
<ul class="menu">
<li>
<a href="#" class="home">ITEM 1</a>
</li>
<li>
<a href="#" class="products">ITEM 2</a>
<div class="sub">
<ul>
<li><h2><a href="#">SUBITEM 1</a></h2></li>
<li><a href="#">subsubitem 1</a></li>
<li><a href="#">subsubitem 2</a></li>
<li><a href="#">subsubitem 3</a></li>
<li><a href="#">subsubitem 4</a></li>
</ul>
</div>
</li>
<li>
<a href="#" class="community">ITEM 3</a>
</li>
<li>
<a href="#" class="store">ITEM 4</a>
</li>
</ul>
I searched for a while in the drupal API website but, theme_menu_item and theme_menu_tree don't seems to do the job.
Thanks for your help!
Comments
You can do this using a hook
You can do this using a hook I think but I don't know how.
But an alternative is to edit the theme template file (this will be something.tpl.php ) by copying the relevant system css file into your theme folder and editing it directly.
I quested for that template
I quested for that template file, but drupal won't let me do the trick because it can't make the difference between a parent and a child.
Somewhere in the menu.inc file, sleep a magic class, that generate menu tree like i want to, but i can't find it anywhere.
I read a good part of the Drupal API, but i feel kind of lost T_T
div can be added in
div can be added in theme_menu_item hook, like this:
To add a div around children in Drupal 7
In Drupal 7, Use theme_menu_link to add a div around children. Refer below code :