You are here

function template_preprocess_we_megamenu_ul in Drupal Mega Menu 8

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

Prepare variables for we_megamenu_ul.

Parameters

array $vars: Variables are declared in theme.

File

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

Code

function template_preprocess_we_megamenu_ul(array &$vars, $backend = FALSE) {
  $data_config = $vars['data_config'];
  foreach ($vars['items'] as $key_li => $li) {
    $vars['href'] = '';
    if ($li['url'] == '<front>') {
      $vars['attributes']['data-id'] = 'home';
      $vars['href'] = Url::fromRoute('<front>')
        ->toString();
    }
    else {
      $vars['attributes']['data-id'] = !empty($li['derivativeId']) ? $li['derivativeId'] : $li['url'];
      $current_user = \Drupal::currentUser();
      if (WeMegaMenuBuilder::routeExists($li['url'])) {
        $vars['href'] = Url::fromRoute($li['url'])
          ->toString();
      }
      else {
        $vars['href'] = $li['url'];
      }
    }
    if (!UrlHelper::isExternal($vars['href'])) {
      if ($backend === FALSE && Url::fromUri('internal:' . $vars['href'])
        ->access() == FALSE) {
        continue;
      }
    }
    if (strpos($vars['href'], 'user/logout') !== FALSE && $current_user
      ->id() == 0 && $vars['section'] != 'admin' && $backend === FALSE) {
      continue;
    }
    if (strpos($vars['href'], 'user/login') !== FALSE && $current_user
      ->id() != 0 && $vars['section'] != 'admin' && $backend === FALSE) {
      continue;
    }
    $vars['content'][] = [
      '#theme' => [
        'we_megamenu_li',
      ],
      '#title' => $li['title'],
      '#item' => $li,
      '#subtree' => $li['subtree'],
      '#data_config' => $vars['data_config'],
      '#section' => $vars['section'],
      '#block_theme' => $vars['block_theme'],
      '#menu_name' => $vars['menu_name'],
      '#trail' => $vars['trail'],
    ];
  }
  $vars['attributes'] = new Attribute();
  $vars['attributes']['class'] = [];
  $vars['attributes']['class'][] = 'we-mega-menu-ul';
  $vars['attributes']['class'][] = 'nav';
  $vars['attributes']['class'][] = 'nav-tabs';
}