You are here

function menu_block_tree_output in Menu Block 6.2

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

Returns a rendered menu tree.

This is an optimized version of menu_tree_output() with additional classes added to the output.

Parameters

$tree: array A data structure representing the tree as returned from menu_tree_data.

Return value

string The rendered HTML of that data structure.

1 call to menu_block_tree_output()
menu_tree_build in ./menu_block.module
Build a menu tree based on the provided configuration.

File

./menu_block.module, line 611
Provides configurable blocks of menu items.

Code

function menu_block_tree_output(&$tree, $config = array()) {
  $output = '';
  $items = array();

  // Create context if no config was provided.
  if (empty($config)) {
    $config['delta'] = 0;

    // Grab any menu item to find the menu_name for this tree.
    $menu_item = current($tree);
    $config['menu_name'] = $menu_item['link']['menu_name'];
  }
  $hook_delta = str_replace('-', '_', $config['delta']);
  $hook_menu_name = str_replace('-', '_', $config['menu_name']);

  // Pull out just the menu items we are going to render so that we
  // get an accurate count for the first/last classes.
  foreach (array_keys($tree) as $key) {
    if (!$tree[$key]['link']['hidden']) {
      $items[$key] = array(
        'link' => $tree[$key]['link'],
        // To prevent copying the entire child array, we render it first.
        'below' => !empty($tree[$key]['below']) ? menu_block_tree_output($tree[$key]['below'], $config) : '',
      );
    }
  }
  $num_items = count($items);
  $i = 1;
  foreach (array_keys($items) as $key) {

    // Render the link.
    $link_class = array();
    if (!empty($items[$key]['link']['localized_options']['attributes']['class'])) {
      $link_class[] = $items[$key]['link']['localized_options']['attributes']['class'];
    }
    if ($items[$key]['link']['in_active_trail']) {
      $link_class[] = 'active-trail';
    }
    if (!empty($link_class)) {
      $items[$key]['link']['localized_options']['attributes']['class'] = implode(' ', $link_class);
    }
    $hooks = array();
    $hooks[] = 'menu_item_link__menu_block__' . $hook_delta;
    $hooks[] = 'menu_item_link__menu_block__' . $hook_menu_name;
    $hooks[] = 'menu_item_link__menu_block';
    $hooks[] = 'menu_item_link';
    $link = theme($hooks, $items[$key]['link']);

    // Render the menu item.
    $extra_class = array();
    if ($i == 1) {
      $extra_class[] = 'first';
    }
    if ($i == $num_items) {
      $extra_class[] = 'last';
    }
    $extra_class[] = 'menu-mlid-' . $items[$key]['link']['mlid'];
    if (!empty($items[$key]['link']['leaf_has_children'])) {
      $extra_class[] = 'has-children';
    }
    if ($items[$key]['link']['href'] == $_GET['q'] || $items[$key]['link']['href'] == '<front>' && drupal_is_front_page()) {
      $extra_class[] = 'active';
    }
    $extra_class = !empty($extra_class) ? implode(' ', $extra_class) : NULL;
    $hooks = array();
    $hooks[] = 'menu_item__menu_block__' . $hook_delta;
    $hooks[] = 'menu_item__menu_block__' . $hook_menu_name;
    $hooks[] = 'menu_item__menu_block';
    $hooks[] = 'menu_item';
    $output .= theme($hooks, $link, $items[$key]['link']['has_children'], $items[$key]['below'], $items[$key]['link']['in_active_trail'], $extra_class);
    $i++;
  }
  $hooks = array();
  $hooks[] = 'menu_tree__menu_block__' . $hook_delta;
  $hooks[] = 'menu_tree__menu_block__' . $hook_menu_name;
  $hooks[] = 'menu_tree__menu_block';
  $hooks[] = 'menu_tree';
  return $output ? theme($hooks, $output) : '';
}