You are here

public function SimpleMobileMenuMenuLinkTree::build in Simple Mobile Menu 8

Same name and namespace in other branches
  1. 8.2 src/Menu/SimpleMobileMenuMenuLinkTree.php \Drupal\simple_mobile_menu\Menu\SimpleMobileMenuMenuLinkTree::build()

Builds a renderable array from a menu tree.

The menu item's LI element is given one of the following classes:

  • expanded: The menu item is showing its submenu.
  • collapsed: The menu item has a submenu that is not shown.
  • leaf: The menu item has no submenu.

Parameters

\Drupal\Core\Menu\MenuLinkTreeElement[] $tree: A data structure representing the tree, as returned from MenuLinkTreeInterface::load().

Return value

array A renderable array.

Overrides MenuLinkTree::build

File

src/Menu/SimpleMobileMenuMenuLinkTree.php, line 15

Class

SimpleMobileMenuMenuLinkTree
Extends MenuLinkTree to add specific theme suggestions for the toolbar.

Namespace

Drupal\simple_mobile_menu\Menu

Code

public function build(array $tree, $level = 0) {
  if ($level == 0) {
    if (!$tree) {
      return [];
    }
    $build = parent::build($tree, $level);

    /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
    $first_link = reset($tree)->link;

    // Get the menu name of the first link.
    $menu_name = $first_link
      ->getMenuName();

    // Add a more specific theme suggestion to differentiate this rendered
    // menu from others.
    $build['#menu_name'] = $menu_name;
    $build['#theme'] = 'menu__simple_mobile_menu__' . strtr($menu_name, '-', '_');
    return $build;
  }
  else {
    return parent::build($tree, $level);
  }
}