Skip to main content

Adding custom markup to Drupal menu items

An article from ComputerMinds - Building with Drupal in the UK since 2005
10th Dec 2007

Mike Dixon

Senior Mind
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

There are times when it's necessary to add some additional markup to a particular menu item, for example a link to DrupalMinds might need a strong tag around the Drupal portion.

This can be achieved in a couple of ways, both using a very similar theme override.

Option 1

The first method is to allow HTML in the title of all menu items - while this is nice and easy, it does mean that you have to manually escape special characters, such as quotes and pound signs. The code is below:


function phptemplate_menu_item_link($item, $link_item) {
  return l($item['title'], 
           $link_item['path'],

           !empty($item['description']) ? array('title' => $item['description']) : array(), 
           isset($item['query']) ? $item['query'] : NULL, 
           false, 
           true);
}

The trick is that final parameter on the l function, telling Drupal that the link is HTML.

Option 2

The second option is to only change the links that you need, by 'hardcoding' them into your template file. Whilst you loose some flexibility you no longer have to get your users to add their menu items in HTML.

The code looks like this:


function phptemplate_menu_item_link($item, $link_item) {

  if ($item['title']=='drupalMinds') {
    $item['title']='drupalMinds';
    $html=true;
  }

  $output = l($item['title'], 
           $link_item['path'],
           !empty($item['description']) ? array('title' => $item['description']) : array(), 
           isset($item['query']) ? $item['query'] : NULL,
           false,
           $html);
}

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.