You are here

function template_preprocess_tb_megamenu_subnav in The Better Mega Menu 2.x

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

Prepare variables for tb_megamenu_subnav.

Parameters

array $vars: Variables are declared in theme.

File

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

Code

function template_preprocess_tb_megamenu_subnav(array &$vars) {
  $col = $vars['col'];
  $level = $vars['level'];
  $vars['lis'] = [];
  foreach ($col as $tb_item) {
    $plugin_id = !empty($tb_item['plugin_id']) ? $tb_item['plugin_id'] : '';
    $item = \Drupal::service('tb_megamenu.menu_builder')
      ->getMenuItem($vars['menu_name'], $plugin_id);
    if (empty($tb_item)) {

      // @todo figure out how to handle NULLs in columns.
      continue;
    }
    if ($item->link == NULL) {
      continue;
    }
    $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'][] = "mega-nav";
  $vars['attributes']['class'][] = "level-" . $level;
  $vars['attributes']['class'][] = "items-" . count($col);

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