You are here

public function renderCheeseburgerMenuBlock::render_tree in Cheeseburger Menu 8

1 call to renderCheeseburgerMenuBlock::render_tree()
renderCheeseburgerMenuBlock::content in src/Controller/renderCheeseburgerMenuBlock.php

File

src/Controller/renderCheeseburgerMenuBlock.php, line 63

Class

renderCheeseburgerMenuBlock

Namespace

Drupal\cheeseburger_menu\Controller

Code

public function render_tree($route_id, $page_type, $current_route, $config) {
  $menus_appear = isset($config['menus_appear']) ? $config['menus_appear'] : [];
  $menus_weight = isset($config['menus_weight']) ? $config['menus_weight'] : [];
  $menus_title = isset($config['menus_title']) ? $config['menus_title'] : [];
  $taxonomy_appear = isset($config['taxonomy_appear']) ? $config['taxonomy_appear'] : [];
  $taxonomy_weight = isset($config['taxonomy_weight']) ? $config['taxonomy_weight'] : [];
  $taxonomy_title = isset($config['taxonomy_title']) ? $config['taxonomy_title'] : [];
  $cart = [];
  $cart['appear'] = isset($config['cart_appear']) ? $config['cart_appear'] : FALSE;
  $cart['weight'] = isset($config['cart_weight']) ? $config['cart_weight'] : 0;
  $phone['appear'] = isset($config['phone_appear']) ? $config['phone_appear'] : FALSE;
  $phone['weight'] = isset($config['phone_weight']) ? $config['phone_weight'] : 0;
  $phone['store'] = isset($config['phone_store']) ? $config['phone_store'] : FALSE;
  $phone['number'] = isset($config['phone_number']) ? $config['phone_number'] : '';
  $language_switcher = isset($config['lang_switcher']) ? $config['lang_switcher'] : FALSE;
  $language_switcher_weight = isset($config['lang_switcher_weight']) ? $config['lang_switcher_weight'] : 0;
  $headerHeight = isset($config['headerHeight']) ? $config['headerHeight'] : 0;
  $headerPadding = isset($config['headerPadding']) ? $config['headerPadding'] : 0;
  $moduleHandler = \Drupal::service('module_handler');
  $menu_ids = $this
    ->getAllMenuLinkId();
  $taxonomy_term_ids = $this
    ->getAllTaxonomyTermIds();
  $tree = [];
  $titles = [];
  foreach ($menus_appear as $key => $menu) {
    $helpArray = [];
    if ($menus_title[$key] === TRUE || $menus_title[$key] === FALSE) {
      $helpArray['title'] = array_search($key, $menu_ids);
    }
    else {
      $helpArray['title'] = $menus_title[$key];
    }
    $helpArray['class'] = $key;
    $helpArray['weight'] = $menus_weight[$key];
    $titles[] = $helpArray;
  }
  foreach ($taxonomy_appear as $key => $taxonomy) {
    $helpArray = [];
    if ($taxonomy_title[$key] === TRUE || $taxonomy_title[$key] === FALSE) {
      $helpArray['title'] = array_search($key, $taxonomy_term_ids);
    }
    else {
      $helpArray['title'] = $taxonomy_title[$key];
    }
    $helpArray['class'] = $key;
    $helpArray['weight'] = $taxonomy_weight[$key];
    $titles[] = $helpArray;
  }
  if ($cart['appear']) {
    $helpArray = [];
    $helpArray['title'] = $this
      ->t('Cart');
    $helpArray['class'] = 'cart';
    $helpArray['url'] = '/cart';
    $helpArray['weight'] = $cart['weight'];
    $titles[] = $helpArray;
  }
  if ($phone['appear']) {
    if ($phone['store'] == 0) {
      $phone_number = $phone['number'];
    }
    else {
      if ($moduleHandler
        ->moduleExists('commerce_store')) {
        $store = Store::load($phone['store']);
        try {
          $phone_number = current($store
            ->get('field_phone')
            ->getValue())['value'];
        } catch (\Exception $exception) {
          $phone_number = FALSE;
        }
        $phone_number = str_replace(' ', '', $phone_number);
      }
      else {
        $phone_number = FALSE;
      }
    }
    if ($phone_number != FALSE) {
      $helpArray = [];
      $helpArray['title'] = $this
        ->t('Phone');
      $helpArray['class'] = 'phone';
      $helpArray['url'] = 'tel:' . $phone_number;
      $helpArray['weight'] = $phone['weight'];
      $titles[] = $helpArray;
    }
  }
  $titles = $this
    ->bubbleSortWeight($titles);
  $tree[] = [
    '#theme' => 'hierarchical_navigation',
    '#tree' => $titles,
    '#weight' => -100,
  ];
  $trees = [];

  //MENUS
  foreach ($menus_appear as $key => $menu) {
    if ($menus_title[$key] === TRUE) {
      $title = array_search($key, $menu_ids);
    }
    elseif ($menus_title[$key] === FALSE) {
      $title = FALSE;
    }
    else {
      $title = $menus_title[$key];
    }
    $trees[] = [
      '#theme' => 'hierarchical_menu',
      '#menu_tree' => $this
        ->getMenuTree($key),
      '#route_id' => $route_id,
      '#page_type' => $page_type,
      '#title' => $title,
      '#machine_name' => $key,
      '#current_url' => $current_route,
      '#cache' => [
        'max-age' => 0,
      ],
      '#attached' => [
        'library' => [
          'cheeseburger_menu/cheeseburger_menu',
        ],
        'drupalSettings' => [
          'collapsibleMenu' => 1,
          'interactiveParentMenu' => 0,
          'headerHeight' => $headerHeight,
          'headerPadding' => $headerPadding,
          'breakpoints' => $config['breakpoints'],
          'activeBreakpoints' => [],
        ],
      ],
      'weight' => $menus_weight[$key],
    ];
  }

  //taxonomies
  foreach ($taxonomy_appear as $key => $taxonomy) {
    if ($taxonomy_title[$key] === TRUE) {
      $title = array_search($key, $taxonomy_term_ids);
    }
    elseif ($taxonomy_title[$key] === FALSE) {
      $title = FALSE;
    }
    else {
      $title = $taxonomy_title[$key];
    }

    /** @var  $entityTypeManager Drupal\Core\Entity\EntityTypeManager */
    $entityTypeManager = \Drupal::service('entity_type.manager');
    $vocabulary_tree = $entityTypeManager
      ->getStorage('taxonomy_term')
      ->loadTree($key);
    $vocabulary_tree_array = [];
    foreach ($vocabulary_tree as $item) {
      $term = Term::load($item->tid);
      try {
        $icon = $term
          ->get('field_icon');
        if (!empty($icon
          ->getValue())) {
          $icon = $term
            ->get('field_icon')->entity
            ->getFileUri();
          $icon = file_create_url($icon);
        }
        else {
          $icon = '';
        }
      } catch (\Exception $exception) {
        $icon = '';
      }
      $vocabulary_tree_array[] = [
        'id' => $item->tid,
        'name' => $term
          ->getName(),
        'url' => $term
          ->url(),
        'parents' => $item->parents,
        'interactive_parent' => 1,
        'type' => 'taxonomy_term',
        'icon' => $icon,
      ];
    }
    $vocabulary_tree = $this
      ->generateTree($vocabulary_tree_array);
    $trees[] = [
      '#theme' => 'hierarchical_menu',
      '#menu_tree' => $vocabulary_tree,
      '#route_id' => $route_id,
      '#page_type' => $page_type,
      '#title' => $title,
      '#machine_name' => $key,
      '#current_url' => $current_route,
      '#cache' => [
        'max-age' => 0,
      ],
      '#attached' => [
        'library' => [
          'cheeseburger_menu/cheeseburger_menu',
        ],
        'drupalSettings' => [
          'collapsibleMenu' => 1,
          'interactiveParentMenu' => 0,
          'headerHeight' => $headerHeight,
          'headerPadding' => $headerPadding,
          'breakpoints' => $config['breakpoints'],
          'activeBreakpoints' => [],
        ],
      ],
      'weight' => $taxonomy_weight[$key],
    ];
  }
  if ($language_switcher == 1) {

    /** @var Drupal\Core\Language\LanguageManager $languageManager */
    $languageManager = \Drupal::service('language_manager');
    $current_language = $languageManager
      ->getCurrentLanguage()
      ->getId();
    $languageTree = [];
    $languages = $languageManager
      ->getLanguages();
    foreach ($languages as $language) {
      $languageTree[] = [
        'name' => $language
          ->getName(),
        'id' => $language
          ->getId(),
        'url' => $language
          ->isDefault() ? '/' : '/' . $language
          ->getId(),
        'type' => 'language',
      ];
    }
    $title = 'Language';
    $trees[] = [
      '#theme' => 'hierarchical_menu',
      '#menu_tree' => $languageTree,
      '#route_id' => $current_language,
      '#page_type' => 'language',
      '#title' => $title,
      '#machine_name' => 'language_switcher',
      '#current_url' => $current_route,
      '#cache' => [
        'max-age' => 0,
      ],
      '#attached' => [
        'library' => [
          'cheeseburger_menu/cheeseburger_menu',
        ],
        'drupalSettings' => [
          'collapsibleMenu' => 1,
          'interactiveParentMenu' => 0,
          'headerHeight' => $headerHeight,
          'headerPadding' => $headerPadding,
          'breakpoints' => $config['breakpoints'],
          'activeBreakpoints' => [],
        ],
      ],
      'weight' => $language_switcher_weight,
    ];
  }
  if ($config['css_default']) {
    $tree['#attached']['library'][] = 'cheeseburger_menu/cheeseburger_menu.css';
  }
  $trees = $this
    ->bubbleSortWeight($trees);
  foreach ($trees as $key => $iterator) {
    unset($trees[$key]['weight']);
  }
  $tree = array_merge($tree, $trees);
  return $tree;
}