You are here

function megamenu_theme_menu_tree in Megamenu 6

Same name and namespace in other branches
  1. 7 megamenu.module \megamenu_theme_menu_tree()

Theme a menu tree

This function takes a menu tree, such as primary links, and generates HTML markup of the menu so that it can be styled as a mega menu. It takes the first three nested levels of the menu tree and creates a structure of nested lists with appropriate classes and IDs assigned (even, odd, active, etc.).

First, we iterate through the first level of menu items (branch/tier-1/megamenu-bin). Each item will be the megamenu-parent of the second level of links (twig/tier-2/megamenu-slot). Next we iterate through the twigs of the menu tree to fill the megamenu-bins. A bin is an unordered list which contains slots (twig/tier-2 items). To fill the slots we iterate through each twig, where the leaves are the deepest level of the menu tree (tier-3). Each leaf is a list item containing a tier-3 menu link.

Abbreviations: t1, t2, & t3 stands for tier-1, tier-2, and tier-3 respectively. They represent nested level menu items.

Parameters

$menutree: The menu tree to be marked up (i.e. primary_links)

Return value

HTML markup for a mega menu

1 call to megamenu_theme_menu_tree()
megamenu_block in ./megamenu.module
Implementation of hook_block()

File

./megamenu.module, line 161
megamenu-Menu Builder

Code

function megamenu_theme_menu_tree($menu_name) {
  $menutree = _megamenu_get_menu_tree($menu_name);
  $skin = _megamenu_get_skin_by_name($menu_name);
  $menu_orientation = _megamenu_get_menu_orientation_by_name($menu_name);

  // TODO: Currently, these attributes are set menu wide. Eventually these might should be set per menu level?
  $slot_orientation = _megamenu_get_slot_orientation_by_name($menu_name);

  /* TODO: temp value, should be attached to branch level in admin interface */
  $slot_attributes = _megamenu_get_slot_attributes_by_name($menu_name);

  /* TODO: temp value, should be attached to twig level in admin interface. */
  $output = '<ul id="megamenu-' . $menu_name . '" class="megamenu-menu ' . $menu_orientation . ' megamenu-skin-' . $skin . '">' . "\n";
  $t1_position = 0;
  $branch_count = count($menutree);
  foreach ($menutree as $branch) {
    $count_attributes = _megamenu_count_attributes($t1_position, $branch_count);
    $t1_position++;

    // TODO: Add an ID scheme (for faster js and css selection)
    $output .= '  <li class="megamenu-parent' . $count_attributes . '">' . "\n";
    $output .= '    <h2 class="megamenu-parent-title">' . l($branch['link']['link_title'], $branch['link']['link_path']) . '</h2>' . "\n";
    if ($branch['below']) {
      $output .= '    <ul class="megamenu-bin megamenu-slots-' . $slot_orientation . '">' . "\n";
      $t2_position = 0;
      $twig_count = count($branch['below']);
      foreach ($branch['below'] as $twig) {
        $count_attributes = _megamenu_count_attributes($t2_position, $twig_count);
        $t2_position++;

        // TODO: Add na ID scheme (for faster js and css selection)
        $output .= '      <li class="megamenu-slot ' . $count_attributes . '">' . "\n";
        $output .= '        <h3 class="megamenu-slot-title">' . l($twig['link']['link_title'], $twig['link']['link_path']) . '</h3>' . "\n";
        if ($twig['below']) {
          $output .= '  	 <ul class="megamenu-items ' . $slotattributes . '">' . "\n";
          $t3_position = 0;
          $leaf_count = count($twig['below']);
          foreach ($twig['below'] as $leaf) {
            $count_attributes = _megamenu_count_attributes($t3_position, $leaf_count);
            $t3_position++;
            $output .= '           <li class="megamenu-item' . $count_attributes . '">' . l($leaf['link']['link_title'], $leaf['link']['link_path']) . '</li>' . "\n";
          }

          // END leaf iteration
          $output .= '  	 </ul>' . "\n";
        }

        // END leaf detection
        $output .= '      </li>' . "\n";
      }

      // END twig iteration
      $output .= '    </ul>' . "\n";
    }

    // END twig detection
    $output .= '  </li>' . "\n";
  }

  // END branch iteration
  $output .= '</ul>' . "\n";
  return $output;
}