7

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.

3
  • Are you talking of any menu link, or just to the menu local task links?
    – avpaderno
    Commented Aug 12, 2011 at 15:19
  • Custom menus, Main Menu, not local tasks.
    – Kevin
    Commented Aug 12, 2011 at 15:20
  • My comment from Sep 27 '13 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.
    – Gabriel R.
    Commented May 27, 2020 at 10:25

5 Answers 5

7

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.

http://api.drupal.org/api/drupal/includes--menu.inc/function/theme_menu_link/7

5

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.

5

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: "";
}
1

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.

2
  • 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
    Commented Aug 15, 2011 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
    Commented Aug 15, 2011 at 14:04
0

I recommend to use a plugin for this. Full tutorial here: Create a menu link with html markup in drupal 8

namespace Drupal\MYMODULE\Plugin\Menu;

use Drupal\Core\Menu\MenuLinkDefault;

/**
 * A menu link that displays number of points.
 */
class MyMessagesMenuLink extends MenuLinkDefault {

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    $count = 0;
    if(\Drupal::currentUser()->isAuthenticated()) {
      // Load in your count here
      ...
    }
    return $this->t('My messages <span class="badge badge-dark">@count</span>', ['@count' => $count]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return 0;
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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