You are here

function theme_nice_menus_build in Nice Menus 7.3

Same name and namespace in other branches
  1. 6.2 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 $variables: Menu arguments.

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 461
Module to enable CSS dropdown and flyout menus.

Code

function theme_nice_menus_build($variables) {

  // Menu array from which to build the nested lists.
  $menu = $variables['menu'];

  // The number of children levels to display. Use -1 to display all children
  // and use 0 to display no children.
  $depth = isset($variables['depth']) ? $variables['depth'] : '-1';

  // An array of parent menu items.
  $trail = isset($variables['trail']) ? $variables['trail'] : array();

  // "Show as expanded" option.
  $respect_expanded = isset($variables['respect_expanded']) ? $variables['respect_expanded'] : 0;
  $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.
      $class = str_replace(array(
        'http',
        'https',
        '://',
        'www',
      ), '', $menu_item['link']['href']);

      // Strip funny symbols.
      $class = drupal_html_class('menu-path-' . $class);
      if ($trail && in_array($mlid, $trail)) {
        $class .= ' active-trail';
      }

      // If this menu item has a mega dropdown, use it, and ignore any child items.
      if (module_exists('nice_mega_dropdowns') && ($dropdown_node = nice_mega_dropdowns_get_dropdown($menu_item))) {
        $parent_class = 'menuparent';
        $dropdown = array(
          '#type' => 'markup',
          '#markup' => '<ul><li class="mega-dropdown">' . $dropdown_node . '</li></ul>' . "\n",
        );
        $element = array(
          '#below' => $dropdown,
          '#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,
            ),
          ),
          '#original_link' => $menu_item['link'],
        );
        $variables['element'] = $element;
        $output .= theme('menu_link', $variables);
        unset($menu_item['below']);
      }
      elseif (!empty($menu_item['link']['has_children']) && !empty($menu_item['below']) && $depth != 0 && ($respect_expanded == 0 || $menu_item['link']['expanded'])) {

        // 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,
            '#respect_expanded' => $respect_expanded,
          );
        }
        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']['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,
            ),
          ),
          '#original_link' => $menu_item['link'],
        );
        $variables['element'] = $element;

        // Check for context reactions menu.
        if (module_exists('context')) {
          context_preprocess_menu_link($variables);
          if (isset($variables['element']['#localized_options']['attributes']['class']) && in_array('active', $variables['element']['#localized_options']['attributes']['class']) && !in_array('active-trail', $variables['element']['#attributes']['class'])) {
            $variables['element']['#attributes']['class'][] = ' active-trail';
          }
        }
        $output .= theme('menu_link', $variables);
      }
      else {
        $element = array(
          '#below' => '',
          '#title' => $menu_item['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,
            ),
          ),
          '#original_link' => $menu_item['link'],
        );
        $variables['element'] = $element;

        // Check for context reactions menu.
        if (module_exists('context')) {
          context_preprocess_menu_link($variables);
          if (isset($variables['element']['#localized_options']['attributes']['class']) && in_array('active', $variables['element']['#localized_options']['attributes']['class']) && !in_array('active-trail', $variables['element']['#attributes']['class'])) {
            $variables['element']['#attributes']['class'][] = ' active-trail';
          }
        }
        $output .= theme('menu_link', $variables);
      }
    }
  }
  return $output;
}