You are here

function template_preprocess_we_megamenu_li in Drupal Mega Menu 8.x

Same name and namespace in other branches
  1. 8 we_megamenu.module \template_preprocess_we_megamenu_li()

Prepare variables for we_megamenu_li.

Parameters

array $vars: Variables are declared in theme.

File

./we_megamenu.module, line 332
Drupal 8 Mega Menu Module.

Code

function template_preprocess_we_megamenu_li(&$vars) {
  $menu_name = $vars['menu_name'];
  $block_theme = $vars['block_theme'];
  $data_config = WeMegaMenuBuilder::loadConfig($menu_name, $block_theme);
  $menu_config = isset($data_config->menu_config) ? $data_config->menu_config : [];
  $vars['attributes'] = new Attribute();
  $vars['attributes']['class'] = [];
  $vars['attributes']['class'][] = 'we-mega-menu-li';
  $vars['attributes']['class'][] = count($vars['subtree']) ? 'dropdown-menu' : '';
  $vars['attributes']['data-level'] = $vars['item']['level'];
  $vars['attributes']['data-element-type'] = 'we-mega-menu-li';
  $vars['attributes']['description'] = isset($vars['item']['description']) ? $vars['item']['description'] : '';
  $vars['href'] = '';
  if ($vars['item']['url'] == '<front>') {
    $vars['attributes']['data-id'] = 'home';
    $vars['href'] = Url::fromRoute('<front>')
      ->toString();
  }
  else {
    $vars['attributes']['data-id'] = !empty($vars['item']['derivativeId']) ? $vars['item']['derivativeId'] : $vars['item']['url'];
    $vars['href'] = $vars['item']['url'];
  }
  $vars['attributes']['data-submenu'] = 0;
  $vars['attributes']['hide-sub-when-collapse'] = 0;
  if (isset($menu_config->{$vars['attributes']['data-id']})) {
    $item_config = $menu_config->{$vars['attributes']['data-id']};
    $vars['attributes']['class'][] = !empty($item_config->item_config->group) ? 'we-mega-menu-group' : '';
    $vars['attributes']['class'][] = !empty($item_config->item_config->class) ? $item_config->item_config->class : '';
    $vars['attributes']['class'][] = !empty($item_config->item_config->{'hide_sub_when_collapse'}) ? 'sub-hidden-collapse' : '';
    $vars['attributes']['class'][] = !empty($item_config->item_config->{'data-alignsub'}) ? $item_config->item_config->{'data-alignsub'} : '';
    $vars['attributes']['data-group'] = !empty($item_config->item_config->group) ? $item_config->item_config->group : 0;
    $vars['attributes']['data-class'] = !empty($item_config->item_config->class) ? $item_config->item_config->class : '';
    $vars['attributes']['data-icon'] = !empty($item_config->item_config->{'data-icon'}) ? $item_config->item_config->{'data-icon'} : '';
    $vars['attributes']['data-caption'] = !empty($item_config->item_config->{'data-caption'}) ? $item_config->item_config->{'data-caption'} : '';
    $vars['attributes']['hide-sub-when-collapse'] = !empty($item_config->item_config->{'hide_sub_when_collapse'}) ? $item_config->item_config->{'hide_sub_when_collapse'} : '';
    $vars['attributes']['data-alignsub'] = !empty($item_config->item_config->{'data-alignsub'}) ? $item_config->item_config->{'data-alignsub'} : '';
    $vars['attributes']['data-target'] = !empty($item_config->item_config->{'data-target'}) ? $item_config->item_config->{'data-target'} : '';
  }
  $trail = $vars['trail'];
  $plugin_id = $vars['item']['plugin_id'];
  if (isset($trail[$plugin_id])) {
    $vars['attributes']['class'][] = 'active';
    $vars['attributes']['class'][] = 'active-trail';
    $vars['link']['attributes']['class'][] = 'active-trail';
  }
  if ($vars['subtree']) {
    $vars['content'] = [
      '#theme' => 'we_megamenu_submenu',
      '#items' => $vars['subtree'],
      '#item_config' => isset($menu_config->{$vars['attributes']['data-id']}) ? $menu_config->{$vars['attributes']['data-id']} : [],
      '#section' => $vars['section'],
      '#block_theme' => $vars['block_theme'],
      '#menu_name' => $vars['menu_name'],
      '#trail' => $vars['trail'],
    ];
    $vars['items'] = $vars['subtree'];
    $vars['attributes']['data-submenu'] = 1;
    $vars['attributes']['data-group'] = !empty($item_config->item_config->group) ? $item_config->item_config->group : 0;
  }
  elseif (isset($menu_config->{$vars['attributes']['data-id']}) && count($menu_config->{$vars['attributes']['data-id']}->rows_content)) {
    $item_config = $menu_config->{$vars['attributes']['data-id']};
    $vars['content'] = [
      '#theme' => 'we_megamenu_submenu',
      '#items' => $vars['subtree'],
      '#item_config' => $item_config,
      '#section' => $vars['section'],
      '#block_theme' => $vars['block_theme'],
      '#menu_name' => $vars['menu_name'],
      '#trail' => $vars['trail'],
    ];
    $vars['attributes']['data-submenu'] = 1;
    $vars['attributes']['class'][] = 'dropdown-menu';
  }
}