You are here

function template_preprocess_tb_megamenu_item in The Better Mega Menu 8

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

Prepare variables for tb_megamenu_item.

Parameters

array $vars: Variables are declared in theme.

File

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

Code

function template_preprocess_tb_megamenu_item(array &$vars) {
  $level = $vars['level'];
  $item = $vars['item'];
  $vars['link'] = $item->link
    ->getPluginDefinition();
  $vars['link']['url'] = $item->link
    ->getUrlObject()
    ->toString();
  $vars['link']['title_translate'] = $item->link
    ->getTitle();
  $plugin_id = $vars['link']['id'];
  $trail = $vars['trail'];
  $menu_config = $vars['menu_config'];
  $item_config = isset($menu_config[$plugin_id]['item_config']) ? $menu_config[$plugin_id]['item_config'] : [];
  $menu_builder = \Drupal::service('tb_megamenu.menu_builder');
  $menu_builder
    ->editItemConfig($item_config);
  $vars['item_config'] = $item_config;
  if (!empty($menu_config[$plugin_id]['rows_content']) && ($vars['section'] == 'backend' || !$item_config['hidesub'])) {
    $vars['submenu'] = [
      '#theme' => 'tb_megamenu_submenu',
      '#menu_name' => $vars['menu_name'],
      '#parent' => $item,
      '#level' => $level,
      '#menu_config' => $vars['menu_config'],
      '#block_config' => $vars['block_config'],
      '#trail' => $vars['trail'],
      '#section' => $vars['section'],
    ];
  }
  else {
    $vars['submenu'] = NULL;
  }
  $vars['link']['attributes'] = new Attribute();
  $vars['attributes'] = new Attribute();
  $vars['attributes']['class'] = [];
  $vars['link']['attributes']['class'] = [];
  $theme_name = str_replace('_', '-', $vars['theme_hook_original']);
  $vars['attributes']['class'][] = $theme_name;
  $vars['attributes']['class'][] = 'level-' . $level;
  $vars['attributes']['class'][] = 'mega';
  if (!empty($item_config['class'])) {
    $vars['attributes']['class'][] = $item_config['class'];
  }
  if (!empty($item_config['alignsub'])) {
    $vars['attributes']['class'][] = "mega-align-" . $item_config['alignsub'];
  }
  if ($item_config['hidewcol']) {
    $vars['attributes']['class'][] = "sub-hidden-collapse";
  }
  if ($item_config['group'] && $vars['submenu']) {
    $vars['attributes']['class'][] = "mega-group";
    $vars['link']['attributes']['class'][] = 'mega-group-title';
  }
  elseif ($vars['submenu'] && !$item_config['hidesub']) {
    $vars['link']['attributes']['class'][] = 'dropdown-toggle';
    if ($level == 1) {
      $vars['attributes']['class'][] = 'dropdown';
    }
    else {
      $vars['attributes']['class'][] = 'dropdown-submenu';
    }
  }
  if (isset($trail[$plugin_id])) {
    $vars['attributes']['class'][] = 'active';
    $vars['attributes']['class'][] = 'active-trail';
    $vars['link']['attributes']['class'][] = 'active-trail';
  }
  $vars['attributes']['data-id'] = $plugin_id;
  $vars['attributes']['data-level'] = $level;
  $vars['attributes']['data-type'] = 'menu_item';
  $vars['attributes']['data-class'] = $item_config['class'];
  $vars['attributes']['data-xicon'] = $item_config['xicon'];
  $vars['attributes']['data-caption'] = $item_config['caption'];
  $vars['attributes']['data-alignsub'] = $item_config['alignsub'];
  $vars['attributes']['data-group'] = $item_config['group'];
  $vars['attributes']['data-hidewcol'] = $item_config['hidewcol'];
  $vars['attributes']['data-hidesub'] = $item_config['hidesub'];
  $vars['attributes']['data-label'] = $item->link
    ->getTitle();
  if ($vars['section'] == 'backend') {
    $vars['attributes']['title'] = t("Menu Item");
  }

  // Add a11y attributes to list item tag.
  $vars['attributes']['aria-level'] = $level;

  // Add a11y attributes to anchor tag.
  if (!empty($vars['link']['description'])) {
    $vars['link']['attributes']['title'] = $vars['link']['description'];
  }
  if ($vars['submenu'] != NULL) {
    $vars['link']['attributes']['aria-expanded'] = t('false');
  }

  // Make other menu attributes available to TB Mega Menu Anchor Links.
  if (!empty($vars['link']['options']['attributes']['target'])) {
    $vars['link']['attributes']['target'] = $vars['link']['options']['attributes']['target'];
  }
  if (!empty($vars['link']['options']['attributes']['rel'])) {
    $vars['link']['attributes']['rel'] = $vars['link']['options']['attributes']['rel'];
  }
  if (!empty($vars['link']['options']['attributes']['class'])) {
    $vars['link']['attributes']
      ->addClass($vars['link']['options']['attributes']['class']);
  }
}