Author image
Senior Mind

Adding custom markup to Drupal menu items

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']='<strong>drupal</strong>Minds';
    $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);
}