Snippets

Jamie Scott Drupal manipulate menu items and links

Created by Jamie Scott
/**----------------------------------------------------------------------------
* Change the output of the Footer Utilities Menu
 ----------------------------------------------------------------------------*/
function charlesstanleydirect_menu_tree__menu_footer_utilities($variables) {
 return '<ul class="sub-footer__list">' . $variables['tree'] . '</ul>';
}

/**----------------------------------------------------------------------------
* Change the output of the menu link items
 ----------------------------------------------------------------------------*/
function charlesstanleydirect_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';

  // This is the part that removes or changes the leaf class.
  if (!empty($element['#attributes']['class'])) {
    foreach ($element['#attributes']['class'] as $key => $class) {
      if ($class == 'leaf') {
        // Use only one of the two following lines.

        // To change the last class.
        //$element['#attributes']['class'][$key] = 'new-last-class';
        // To remove the last class.
        unset($element['#attributes']['class'][$key]);
      }
    }
  }
  
  // This is the part that removes the first class.
  if (!empty($element['#attributes']['class'])) {
    foreach ($element['#attributes']['class'] as $key => $class) {
      if ($class == 'first') {
        unset($element['#attributes']['class'][$key]);
      }
    }
  }
  
  // This is the part that removes the last class.
  if (!empty($element['#attributes']['class'])) {
    foreach ($element['#attributes']['class'] as $key => $class) {
      if ($class == 'first') {
        unset($element['#attributes']['class'][$key]);
      }
    }
  }
  
  // This is the part that removes or changes the menu-mlid-* class.
  if (!empty($element['#attributes']['class'])) {
    foreach ($element['#attributes']['class'] as $key => $class) {
      
      if (strpos($class, 'menu-mlid-') !== false) {
       $element['#attributes']['class'][$key] = '';
      }
      
    }
  }
  

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.