You are here

protected function MenuItems::simplifyLinks in Simplify Menu 8

Same name and namespace in other branches
  1. 8.2 src/MenuItems.php \Drupal\simplify_menu\MenuItems::simplifyLinks()

Map menu tree into an array.

Parameters

array $links: The array of menu tree links.

string $submenuKey: The key for the submenu to simplify.

Return value

array The simplified menu tree array.

1 call to MenuItems::simplifyLinks()
MenuItems::getMenuTree in src/MenuItems.php
Get header menu links.

File

src/MenuItems.php, line 43

Class

MenuItems
Class MenuItems.

Namespace

Drupal\simplify_menu

Code

protected function simplifyLinks(array $links, $submenuKey = 'submenu') {
  $result = [];
  foreach ($links as $item) {
    $simplifiedLink = [
      'text' => $item->link
        ->getTitle(),
      'url' => $item->link
        ->getUrlObject()
        ->toString(),
    ];
    if ($item->hasChildren) {
      $simplifiedLink[$submenuKey] = $this
        ->simplifyLinks($item->subtree);
    }
    $result[] = $simplifiedLink;
  }
  return $result;
}