You are here

protected function Menu::buildTree in Workbench Access 8

Traverses the menu link tree and builds parentage arrays.

Note: this method is necessary because Menu does not auto-load parents.

Parameters

string $id: The root id of the section tree.

array $data: An array of menu tree or subtree data.

array &$tree: The computed tree array to return.

Return value

array The compiled tree data.

1 call to Menu::buildTree()
Menu::getTree in src/Plugin/AccessControlHierarchy/Menu.php
Gets the entire hierarchy tree.

File

src/Plugin/AccessControlHierarchy/Menu.php, line 99

Class

Menu
Defines a hierarchy based on a Menu.

Namespace

Drupal\workbench_access\Plugin\AccessControlHierarchy

Code

protected function buildTree($id, array $data, array &$tree) {
  foreach ($data as $link_id => $link) {
    $tree[$id][$link_id] = [
      'id' => $link_id,
      'label' => $link->link
        ->getTitle(),
      'depth' => $link->depth,
      'parents' => [],
      'weight' => $link->link
        ->getWeight(),
      'description' => $link->link
        ->getDescription(),
      'path' => $link->link
        ->getUrlObject()
        ->toString(),
    ];

    // Get the parents.
    if ($parent = $link->link
      ->getParent()) {
      $tree[$id][$link_id]['parents'] = array_unique(array_merge($tree[$id][$link_id]['parents'], [
        $parent,
      ]));
      $tree[$id][$link_id]['parents'] = array_unique(array_merge($tree[$id][$link_id]['parents'], $tree[$id][$parent]['parents']));
    }
    else {
      $tree[$id][$link_id]['parents'] = [
        $id,
      ];
    }
    if (isset($link->subtree)) {

      // The elements of the 'subtree' sub-array are not sorted by weight.
      uasort($link->subtree, [
        $this,
        'sortTree',
      ]);
      $this
        ->buildTree($id, $link->subtree, $tree);
    }
  }
  return $tree;
}