You are here

public function RenderCheeseburgerMenuBlock::renderTree in Cheeseburger Menu 8.4

Same name and namespace in other branches
  1. 8.2 src/Controller/RenderCheeseburgerMenuBlock.php \Drupal\cheeseburger_menu\Controller\RenderCheeseburgerMenuBlock::renderTree()
  2. 8.3 src/Controller/RenderCheeseburgerMenuBlock.php \Drupal\cheeseburger_menu\Controller\RenderCheeseburgerMenuBlock::renderTree()

Render given tree.

1 call to RenderCheeseburgerMenuBlock::renderTree()
RenderCheeseburgerMenuBlock::content in src/Controller/RenderCheeseburgerMenuBlock.php

File

src/Controller/RenderCheeseburgerMenuBlock.php, line 138
Controller used for rendering block.

Class

RenderCheeseburgerMenuBlock
Class RenderCheeseburgerMenuBlock.

Namespace

Drupal\cheeseburger_menu\Controller

Code

public function renderTree($config, $current_route, $derivative_id = NULL) {
  $menus = [];
  foreach ([
    'menu' => 'Menu',
    'taxonomy_vocabulary' => 'Vocabulary',
  ] as $menu_key => $menu_label) {
    foreach ($config[$menu_key] as $id => $data) {
      switch ($data['title']) {
        case 'do_not_show':
          $title = '';
          $menu = $this
            ->entityTypeManager()
            ->getStorage($menu_key)
            ->load($id);
          $navigation_title = $menu
            ->label();
          break;
        case 'use_default':
          $menu = $this
            ->entityTypeManager()
            ->getStorage($menu_key)
            ->load($id);
          $navigation_title = $title = $menu
            ->label();
          break;
        case 'manual':
          $navigation_title = $title = $data['manual_title'];
          break;
      }
      $menus[] = [
        'tree' => $this
          ->{'get' . $menu_label . 'Tree'}($id, $config['depth']),
        'id' => $id,
        'menu_weight' => $data['menu_weight'],
        'title' => $title,
        'collapsible_title' => $data['collapsible_title'] ? '' : ' cheeseburger-menu__menu-list-item--expanded',
        'navigation_title' => $navigation_title,
      ];
    }
  }
  $additional_menus = [];

  // Cart show.
  if (array_key_exists('cart', $config) && $config['cart']['show']) {
    $additional_menus[] = [
      'id' => 'cart',
      'menu_weight' => $config['cart']['menu_weight'],
      'url' => '/cart',
      'title' => '',
      'navigation_title' => $this
        ->t('Cart'),
    ];
  }

  // Phone show.
  if (array_key_exists('phone', $config) && $config['phone']['show']) {
    if ($config['phone']['store'] == 0) {
      $additional_menus[] = [
        'id' => 'phone',
        'menu_weight' => $config['phone']['menu_weight'],
        'url' => 'tel:' . $config['phone']['manual_title'],
        'title' => '',
        'navigation_title' => $this
          ->t('Phone'),
      ];
    }
    else {
      if ($this
        ->moduleHandler()
        ->moduleExists('commerce_store')) {
        $store = Store::load($config['phone']['store']);
        if (!empty($store) && $store
          ->hasField('field_phone')) {
          if (!$store
            ->get('field_phone')
            ->isEmpty()) {
            $additional_menus[] = [
              'id' => 'phone',
              'menu_weight' => $config['phone']['menu_weight'],
              'url' => 'tel:' . $store
                ->get('field_phone')->value,
              'title' => '',
              'navigation_title' => $this
                ->t('Phone'),
            ];
          }
        }
      }
    }
  }
  if (array_key_exists('lang_switcher', $config) && $config['lang_switcher']['show']) {
    $languageTree = [];
    $route_name = $this->pathMatcher
      ->isFrontPage() ? '<front>' : '<current>';
    $links = $this
      ->languageManager()
      ->getLanguageSwitchLinks($derivative_id, Url::fromRoute($route_name));
    $languages = $this
      ->languageManager()
      ->getLanguages();
    foreach ($languages as $language) {
      if (isset($links->links[$language
        ->getId()])) {
        $languageTree[] = [
          'title' => $language
            ->getName(),
          'id' => $language
            ->getId(),
          'params' => [],
          'url' => $links->links[$language
            ->getId()]['url']
            ->setOption('language', $language)
            ->toString(),
          'children' => [],
          'entity_type_id' => 'lang',
        ];
      }
    }
    $additional_menus[] = [
      'id' => 'lang-switcher',
      'menu_weight' => $config['lang_switcher']['menu_weight'],
      'navigation_title' => $this
        ->t('Language switcher'),
      'title' => $this
        ->t('Language switcher'),
      'tree' => $languageTree,
    ];
  }
  $tree = array_merge($menus, $additional_menus);
  $this
    ->formTree($tree, $current_route, $config['active_state_enable']);
  foreach ($tree as &$tree_item) {
    $this
      ->moduleHandler()
      ->alter('cheeseburger_tree_item', $tree_item);
  }
  $this
    ->moduleHandler()
    ->alter('cheeseburger_tree', $tree);
  $render = [
    '#theme' => 'cheeseburger_menu',
    '#tree' => $tree,
    '#show_navigation' => $config['show_navigation'],
    '#parent_menu_as_link' => $config['parent_menu_as_link'],
  ];
  if ($config['active_state_enable']) {
    $render['#cache']['contexts'] = [
      'url',
    ];
  }
  return $render;
}