You are here

function theme_nice_menus_build in Nice Menus 6.2

Same name and namespace in other branches
  1. 7.3 nice_menus.module \theme_nice_menus_build()
  2. 7.2 nice_menus.module \theme_nice_menus_build()

Helper function that builds the nested lists of a Nice menu.

Parameters

array $menu: Menu array from which to build the nested lists.

int $depth: The number of children levels to display. Use -1 to display all children and use 0 to display no children.

array $trail: An array of parent menu items.

1 theme call to theme_nice_menus_build()
theme_nice_menus_tree in ./nice_menus.module
Builds the final Nice menu.

File

./nice_menus.module, line 372
Module to enable CSS dropdown and flyout menus.

Code

function theme_nice_menus_build($menu, $depth = -1, $trail = NULL) {
  $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.
        $children = theme('nice_menus_build', $menu_item['below'], $depth, $trail);

        // Set the class to parent only of children are displayed.
        $parent_class = $children && ($menu_item['link']['depth'] <= $depth || $depth == -1) ? 'menuparent ' : '';
        $output .= '<li class="menu-' . $mlid . ' ' . $parent_class . $class . $first_class . $oddeven_class . $last_class . '">' . theme('menu_item_link', $menu_item['link']);

        // Check our depth parameters.
        if ($menu_item['link']['depth'] <= $depth || $depth == -1) {

          // Build the child UL only if children are displayed for the user.
          if ($children) {
            $output .= '<ul>';
            $output .= $children;
            $output .= "</ul>\n";
          }
        }
        $output .= "</li>\n";
      }
      else {
        $output .= '<li class="menu-' . $mlid . ' ' . $class . $first_class . $oddeven_class . $last_class . '">' . theme('menu_item_link', $menu_item['link']) . '</li>' . "\n";
      }
    }
  }
  return $output;
}