You are here

public function renderCheeseburgerMenuBlock::getMenuTree in Cheeseburger Menu 8

Same name and namespace in other branches
  1. 8.4 src/Controller/RenderCheeseburgerMenuBlock.php \Drupal\cheeseburger_menu\Controller\RenderCheeseburgerMenuBlock::getMenuTree()
  2. 8.2 src/Controller/RenderCheeseburgerMenuBlock.php \Drupal\cheeseburger_menu\Controller\RenderCheeseburgerMenuBlock::getMenuTree()
  3. 8.3 src/Controller/RenderCheeseburgerMenuBlock.php \Drupal\cheeseburger_menu\Controller\RenderCheeseburgerMenuBlock::getMenuTree()
1 call to renderCheeseburgerMenuBlock::getMenuTree()
renderCheeseburgerMenuBlock::render_tree in src/Controller/renderCheeseburgerMenuBlock.php

File

src/Controller/renderCheeseburgerMenuBlock.php, line 323

Class

renderCheeseburgerMenuBlock

Namespace

Drupal\cheeseburger_menu\Controller

Code

public function getMenuTree($menu) {
  $menu_tree = \Drupal::menuTree();
  $tree = $menu_tree
    ->load($menu, new \Drupal\Core\Menu\MenuTreeParameters());
  $tree = $menu_tree
    ->transform($tree, []);
  $menu_build = $menu_tree
    ->build($tree);
  $menu_tree_formatted = [];
  if (array_key_exists('#items', $menu_build)) {
    if (is_iterable($menu_build['#items'])) {
      foreach ($menu_build['#items'] as $menu_data) {
        if ($menu_data['url']
          ->isRouted()) {
          $help_array = [];
          $help_array['subitem'] = $this
            ->findChildren($menu_data['below']);
          $help_array['name'] = $menu_data['title'];
          try {
            $help_array['id'] = $menu_data['url']
              ->getRouteParameters();
          } catch (\Exception $exception) {
            $help_array['id'][0] = NULL;
          }
          if (reset($help_array['id']) != NULL) {
            $help_array['type'] = key($help_array['id']);
            $help_array['id'] = reset($help_array['id']);
          }
          else {
            try {
              $help_array['id'] = $menu_data['url']
                ->getRouteName();
            } catch (\Exception $exception) {
              $help_array['id'] = NULL;
            }
            if ($help_array['id'] == "user.page") {
              $user = \Drupal::currentUser();
              $help_array['id'] = $user != NULL ? $user
                ->id() : $user;
              $help_array['type'] = 'user';
            }
            else {
              $help_array['type'] = 'view';
            }
          }
          try {
            $route_name = $menu_data['url']
              ->getRouteName();
          } catch (\Exception $exception) {
            $route_name = NULL;
          }
          if ($route_name != NULL) {
            $url = \Drupal\Core\Url::fromRoute($menu_data['url']
              ->getRouteName(), $menu_data['url']
              ->getRouteParameters());
            $help_array['url'] = $url
              ->toString();
          }
          else {
            $help_array['url'] = '';
          }
          $menu_tree_formatted[] = $help_array;
        }
      }
    }
  }
  return $menu_tree_formatted;
}