You are here

function _menu_tree_data in Drupal 6

Same name and namespace in other branches
  1. 7 includes/menu.inc \_menu_tree_data()

Recursive helper function to build the data representing a menu tree.

The function is a bit complex because the rendering of an item depends on the next menu item. So we are always rendering the element previously processed not the current one.

Related topics

1 call to _menu_tree_data()
menu_tree_data in includes/menu.inc
Build the data representing a menu tree.

File

includes/menu.inc, line 1069
API for the Drupal menu system.

Code

function _menu_tree_data($result, $parents, $depth, $previous_element = '') {
  $remnant = NULL;
  $tree = array();
  while ($item = db_fetch_array($result)) {

    // We need to determine if we're on the path to root so we can later build
    // the correct active trail and breadcrumb.
    $item['in_active_trail'] = in_array($item['mlid'], $parents);

    // The current item is the first in a new submenu.
    if ($item['depth'] > $depth) {

      // _menu_tree returns an item and the menu tree structure.
      list($item, $below) = _menu_tree_data($result, $parents, $item['depth'], $item);
      if ($previous_element) {
        $tree[$previous_element['mlid']] = array(
          'link' => $previous_element,
          'below' => $below,
        );
      }
      else {
        $tree = $below;
      }

      // We need to fall back one level.
      if (!isset($item) || $item['depth'] < $depth) {
        return array(
          $item,
          $tree,
        );
      }

      // This will be the link to be output in the next iteration.
      $previous_element = $item;
    }
    elseif ($item['depth'] == $depth) {
      if ($previous_element) {

        // Only the first time.
        $tree[$previous_element['mlid']] = array(
          'link' => $previous_element,
          'below' => FALSE,
        );
      }

      // This will be the link to be output in the next iteration.
      $previous_element = $item;
    }
    else {
      $remnant = $item;
      break;
    }
  }
  if ($previous_element) {

    // We have one more link dangling.
    $tree[$previous_element['mlid']] = array(
      'link' => $previous_element,
      'below' => FALSE,
    );
  }
  return array(
    $remnant,
    $tree,
  );
}