You are here

public function UltimenuManager::buildMenuTree in Ultimenu 8

Same name and namespace in other branches
  1. 8.2 src/UltimenuManager.php \Drupal\ultimenu\UltimenuManager::buildMenuTree()

Build the menu to contain Ultimenu regions.

Parameters

string $menu_name: The menu name.

array $config: The config available for the menu tree.

Return value

array An array of the modified ultimenu links.

Overrides UltimenuManagerInterface::buildMenuTree

File

src/UltimenuManager.php, line 366

Class

UltimenuManager
Manages Ultimenu plugin.

Namespace

Drupal\ultimenu

Code

public function buildMenuTree($menu_name, array $config) {
  $tree = $this
    ->loadMenuTree($menu_name);
  $submenu_enabled = !empty($config['submenu']);
  $ultimenu = [];
  foreach ($tree as $key => $data) {

    // Generally we only deal with visible links, but just in case.
    if (!$data->link
      ->isEnabled()) {
      continue;
    }
    if ($data->access !== NULL && !$data->access instanceof AccessResultInterface) {
      throw new \DomainException('MenuLinkTreeElement::access must be either NULL or an AccessResultInterface object.');
    }

    // Only render accessible links.
    if ($data->access instanceof AccessResultInterface && !$data->access
      ->isAllowed()) {
      continue;
    }
    $class = [];
    $element = [];
    $link = $data->link;
    $url = $link
      ->getUrlObject();
    $title = $link
      ->getTitle();
    $has_children = isset($data->hasChildren) && $data->hasChildren == TRUE;
    $expanded = $link
      ->isExpanded();
    $plugin_id = $link
      ->getPluginId();
    $show_submenu = $submenu_enabled && $expanded && $has_children;
    $children = $show_submenu ? $this
      ->loadSubMenuTree($menu_name, $plugin_id, $title) : [];

    // Get the non-localized title to get relevant info.
    $definition = $link
      ->getPluginDefinition();
    if ($url
      ->isExternal()) {
      $class[] = 'external';
    }
    $region['region'] = $this
      ->getRegionKey($config['menu_name'], $plugin_id, $title);
    $flyout = '';
    if ($regions = $this
      ->getSetting('regions')) {
      if (!empty($regions[$region['region']])) {
        $region['config'] = $config;
        $flyout = $this
          ->buildDataRegion($region, $children);
      }
    }
    $element['attributes']['class'] = $class;
    $element['title'] = $title;
    $element['url'] = $url;
    $element['options'] = $link
      ->getOptions();
    $element['description'] = $link
      ->getDescription();
    $element['flyout'] = $flyout;
    $element['config'] = $config;

    // @todo check provider: standard, menu_link_content.
    if ($definition['route_name'] == '<front>') {
      $shortened_uuid = 'front_page';
    }
    else {
      $shortened_uuid = $this
        ->getShortenedUuid($definition['id']);
    }
    $element['uuid'] = $definition['id'];
    $element['shortened_uuid'] = $shortened_uuid;
    $element['shortened_hash'] = $this
      ->getShortenedHash($definition['id']);
    $ultimenu[$link
      ->getPluginId()] = $element;
    unset($link);
  }
  return $ultimenu;
}