views:

483

answers:

3

I'm using the menu_block module in Drupal for my menus. This works very well, but I want my menu items styled as an image menu. This cannot be done nicely with the default settings - the menu items look something like this:

<li class="leaf first menu-mlid-199"><a href="/this/is/some/nice/url" title="Homepage">Homepage</a></li>

I'm guessing I could used the menu-mlid-199 class to get the styling I want, because it's a unique id of each menu item, but that seems rather ugly to me. Is there any other way to add reasonably named classes to my menu items, e.g. generate them from the page title or url alias? Even just a sequence would seem nicer - like menu-item-1 and so on.

+2  A: 

While your question is very clear about what you want to know (see below) it is not 100% clear to me the kind of functionality you want to achieve. So I just thought to start off by mentioning the image menu module, in case that module goes any close to what you need. Another alternative could also be the menu icons module. Other custom solution to similar problems have been discussed here.

As far as changing class attribution to menu items: classes are attributed to the menu item by theme_menu_item(). You could simply override that function from your own theme. An explanation on how to override themeable functions is given in the official drupal documentation.

mac
Basically, I have this website, which has one menu on the left containing only the top-level pages and a contextual menu on the right, which displays additional menu items relevant to the page visitor is currently looking at. This can be done nicely using menu_block. Now I'd like to be able to skin the "static menu" on the left, which contains about 6 items. Set some backgroung images, do rollovers and stuff. For that I need to be able to pin point each of the menu items with a css selector. Does image menu solve this use case? Sorry, I'm a drupal noob :-)
VoY
Image menu and menu icon (of which I added the reference in the main answer right now) allow you to create menus with images / icons. I frankly do not remember if and how they add classes to the generated HTML, but I would imagine they don't, at least not in the way you would need. You can give it a shot though (you can disinstall them if they have no use for you). I mentioned them just in case you needed classes just for the sake of images, but if you really need to change the CSS then I would rather go for overriding the theming of menu items.
mac
A: 

When theming module output, always look at the .tpl.php files that are in the module directory. In this case the .tpl.php file is menu-block-wrapper.tpl.php. Copy that to the folder of the theme you're using and adapt it as needed.

Also there's some good info on styling the menu blocks in the README.txt file contained in the module package.

Practically every piece of HTML Drupal outputs is themeable, so read up on theming and how to override the function that prints out the list of menu items. If you install the Devel module you can see which functions create that list. You can then assign extra classes or id's to those items so you can theme them with CSS as much as you want.

Good luck!

Niels Bom
I might be wrong, but from the comment of the OP to the other answer, I understood that what he is trying to achieve is to style the "static menu" which I understand is *not* the one done with menu_block but the standard one (primary or secondary links?). Also: not all theming is done via template, it could well be done via function, which is in fact the case for the menu core module. Finally (and most importantly) the template you are suggesting to modify gets the menu items as an unordered list, which would require you to process HTML instead of variables: a rather inelegant solution.
mac
I could well have misunderstood OP's question. If that is the case, then indeed my answer can be ignored. I don't quite understand your last comment, would you be so kind to elaborate?
Niels Bom
@Niels - What I mean, is that the template file you are suggesting to use, receives the menu as HTML already, so - in order to modify classes - you will need to write code that reverse-engineer the HTML into individual items and then does some string manipulation on HTML directly. If you would override theme\_menu\_item() instead, you would just need to concatenate one function to the line generating the classes; you would also receive into the function all the parameters you might need to decide the name of the class (e.g.: the mlid), without having to dig them out of HTML.
mac
@mac I agree, your solution is the better one.
Niels Bom
+1  A: 

I have changed a line

in menu_block.module:

(639) $extra_class[] = 'menu-mlid-' . $items[$key]['link']['mlid'];

into

$extra_class[] = 'menu-href-' . str_replace('/','-',$items[$key]['link']['href']);

and it works ok for me, it does not use the mlid (which I generally do not know), but the path (which I do know)

michal