Nice Menus has a function called theme_nice_menus_build. This function basically creates the structure of each menu item and then an other function called theme_menu_link takes care about the output rendering. This is okay, but theme_menu_link will never be informed about the menu name what the menu link belongs to, however theme_nice_menus_build knows the menu name. So theme_menu_link will render the menu link (and optionally its child links) but knows nothing about the menu name. If you want to override the menu link menu specifically in theme_menu_link function, you can't do that without modifying the theme_nice_menus_build function which calls it. In theme_nice_menus_build you have to pass the menu name variable to theme_menu_link to solve this problem.

Here is my modified theme_nice_menus_build function ...

function mytheme_nice_menus_build($variables) {

  // ***** default function behavior starts here *****

  $menu = $variables['menu'];
  $depth = $variables['depth'];
  $trail = $variables['trail'];
  $output = '';
  // Prepare to count the links so we can mark first, last, odd and even.
  $index = 0;
  $count = 0;
  foreach ($menu as $menu_count) {
    if ($menu_count['link']['hidden'] == 0) {
      $count++;
    }
  }
  // Get to building the menu.
  foreach ($menu as $menu_item) {
    $mlid = $menu_item['link']['mlid'];
    // Check to see if it is a visible menu item.
    if (!isset($menu_item['link']['hidden']) || $menu_item['link']['hidden'] == 0) {
      // Check our count and build first, last, odd/even classes.
      $index++;
      $first_class = $index == 1 ? ' first ' : '';
      $oddeven_class = $index % 2 == 0 ? ' even ' : ' odd ';
      $last_class = $index == $count ? ' last ' : '';
      // Build class name based on menu path
      // e.g. to give each menu item individual style.
      // Strip funny symbols.
      $clean_path = str_replace(array('http://', 'www', '<', '>', '&', '=', '?', ':', '.'), '', $menu_item['link']['href']);
      // Convert slashes to dashes.
      $clean_path = str_replace('/', '-', $clean_path);
      $class = 'menu-path-' . $clean_path;
      if ($trail && in_array($mlid, $trail)) {
        $class .= ' active-trail';
      }
      // If it has children build a nice little tree under it.
      if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below'])) && $depth != 0) {
        // Keep passing children into the function 'til we get them all.
        if ($menu_item['link']['depth'] <= $depth || $depth == -1) {
          $children = array(
            '#theme' => 'nice_menus_build',
            '#prefix' => '<ul>',
            '#suffix' => '</ul>',
            '#menu' => $menu_item['below'],
            '#depth' => $depth,
            '#trail' => $trail,
          );
        }
        else {
          $children = '';
        }
        // Set the class to parent only of children are displayed.
        $parent_class = ($children && ($menu_item['link']['depth'] <= $depth || $depth == -1)) ? 'menuparent ' : '';
        $element = array(
          '#below' => $children,
          '#title' => $menu_item['link']['link_title'],
          '#href' =>  $menu_item['link']['href'],
          '#localized_options' => $menu_item['link']['localized_options'],
          '#attributes' => array(
            'class' => array('menu-' . $mlid, $parent_class, $class, $first_class, $oddeven_class, $last_class),
          ),
        );
        $variables['element'] = $element;

        // ***** default function behavior ends here *****

        $variables['element']['menu_name'] = $menu_item['link']['menu_name'];

        // ***** default function behavior continues here *****

        $output .= theme('menu_link', $variables);

      }
      else {

        $element = array(
          '#below' => '',
          '#title' => $menu_item['link']['link_title'],
          '#href' =>  $menu_item['link']['href'],
          '#localized_options' => $menu_item['link']['localized_options'],
          '#attributes' => array(
            'class' => array('menu-' . $mlid, $class, $first_class, $oddeven_class, $last_class),
          ),
        );
        $variables['element'] = $element;

        // ***** default function behavior ends here *****

        $variables['element']['menu_name'] = $menu_item['link']['menu_name'];

        // ***** default function behavior continues here *****

        $output .= theme('menu_link', $variables);
      }
    }
  }
  return $output;
}
 

... and the modified theme_menu_link function. For example, I would like to wrap around the title of the menu link with <span> element, if the menu link belongs to the main menu.

function mytheme_menu_link(array $variables) {

  // ***** default function behavior starts here *****

  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }

  // ***** default function behavior ends here *****

  $options = $element['#localized_options'];

  // If this menu link is part of the main menu, then modify the output.
  if (array_key_exists('menu_name', $element) && $element['menu_name'] == 'main-menu') {
    $options['html'] = TRUE;
    $output = l('<span>' . check_plain($element['#title']) . '</span>', $element['#href'], $options);
  }
  else {
    $output = l($element['#title'], $element['#href'], $options);
  }

  // ***** default function behavior continues here *****

  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

So my personal opinion is, that would be nice if theme_nice_menus_build would inform theme_menu_link about the menu name.

Comments

boobaa’s picture

Care to send a patch against latest stable nice_menus release?

avpaderno’s picture

Version: 7.x-2.1 » 7.x-2.x-dev
astonvictor’s picture

Status: Active » Closed (outdated)

D7 reached its EOL back in January 2025, and there is no active release for D7 for this module anymore.
Development or support is not planned for D7. All D7-related issues are marked as outdated in a bunch.

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.