You are here

function theme_nice_menus_tree in Nice Menus 7.3

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

Builds the final Nice menu.

Return value

mixed An HTML string of properly nested Nice menu lists.

1 theme call to theme_nice_menus_tree()
theme_nice_menus in ./nice_menus.module
Theme function to allow any menu tree to be themed as a Nice menu.

File

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

Code

function theme_nice_menus_tree($variables) {

  // load nice_menus js and css.
  _load_nice_menus_library();

  /*
   * The top-level menu name that contains the menu to use (e.g. navigation
   * or main-menu) for Drupal menus. For custom $menus this is just the
   * name for menu display.
   */
  $menu_name = $variables['menu_name'];

  /*
   * The menu ID from which to start building the items, i.e. the parent
   * of the displayed menu.
   */
  $mlid = $variables['mlid'];

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

  /*
   * Optional. A custom menu array to use for theming -- it should have.
   * the same structure as that returned by menu_tree_all_data().
   */
  $menu = $variables['menu'];
  $respect_expanded = $variables['respect_expanded'];

  // Load the full menu array.
  $menu = isset($menu) ? $menu : menu_tree_all_data($menu_name);
  if (isset($menu)) {
    $page_menu = menu_tree_page_data($menu_name);
    $trail = nice_menus_build_page_trail($page_menu);
    unset($page_menu);
  }

  // Allow i18n module to translate strings where available.
  if (module_exists('i18n_menu')) {
    $menu = i18n_menu_localize_tree($menu);
  }

  // Assume depth == 0 by default, overriden if mlid is specified.
  $parent_depth = 0;

  // For custom $menus and menus built all the way from the top-level we.
  // don't need to "create" the specific sub-menu and we need to get the title.
  // from the $menu_name since there is no "parent item" array.
  // Create the specific menu if we have a mlid.
  if (!empty($mlid)) {

    // Load the parent menu item.
    $item = menu_link_load($mlid);
    $title = check_plain($item['title']);

    // The depth for our parent item, if it exists.
    $parent_depth = $item['depth'] ? $item['depth'] : 0;

    // Narrow down the full menu to the specific sub-tree we need.
    for ($p = 1; $p < 10; $p++) {
      if ($sub_mlid = $item["p{$p}"]) {
        $subitem = menu_link_load($sub_mlid);

        // Menu sets these ghetto-ass keys in _menu_tree_check_access().
        $menu = $menu[50000 + $subitem['weight'] . ' ' . $subitem['title'] . ' ' . $subitem['mlid']]['below'];
      }
    }
  }
  else {

    // Get the title from the DB since we don't have it in the $menu.
    $result = db_query("SELECT title FROM {menu_custom} WHERE menu_name = :menu_name", array(
      ':menu_name' => $menu_name,
    ))
      ->fetchField();
    $title = check_plain($result);
  }
  $output['content'] = '';
  $output['subject'] = $title;
  if ($menu) {

    // Set the total menu depth counting from this parent if we need it.
    $depth = $depth > 0 ? $parent_depth + $depth : $depth;
    $output['content'] .= theme('nice_menus_build', array(
      'menu' => $menu,
      'depth' => $depth,
      'trail' => $trail,
      'respect_expanded' => $respect_expanded,
    ));
  }
  return $output;
}