I am new to Drupal and am porting a Joomla site to D7, whereby the Joomla site has multi-line menu items with the second line italiziced. I am using Views for my pages and have created the main navigation menu using Admin/Structure/Menus/Main Menu.

The html in the Joomla site looks like this:

<div id="main-menu">
<ul>
<li class="level1 item1 active current">
<a href="/page-1">
<span class="title">Menu Item Title</span>
<span class="subtitle">Menu Item Subtitle</span>
</a></li></ul></div>

The pertinent css for breaking the line and italicizing:

#main-menu  span.subtitle {
	font-style: italic;
	display: block;
}

I've researched many posts and many approaches for wrapping menu items in span tags and modifying menu item attributes. However, I have only found one approach for allowing inline html in menu items. It entails adding this code to template.php:

function MYTHEME_menu_link__MENUNAME($variables)
{
    $element    = $variables['element'];
    $sub_menu   = '';
 
    if ($element['#below']) {
        $sub_menu = drupal_render($element['#below']);
    }
 
    // Enable this to use html in title, eg img, span or something else...
    $element['#localized_options']['html'] = true;
 
    $link = l('<span>' . $element['#title'] . '</span>', $element['#href'], $element['#localized_options']);
 
    return '<li' . drupal_attributes($element['#attributes']) . '>' . $link . $sub_menu . "</li>n";
}

However, I can't get this to work in D7. I have pasted the code into template.php in my theme. In the numerous posts I found relating to this, there is no indication that any other action or hooks are required. I found an older post indicating an issue with this function in conjunction with Views but have found no such posts for D7. I have flushed all caches using Drush and Admin but any HTML I include in the menu text is still output as a string, not parsed as code. I guess I could put a placeholder after the Menu Item Title, then loop through each item, replacing the placeholder text with </span><span class="subtitle"> on the output side to get the internal HTML but that seems like overkill...is there a better, easier way?

I've spent at least two days trying to achieve something that should be quite trivial and would greatly appreciate your assistance beyond just pointing me to the API, since I am obviously missing something basic. It would seem that the ability to break lines, include entities and vary formatting within a menu item string would warrant an easy solution, particularly for localization. Thanks in advance for your help.

Vana

Comments

payamspot’s picture

I suggest using a module (e.g. Superfish Menu) to do this.

Vana’s picture

Thanks, Superfish does as I described by copying the Title attribute of the link into a span tag on the output side. Works great, no coding required aside from the css to style the output.

Still, I'd be curious if there is another way to include HTML in the menu links and what I was doing wrong wit the script above. The only downside of the Superfish module is that it uses the Title attribute for the subtitle under the main menu item, so the title can't be used for ToolTip, hover text, or longer SEO strings.

Thanks again!

vana

payamspot’s picture

You are welcome!

I have no idea what may be wrong with your code, but make sure the script loads in your site at all.

xalexas’s picture