You are here

public function CheeseburgerMenuService::buildMenu in Cheeseburger Menu 5.0.x

Returns cheeseburger menu from menu.

Parameters

array $menu_settings: The menu settings.

bool $parent_menu_as_link: Whether parent menu item should be links or not.

Return value

\Drupal\cheeseburger_menu\CheeseburgerMenu Built cheeseburger menu.

File

src/CheeseburgerMenuService.php, line 291

Class

CheeseburgerMenuService
Base service providing functions.

Namespace

Drupal\cheeseburger_menu

Code

public function buildMenu(array $menu_settings, $parent_menu_as_link = FALSE) {
  $menu_tree_parameters = new MenuTreeParameters();
  $menu_tree_parameters
    ->onlyEnabledLinks();
  $max_depth = $menu_settings['settings']['max_depth'];
  $initial_visibility_level = $menu_settings['settings']['initial_visibility_level'] ?? 1;
  $active_trail = $this->menuActiveTrail
    ->getActiveTrailIds($menu_settings['id']);
  $menu_tree_parameters
    ->setActiveTrail($active_trail);
  $menu_tree_parameters
    ->setMinDepth($initial_visibility_level);
  if (!is_null($max_depth) && is_numeric($max_depth) && $max_depth > 0) {
    $menu_tree_parameters
      ->setMaxDepth($initial_visibility_level + $max_depth - 1);
  }
  if ($initial_visibility_level > 1) {
  }
  $menu_link_tree = $this->menuLinkTree
    ->load($menu_settings['id'], $menu_tree_parameters);
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];
  $this->moduleHandler
    ->alter('cheeseburger_menu_tree_manipulators', $manipulators, $menu_link_tree);
  $menu_link_tree = $this->menuLinkTree
    ->transform($menu_link_tree, $manipulators);
  $menu = $this->entityTypeManager
    ->getStorage('menu')
    ->load($menu_settings['id']);
  $cheeseburger_menu = CheeseburgerMenu::createFromSettings($menu_settings, $menu);
  $menu_item_settings = [
    'parent_as_link' => $parent_menu_as_link,
    'expanded' => $menu_settings['settings']['default_expanded'],
  ];
  foreach ($menu_link_tree as $menu_link_tree_element) {
    if ((!$menu_link_tree_element->access instanceof AccessResultInterface || $menu_link_tree_element->access
      ->isAllowed()) && $menu_link_tree_element->link
      ->isEnabled()) {
      if ($menu_settings['settings']['show_links_in_navigation']) {
        $cheeseburger_menu
          ->addNavigationMenuItem(CheeseburgerMenuItem::createFromMenuLinkTreeElement($menu_link_tree_element, $menu_item_settings));
      }
      else {
        $cheeseburger_menu
          ->addMenuItem(CheeseburgerMenuItem::createFromMenuLinkTreeElement($menu_link_tree_element, $menu_item_settings));
      }
    }
  }
  return $cheeseburger_menu;
}