Created
May 30, 2012 13:26
-
-
Save pixelwhip/2836327 to your computer and use it in GitHub Desktop.
Adding icons to menu links in Drupal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Custom implementation of theme_menu_link() for including icons. | |
*/ | |
function cic_menu_link__icon(array $variables) { | |
$element = $variables['element']; | |
$sub_menu = ''; | |
/* Prevent the <span> tag from being escaped */ | |
$element['#localized_options']['html'] = TRUE; | |
if ($element['#below']) { | |
$sub_menu = drupal_render($element['#below']); | |
} | |
$icon = '<span' . drupal_attributes($element['#icon_attributes']) . '></span>'; | |
$output = l($icon . $element['#title'], $element['#href'], $element['#localized_options']); | |
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n"; | |
} | |
/** | |
* Implements hook_preprocess_menu_link() | |
*/ | |
function cic_preprocess_menu_link(&$vars) { | |
/* Set shortcut variables. Hooray for less typing! */ | |
$menu = $vars['element']['#original_link']['menu_name']; | |
$mlid = $vars['element']['#original_link']['mlid']; | |
$item_classes = &$vars['element']['#attributes']['class']; | |
$link_classes = &$vars['element']['#localized_options']['attributes']['class']; | |
/* Set icon attributes */ | |
$vars['element']['#icon_attributes'] = array( | |
'aria-hidden' => array('true'), | |
'class' => array() | |
); | |
$icon_attributes = &$vars['element']['#icon_attributes']; | |
/* Add global classes to all menu links */ | |
$item_classes[] = 'nav-item'; | |
$link_classes[] = 'nav-link'; | |
switch ($menu) { | |
case 'menu-utility': | |
/* Add icons to all links in the utility menu. */ | |
$vars['theme_hook_suggestions'][] = 'menu_link__icon'; | |
/* Designate which icons to use for each menu link */ | |
switch ($mlid) { | |
/* RSS link */ | |
case '535': | |
$icon_attributes['class'][] = 'icon icon-rss'; | |
break; | |
/* Email Updates link */ | |
case '536': | |
$icon_attributes['class'][] = 'icon icon-email'; | |
break; | |
} | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment