You are here

function template_preprocess_tb_megamenu in The Better Mega Menu 8

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

Prepare variables for tb_megamenu.

Parameters

array $vars: Variables are declared in theme.

File

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

Code

function template_preprocess_tb_megamenu(array &$vars) {
  $menu_name = $vars['menu_name'];
  $theme = $vars['block_theme'];
  $vars['#attached']['drupalSettings']['TBMegaMenu']['theme'] = $theme;
  $menu_tree = \Drupal::menuTree();
  $menu_active_trail = \Drupal::service('menu.active_trail')
    ->getActiveTrailIds($menu_name);
  $parameters = (new MenuTreeParameters())
    ->setActiveTrail($menu_active_trail)
    ->onlyEnabledLinks();
  $menu_items = $menu_tree
    ->load($menu_name, $parameters);
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkNodeAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
  ];
  $menu_items = $menu_tree
    ->transform($menu_items, $manipulators);
  tb_megamenu_check_access($menu_items);

  /*
   * This function is old, we need to solve this later.
   * if (function_exists('i18n_menu_localize_tree')) {
   *  $items = i18n_menu_localize_tree($items);
   * }
   */

  // @todo what does this function to do ?
  // $items_page = menu_tree_page_data($menu_name);
  // $trail = tb_megamenu_build_page_trail($items_page);
  $menu_builder = \Drupal::service('tb_megamenu.menu_builder');
  $trail = $menu_builder
    ->buildPageTrail($menu_items);
  $menu_config = $menu_builder
    ->getMenuConfig($menu_name, $theme);
  $block_config = $menu_builder
    ->getBlockConfig($menu_name, $theme);
  $menu_builder
    ->editBlockConfig($block_config);
  $menu_builder
    ->syncConfigAll($menu_items, $menu_config, $vars['section']);
  $vars['block_config'] = $block_config;
  $vars['attributes'] = new Attribute();
  $vars['attributes']['class'] = [];
  $theme_name = str_replace('_', '-', $vars['theme_hook_original']);
  $vars['attributes']['class'][] = $theme_name;
  $vars['attributes']['class'][] = $theme_name . '-' . str_replace('_', '-', $menu_name);
  if ($vars['section'] == 'frontend' && $block_config['style'] != '') {
    $vars['#attached']['library'][] = 'tb_megamenu/block_style.' . $block_config['style'];
    $vars['attributes']['class'][] = "style-" . $block_config['style'];
  }
  if ($block_config['animation'] != 'none') {
    $vars['attributes']['class'][] = "animate";
    $vars['attributes']['class'][] = $block_config['animation'];
    $vars['attributes']['data-duration'] = $block_config['duration'];
    $vars['attributes']['data-delay'] = $block_config['delay'];
    $vars['attributes']['id'] = "tb-megamenu-" . str_replace('_', '-', $menu_name);
  }
  $vars['content'] = [
    '#theme' => 'tb_megamenu_nav',
    '#menu_name' => $vars['menu_name'],
    '#level' => 0,
    '#menu_config' => $menu_config,
    '#block_config' => $block_config,
    '#items' => $menu_items,
    '#trail' => $trail,
    '#section' => $vars['section'],
  ];
  if ($vars['section'] == 'frontend' && $block_config['animation'] != 'none') {
    $css = '';
    $css .= '  transition-delay: ' . $block_config['delay'] . 'ms;';
    $css .= '  -webkit-transition-delay: ' . $block_config['delay'] . 'ms;';
    $css .= '  -ms-transition-delay: ' . $block_config['delay'] . 'ms;';
    $css .= '  -o-transition-delay: ' . $block_config['delay'] . 'ms;';
    $css .= '  transition-duration: ' . $block_config['duration'] . 'ms;';
    $css .= '  -webkit-transition-duration: ' . $block_config['duration'] . 'ms;';
    $css .= '  -ms-transition-duration: ' . $block_config['duration'] . 'ms;';
    $css .= '  -o-transition-duration: ' . $block_config['duration'] . 'ms;';
    $vars['css_style'] = $css;
  }

  // Add a11y attributes.
  $vars['attributes']['role'] = 'navigation';
  $vars['attributes']['aria-label'] = t('Main navigation');
}