function uikit_preprocess_menu__navbar

8.x-3.x preprocess.inc uikit_preprocess_menu__navbar(&$variables)
8.x-2.x preprocess.inc uikit_preprocess_menu__navbar(&$variables)

File

includes/preprocess.inc, line 433
Set up variables to be placed within the template (.html.twig) files.

Code

function uikit_preprocess_menu__navbar(&$variables) {
  $items = $variables ['items'];
  $current_url = Url::fromRoute('<current>');
  $current_path = $current_url->toString();

  foreach ($items as $key => $item) {
    $variables ['items'][$key]['has_url'] = TRUE;
    $route = $item ['url']->getRouteName();
    $is_active = FALSE;

    if ($route == '<none>') {
      // <none> routes do not have a URL.
      $variables ['items'][$key]['has_url'] = FALSE;
    }

    if ($item ['url']->toString() == '/user') {
      $uid = \Drupal::currentUser()->id();

      if ($uid && $current_path == "/user/$uid") {
        $is_active = TRUE;
      }
    }

    if ($is_active && !$item ['attributes']->hasClass('uk-active')) {
      $item ['attributes']->addClass('uk-active');
    }

    if ($item ['below']) {
      foreach ($item ['below'] as $below_key => $below_item) {
        // Set is_divider and is_header variables to FALSE.
        $variables ['items'][$key]['below'][$below_key]['is_divider'] = FALSE;
        $variables ['items'][$key]['below'][$below_key]['is_header'] = FALSE;

        // Get the menu item options and menu_item_type, if set. menu_item_type
        // is set by the UIkit Components module.
        $options = $below_item ['url']->getOptions();
        $menu_item_type = isset($options ['menu_item_type']) ? $options ['menu_item_type'] : 0;

        if ($menu_item_type) {
          switch ($menu_item_type) {
            case 'nav_divider':
              // Add uk-nav-divider class and set is_divider to TRUE for
              // menu--navbar.html.twig.
              $variables ['items'][$key]['below'][$below_key]['attributes']->addClass('uk-nav-divider');
              $variables ['items'][$key]['below'][$below_key]['is_divider'] = TRUE;
              break;

            case 'nav_header':
              // Add uk-nav-header class and set is_header to TRUE for
              // menu--navbar.html.twig.
              $variables ['items'][$key]['below'][$below_key]['attributes']->addClass('uk-nav-header');
              $variables ['items'][$key]['below'][$below_key]['is_header'] = TRUE;
              break;
          }
        }
      }
    }
  }
}

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Log in