You are here

public function RenderCheeseburgerMenuBlock::getMenuTree in Cheeseburger Menu 8.2

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

Returning menu tree data.

1 call to RenderCheeseburgerMenuBlock::getMenuTree()
RenderCheeseburgerMenuBlock::renderTree in src/Controller/RenderCheeseburgerMenuBlock.php
Render given tree.

File

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

Class

RenderCheeseburgerMenuBlock
Class RenderCheeseburgerMenuBlock.

Namespace

Drupal\cheeseburger_menu\Controller

Code

public function getMenuTree($menu) {
  $menu_tree = $this->menuTree;
  $menu_tree_parameters = new MenuTreeParameters();
  $menu_tree_parameters
    ->onlyEnabledLinks();
  $tree = $menu_tree
    ->load($menu, $menu_tree_parameters);
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];
  $tree = $menu_tree
    ->transform($tree, $manipulators);
  $menu_build = $menu_tree
    ->build($tree);
  $menu_tree_formatted = [];
  if (array_key_exists('#items', $menu_build)) {
    if (is_array($menu_build['#items']) || is_object($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 = $this
                ->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 = 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;
}