I have certain menu items where I'd like to include a line break between words (its a horizontal Nice Menu).

I'm hooked in using the phptemplate_menu_item_link() function in my template.tpl.php and am able to manipulate the title text, but if i stick any HTML in the title, the HTML doesn't get parsed, it just shows up in the menu title (like ABOUT<br />US). This is no different than the behavior of just including HTML in the menu title via the user interface.

Any ideas? Thanks!

Best,
j

Comments

choster’s picture

Rather than using markup, I would limit the width of the menu item itself and rely on "natural" word-wrapping. It could be as simple as

ul.nice-menu li {width:8em;}

to set a width for all menu-items, or you can specify widths for specific menu items using the id attributes of each list item.

jruberto’s picture

Agreed, and I've had some success like that, but it needs to be manageable by the client via the Drupal user interface.

The best thing I can come up with for that is inserting a token in the menu title to cause a line break. Another thing that's in play is multi-lingual menus... it will be different from language to language, it would be better if the menu title drove the width of its container, rather than the opposite.

Thanks & let me know if something else comes to mind! --j

Fiable.biz’s picture

How to attribute ids to menu items? I get classes, not ids, like this:

<ul class="nice-menu nice-menu-down" id="nice-menu-1"><li class="menu-947 menu-path-node-4  first   odd  "><a href="/home.mn" title="Нүүр хуудас">Нүүр</a></li>
<li class="menu-954 menu-path-node-5   even  "><a href="/presentation.mn">Танилцуулга</a></li>
<li class="menu-955 menu-path-node-6   odd  "><a href="/oganization.mn">Бүтэц тогтолцоо</a></li>
<li class="menu-956 menu-path-legalinfomn-   even  "><a href="http://www.legalinfo.mn/" title="Монгол улсын хууль тогтоомжийн албан ёсны сайт.">Хууль тогтоомж</a></li>
<li class="menu-957 menuparent  menu-path-#   odd  "><a href="http://#" title="">Үйл ажиллагаа</a><ul><li class="menu-958 menu-path-node-7  first   odd  "><a href="/social.mn">Нийгэмшүүлэх ажил</a></li>
<li class="menu-961 menu-path-node-8   even  "><a href="/manufacture.mn">Үйлдвэр</a></li>
<li class="menu-962 menu-path-node-9   odd   last "><a href="/construction.mn">Барилга</a></li>
</ul></li>
(...)

This said, I notice that each item gets a different class, so they can be used as ids. Since the menus are themselves numbered, we can even apply different styles to copies of a same item in different menus.

http://Fiable.biz Web site creation.

kunalmaity’s picture

As your requirement we want to give
in menu title.......
No problem when you add a menu from admin pannel you just give
with the menu title(i.e. about
us) and save it...

Now go to includes folder and open the file menu.inc and then search the string
"function theme_menu_item("
and add one line before return statement...
" $Menutag = str_replace("<br>", "
", $link);

and in return statement replace $link with $Menutag

It will work for drupal 6.x

vm’s picture

hacking core is bad practice. When that file is updated, you will have to remember to make the change again. The more changes you make the more difficult it becomes to upgrade.

kunalmaity’s picture

You are right and for that reason I mention drupal version

kunalmaity’s picture

As your requirement we want to give
in menu title.......
No problem when you add a menu from admin pannel you just give
with the menu title(i.e. about
us) and save it...

Now go to includes folder and open the file menu.inc and then search the string
"function theme_menu_item("
and add one line before return statement...

$Menutag = str_replace("&lt;br&gt;", "<br>", $link);
and in return statement replace $link with $Menutag

It will work for drupal 6.x
Please igone my previos post. follow this replay...... :)

vm’s picture

That is also hacking a core file.
theme menu item() should likely put put in template.php to override core

jruberto’s picture

nice, nice solution. i haven't tried it, but well done. (i wound up handling my case with CSS -- but i'll use this in the future)

yes, rather than changing it in the core file, you can override in template.php

thanks!

p.s. that should be a <br/> to have valid XHTML

johnvgross’s picture

I tried the str_replace, Its doing for the menu items present on that selected menu,
But its not replaceing the parent menu name.

Is anybody facing the same problem like me?

photoshop_nerd’s picture

The core hack to menu.inc works, but would like to try the better way.
when trying to add the modified function theme_menu_item to template.php I get the error:

Fatal error: Cannot redeclare theme_menu_item() (previously declared in mypath\includes\menu.inc:1149) in mypath\sites\all\themes\fusion\fusion_core\template.php on line 484

Any help on where and how to add this to template.php would be appreciated
thanks

jruberto’s picture

name the function mythemename_menu_item with "mythemename" replaced with the name of your theme. depending on the theme, there may be further documentation in the comments in your template.php (zen theme is awesome in this regard)

drupalmonkey’s picture

jruberto: you said you ended up solving this problem using CSS, could you explain how you did it please?

jruberto’s picture

Yes, by carefully managing the width of individual menu item containers with CSS, and letting the browser do the line-breaking. I believe I also had to adjust the vertical padding or margins of those items to get them to center vertically. Not a particularly maintainable solution but got the job done.

Cheers, j

awasson’s picture

As I understand it the best way to make adjustments to core functions (unless you're creating custom modules) is to override the function you're after within your theme's template.php... Drupal 7 is a little different than Drupal 6 so here is a chunk of Drupal 7 specific code that you can add to template.php to replace html escaped line breaks in your menu link titles with real html line breaks.


/**
* Override theme_menu_link() to add line breaks in menu items
* that have a <br> or <br/> or <br />  placed in them.
*/
function YOURTHEME_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);

  // Replace text line breaks with html line breaks. 
  $output = str_replace("&lt;br&gt;", "<br/>", $output); 
  $output = str_replace("&lt;br/&gt;", "<br/>", $output);
  $output = str_replace("&lt;br /&gt;", "<br/>", $output);
  
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

I just added this to a site I'm working on and it works flawlessly : )

Cheers,
Andrew

tsjuster’s picture

This ist't working for me. I tried this code to my template.php file. Also tried
// Replace text line breaks with html line breaks.
$output = str_replace("<br>", "
", $output);
$output = str_replace("<br/>", "
", $output);
$output = str_replace("<br />", "
", $output);
to includes/menu.inc (I know this is wrong, but I want to try out if it works then)

I use drupal 7. What do I have to place in the menu? I tried:
about<br>us
about
us

But with no result

Can anybody help me with this?

Thanks

vm’s picture

after altering template.php did you clear the cache , in an effort to clear the theme registry and have the new changes to template.php picked up?

LaurenW’s picture

I did clear my cache, but it does not seem to do anything.

Thanks,
Lauren

vm’s picture

https://api.drupal.org/api/drupal/includes!menu.inc/function/theme_menu_... indicates that the code snippet above should have worked. The only other reason I can think of that it wouldn't work (assuming the code is correct) is that YOURTHEMENAME wasn't changed in the function to the name of your theme.

langweer’s picture

This works fine and solves the problem mentioned in the original post - at least it does in my case.
And I am so happy we have this archive to fine stuff 2 years old.
Thanks - Andreas

cedric_a’s picture

For those saying that it doesn't work, try to enable the "Primary menu" block. I figured that it works only with the menu blocks and not with the theme menus (primary and secondary menu's displays provided with your themes)...

peterconnolly’s picture

See http://www.kpdirection.com/wordpress/drupal/putting-a-line-break-in-a-dr... for a breakdown of how to do it in Drupal 6 without hacking core

tammo’s picture

The link *did* not respond, but now it does. Great!

Tools for Research
http://www.toolsforresearch.com (international)
http://www.kleinestappen.nl (NL)

All using open source software: Drupal, Limesurvey

dolen’s picture

I am using Drupal 5, how i am going to modify this? Please reply asap. Thanks

datadl’s picture

Hello,

I tried using the Drupal 7 solution explained here, but it never worked for me, and I think this is too much process, so that could take long time to render.
I modified the file /includes/menu.inc on line 695 ($item['title'] = $item['link_title'];) and added just before:

$item['localized_options']['html'] = TRUE;

I know this is a modification of a drupal core file, but this works.

I hope this can help.
Cheers
V.

awasson’s picture

@datadl, it's never a good idea to hack the core... I've done it (once) and I've been bitten by it a few times because others have decided to do it.

You can either do it with the code I posted here... You put it in template.php in your theme directory and rename the function YOURTHEME_menu_link(array $variables) so that YOURTHEME is the name of your theme. It does work.

Or, you can write a module to do the same thing.

Good luck!

Andrew

Nel_2’s picture

Hi,
this works perfekt for me with the menu - but what do I do if I wan't to have a line break in the superfish menu???

Thanks, Nel

devad’s picture

---------------------------------------------------
Drupal developer. Available for new projects