1

This should really be a basic question but I simply don't get it after hours of searching. The question is, how do I theme menu blocks in Drupal 7?

I've created three different blocks all based on the main menu. Now I want to:

  1. create unique HTML for all three blocks, that means modifing the surrounding wrapper and the <ul> and <li> that builds the menu. I wanna set special classes and remove all of the Drupal-added stuff

  2. attach different classes to the different levels within each block. One of blocks will show two levels of the menu, i.e. it will display a submenu. I want to set a special class on the for the submenu...

This seems impossible... :(

Thank you in advance for the help!!!!

apaderno
  • 28,547
  • 16
  • 75
  • 90
Joel
  • 3,166
  • 5
  • 24
  • 29
  • 1
    Exact question on Drupal Answers: http://drupal.stackexchange.com/questions/7274/how-to-theme-a-menu-block – claws Feb 24 '14 at 08:11

3 Answers3

8

Theming is a tricky beast that often varies a lot depending what you need to do. Even with your very detailed description I can still say "it depends", but here are a couple steps that may help you get pointed in the right direction.

Step 1: Use a block tpl.php as suggested by Caffeine Addict. If you're not sure what to name the .tpl.php, I recommend the Theme Developer module. It's buggy, but you can use it to select an particular element and have it tell you suggestions for naming of .tpl.php files.

Outlining candidate template files from devel themer

Step 2: Use a theme / preprocess function in template.php to modify the pre-defined variables and markup. Be sure to check on the theme_menu_tree & template_preprocess_menu_tree functions on api.drupal.org for starting points. If you're using the devel module, use dpm($variables); in each of those to see what you have to work with from the start.

I hope that helps! I agree with Caffeine Addict when he says that superfish might be an alternative. You should also probably check out the menu block module for breaking out conditional sub-sections into their own blocks.

davidneedham
  • 378
  • 2
  • 9
1

In addition to what davidneedham said, to change what Drupal added to your menu HTML tags, you can override them. Here it is added classes:

<ul class="expanded">
  <li class="firstleaf">...<li>
  ...
</ul>

i did not find a way to remove this classes, but you can override them in your block--system--main_menu.tpl.php file, like this:

li.expanded,
li.collapsed,
li.leaf {
  padding: 0 0 0 0;
  margin: 0;
}
ul.menu li {
  margin: 0 0 0 0;
}

and then print your menu content:

<?php echo $content; ?>

I'm new in Drupal, wish my post can help you! :)

Jon Surrell
  • 9,444
  • 8
  • 48
  • 54
simin
  • 29
  • 7
0

I would suggest to start with installing the Zen theme and follow the instructions inside the theme to setup a starter sub theme. This has all the information needed to learn theming in drupal and even how to add your own stylesheets etc.

This will allow you to start editing the templates for menu blocks and set your own html wrappers and classes.

For setting extra classes on blocks i would use this module: http://drupal.org/project/block_class

Then just edit the block and you will see an extra section for adding additional classes to the block.

Kevin Andrews
  • 236
  • 3
  • 7
  • Hi, I know themeing...it's just that menu blocks doesn't seem to follow the standard .tpl.php files stuff...What I want to know is how to modify the HTML that the menu block outputs... – Joel Aug 13 '12 at 13:53
  • Ah ok how about trying a region block override template? e.g block--[regionname].tpl.php – Kevin Andrews Aug 13 '12 at 14:03
  • Sadly the block tpl only lets me create the html for the weapper, so one thing is solved. I still have the issue of reaching the HTML for the UL LI that the menu creates. Here I want to somehow loop thru the menu and add different HTML based on what level I'm at in the hierarchy... – Joel Aug 13 '12 at 14:25
  • Try installing the superfish module, that will give you a tone of styling options when setting up a superfish menu block, including odd and even classes and a fair few styling points to target. It also has a html wrappers section which should solve your problem... – Kevin Andrews Aug 14 '12 at 12:31