You are here

function smartmenus_format_menu_tree in Smartmenus.js 7

Reformat the menu tree.

Parameters

array $tree: The fully rendered menu tree.

array $attributes: Any additional attributes to be added.

Return value

array Renderable array representing the menu tree.

1 call to smartmenus_format_menu_tree()
theme_smartmenus_menu in ./smartmenus.theme.inc
Theme function definition.

File

./smartmenus.theme.inc, line 76
Smartmenus theme fnctions.

Code

function smartmenus_format_menu_tree(array $tree, array $attributes = array()) {
  $items = array();
  foreach ($tree as $data) {
    if ($data['link']['access'] && !$data['link']['hidden']) {
      $items[] = $data;
    }
  }
  $build = array();
  $num_items = count($items);
  foreach ($items as $i => $item) {
    $class = array();
    if ($i == 0) {
      $class[] = 'first';
    }
    if ($i == $num_items - 1) {
      $class[] = 'last';
    }
    if ($item['link']['in_active_trail']) {
      $class[] = 'active-trail';

      // Add a class to the LI too.
      $item['link']['localized_options']['attributes']['class'][] = 'active-trail';
    }

    // Build link.
    $element['#theme'] = 'menu_link__' . strtr($item['link']['menu_name'], '-', '_');
    $element['#attributes']['class'] = $class;
    $element['#title'] = $item['link']['title'];
    $element['#href'] = $item['link']['href'];
    $element['#localized_options'] = !empty($item['link']['localized_options']) ? $item['link']['localized_options'] : array();
    $element['#original_link'] = $item['link'];

    // Recursively build menu output.
    if ($item['below']) {
      $element['#below'] = smartmenus_format_menu_tree($item['below']);
    }
    else {
      $element['#below'] = array();
    }

    // Return expected menu array.
    $build[$item['link']['mlid']] = $element;
  }
  $output = array();
  if ($build) {

    // Implement own menu wrapper.
    $output['#sorted'] = TRUE;
    $output['#tree'] = $build;
    $output['#attributes'] = $attributes;
    $output['#theme_wrappers'][] = 'smartmenus_menu_tree';
  }
  return $output;
}