Skip to content

Instantly share code, notes, and snippets.

@pixel5
Last active July 15, 2016 19:52
Show Gist options
  • Save pixel5/30089678e2f08357f039f4a4add3a368 to your computer and use it in GitHub Desktop.
Save pixel5/30089678e2f08357f039f4a4add3a368 to your computer and use it in GitHub Desktop.
Drupal Bootstrap Menu Link Badges
<?
function MODULE_preprocess_menu_link(array &$variables) {
// Set your menu item here. Use the path you'd use to get to it via URL, no preceding slashes!
$link_path = 'path/to/item';
if ($variables['element']['#original_link']['link_path'] == $link_path) {
// This is where you get the number or string that shows up in the badge
$badge_count = MODULE_badge_count();
if (!empty($badge_count)) {
// Tell Drupal that this menu link contains HTML
$variables['element']['#localized_options']['html'] = TRUE;
// Create the HTML to be appended to the menu link
$badge = '<span class="badge">' . $badge_count . '</span>'; // Badge HTML to add to menu item
// Take the current menu link and set it equal to itself + the markup for the badge
$variables['element']['#title'] = $variables['element']['#title'] . ' ' . $badge;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment