You are here

public static function WeMegaMenuBuilder::getMenuTree in Drupal Mega Menu 8.x

Same name and namespace in other branches
  1. 8 src/WeMegaMenuBuilder.php \Drupal\we_megamenu\WeMegaMenuBuilder::getMenuTree()

Get menu tree we_megamenu.

Parameters

string $menu_name: Public static function getMenuTree menu_name.

\Drupal\Core\Menu\MenuLinkTreeElement $items: Public static function getMenuTree items.

int $level: Public static function getMenuTree level.

Return value

array Public static function getMenuTree array.

1 call to WeMegaMenuBuilder::getMenuTree()
WeMegaMenuBuilder::getMenuTreeOrder in src/WeMegaMenuBuilder.php
Get menu tree sorted by weight ascending.

File

src/WeMegaMenuBuilder.php, line 24

Class

WeMegaMenuBuilder

Namespace

Drupal\we_megamenu

Code

public static function getMenuTree($menu_name, $backend = TRUE, $items = [], $level = 0) {
  $result = [];
  if ($level == 0) {
    $menu_active_trail = Drupal::service('menu.active_trail')
      ->getActiveTrailIds($menu_name);
    $menu_tree_parameters = (new MenuTreeParameters())
      ->setActiveTrail($menu_active_trail)
      ->onlyEnabledLinks();
    $menu_tree = \Drupal::menuTree();
    $tree = $menu_tree
      ->load($menu_name, $menu_tree_parameters);
    $manipulators = [
      [
        'callable' => 'menu.default_tree_manipulators:checkAccess',
      ],
    ];
    $tree = $menu_tree
      ->transform($tree, $manipulators);
    foreach ($tree as $item) {
      if ($backend === FALSE && !$item->access
        ->isAllowed()) {
        continue;
      }
      $route_name = $item->link
        ->getPluginDefinition()['route_name'];
      $result[] = [
        'derivativeId' => $item->link
          ->getDerivativeId(),
        'title' => $item->link
          ->getTitle(),
        'level' => $level,
        'description' => $item->link
          ->getDescription(),
        'weight' => $item->link
          ->getWeight(),
        'url' => $item->link
          ->getUrlObject()
          ->toString(),
        'subtree' => self::getMenuTree($menu_name, $backend, $item, $level + 1),
        'route_name' => $route_name,
        'in_active_trail' => $item->inActiveTrail,
        'plugin_id' => $item->link
          ->getPluginId(),
      ];
    }
  }
  else {
    if ($items->hasChildren) {
      foreach ($items->subtree as $key_item => $item) {
        if ($backend === FALSE && !$item->access
          ->isAllowed()) {
          continue;
        }
        $route_name = $item->link
          ->getPluginDefinition()['route_name'];
        $result[] = [
          'derivativeId' => $item->link
            ->getDerivativeId(),
          'title' => $item->link
            ->getTitle(),
          'level' => $level,
          'description' => $item->link
            ->getDescription(),
          'weight' => $item->link
            ->getWeight(),
          'url' => $item->link
            ->getUrlObject()
            ->toString(),
          'subtree' => self::getMenuTree($menu_name, $backend, $item, $level + 1),
          'route_name' => $route_name,
          'in_active_trail' => $item->inActiveTrail,
          'plugin_id' => $item->link
            ->getPluginId(),
        ];
      }
    }
  }
  return $result;
}