By jruberto on
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
use css
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.
yep that can work but..
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
id or classes
How to attribute ids to menu items? I get classes, not ids, like this:
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.
<br> will work in menu title
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
=-=
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.
You are right and for that
You are right and for that reason I mention drupal version
As your requirement we want
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>", "<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...... :)
=-=
That is also hacking a core file.
theme menu item() should likely put put in template.php to override core
nice, nice solution. i
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 XHTMLI tried the str_replace, Its
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?
how to override in template.php
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
name the function
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)
jruberto: you said you ended
jruberto: you said you ended up solving this problem using CSS, could you explain how you did it please?
Yes, by carefully managing
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
Adding Line Breaks to menu items in Drupal 7
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.
I just added this to a site I'm working on and it works flawlessly : )
Cheers,
Andrew
This ist't working for me. I
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
=-=
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?
I did clear my cache, but it
I did clear my cache, but it does not seem to do anything.
Thanks,
Lauren
=-=
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.
Great, thanks a lot!
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
Works with menu blocks not with theme menus
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)...
Drupal 6 Solution
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
Your link does not respond
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
I am using Drupal 5
I am using Drupal 5, how i am going to modify this? Please reply asap. Thanks
Drupal 7 Solution
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.
@datadl, it's never a good
@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
Line break with Superfish Module (Drupal 7)
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
You can also use https://www
You can also use https://www.drupal.org/project/menu_html
KOBA - Drupal Webdesign & Webdevelopment
For D9+ You can use
https://www.drupal.org/project/menu_item_extras
---------------------------------------------------
Drupal developer. Available for new projects