In Drupal 7, how I can intercept menu rendering in template.php and add pipes after menu links?

I tried using template_preprocess_menu_tree but I don't think that is what I am looking for. I want to change the HTML structure of a couple of menus.

  • Are you talking of any menu link, or just to the menu local task links? – kiamlaluno Aug 12 '11 at 15:19
  • Custom menus, Main Menu, not local tasks. – Kevin Aug 12 '11 at 15:20

theme_menu_link() did the trick. It contains an array under element of #original_link which has the menu name in it. Using that, I can add pipe delimited links to certain menus.



This may be helpful. You can use preprocess functions to change or add something to menu link.

function THEME_preprocess_menu_link(&$variables) {
  $variables['element']['#below']['#markup'] = ' <span>→</span>';

In this code span added to every navigation link item.


Personally I'd just add the pipe with CSS - you don't really want the pipes in your markup as they're purely presentational:

.breadcrumb li:after {
  content: "|";
  margin: 0 5px; // optional styling nicety

.breadcrumb li:last-child::after {
  content: "";

Menus are themed with the theme_links function. You'll want to create a copy in your [themes folder]/MYTHEME/template.php and rename it to MYTHEME_links. You can find the default function for drupal 7 by visiting: http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_links/7

Now you'll be able to customize how the menus look from the template.php file.

  • This isn't working either. It only dumps the contextual links (using krumo and var_dump on $variables). This is what I used in D6 pretty sure, but doesn't seem to touch menus in a menu block. – Kevin Aug 15 '11 at 13:35
  • I needed to only do this to certain menus, this answer helped. theme_menu_link contains an array about the menu it is in, so I could then adjust the output if it was a particular menu. stackoverflow.com/questions/2317462/… – Kevin Aug 15 '11 at 14:04

Using HTML in a menu option would be very useful from a module too, i.e. inserting an image as a menu option. Is there a way to do use something like function MODULE_preprocess_menu_link(&$variables).

PS: I'll ask this as a separate question too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.