You are here

function template_preprocess_tb_megamenu_nav in The Better Mega Menu 8

Same name and namespace in other branches
  1. 7 tb_megamenu.themes.inc \template_preprocess_tb_megamenu_nav()
  2. 2.x tb_megamenu.module \template_preprocess_tb_megamenu_nav()

Prepare variables for tb_megamenu_nav.

Parameters

array $vars: Variables are declared in theme.

File

./tb_megamenu.module, line 523
TB Mega Menu.

Code

function template_preprocess_tb_megamenu_nav(array &$vars) {
  $items = $vars['items'];
  $level = $vars['level'];
  $vars['lis'] = [];
  foreach ($items as $item) {
    if ($item->link
      ->isEnabled()) {
      $weight = $item->link
        ->getWeight();
      $title = $item->link
        ->getTitle();
      $vars['lis'][$weight][$title] = [
        '#theme' => 'tb_megamenu_item',
        '#menu_name' => $vars['menu_name'],
        '#level' => $level + 1,
        '#item' => $item,
        '#menu_config' => $vars['menu_config'],
        '#block_config' => $vars['block_config'],
        '#trail' => $vars['trail'],
        '#section' => $vars['section'],
      ];
    }
  }

  // Sort items by link weights as array keys.
  ksort($vars['lis']);

  // Restructure to account for duplicate weights.
  $new_list = [];
  foreach ($vars['lis'] as $weight_group) {
    ksort($weight_group);
    foreach ($weight_group as $item) {
      $new_list[] = $item;
    }
  }
  $vars['lis'] = $new_list;
  $vars['attributes'] = new Attribute();
  $vars['attributes']['class'] = [];
  $theme_name = str_replace('_', '-', $vars['theme_hook_original']);
  $vars['attributes']['class'][] = $theme_name;
  $vars['attributes']['class'][] = "nav";
  $vars['attributes']['class'][] = "level-" . $level;
  $vars['attributes']['class'][] = "items-" . count($items);

  // Add a11y attributes to unordered list tag.
  $vars['attributes']['role'] = 'list';
}