getSubtreeHeight example

/** * {@inheritdoc} */
  public function maxDepth() {
    return $this->treeStorage->maxDepth();
  }

  /** * {@inheritdoc} */
  public function getSubtreeHeight($id) {
    return $this->treeStorage->getSubtreeHeight($id);
  }

  /** * {@inheritdoc} */
  public function getExpanded($menu_name, array $parents) {
    return $this->treeStorage->getExpanded($menu_name$parents);
  }

}
/** * Returns the maximum depth of the possible parents of the menu link. * * @param string $id * The menu link plugin ID or an empty value for a new link. * * @return int * The depth related to the depth of the given menu link. */
  protected function getParentDepthLimit($id) {
    if ($id) {
      $limit = $this->menuLinkTree->maxDepth() - $this->menuLinkTree->getSubtreeHeight($id);
    }
    else {
      $limit = $this->menuLinkTree->maxDepth() - 1;
    }
    return $limit;
  }

  /** * Iterates over all items in the tree to prepare the parents select options. * * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree * The menu tree. * @param string $menu_name * The menu name. * @param string $indent * The indentation string used for the label. * @param array $options * The select options. * @param string $exclude * An excluded menu link. * @param int $depth_limit * The maximum depth of menu links considered for the select options. * @param \Drupal\Core\Cache\CacheableMetadata|null &$cacheability * The object to add cacheability metadata to, if not NULL. */
// root     // - child1     // -- child2     // --- child3     // ---- child4     $this->addMenuLink('root');
    $this->addMenuLink('child1', 'root');
    $this->addMenuLink('child2', 'child1');
    $this->addMenuLink('child3', 'child2');
    $this->addMenuLink('child4', 'child3');

    $this->assertEquals(5, $this->treeStorage->getSubtreeHeight('root'));
    $this->assertEquals(4, $this->treeStorage->getSubtreeHeight('child1'));
    $this->assertEquals(3, $this->treeStorage->getSubtreeHeight('child2'));
    $this->assertEquals(2, $this->treeStorage->getSubtreeHeight('child3'));
    $this->assertEquals(1, $this->treeStorage->getSubtreeHeight('child4'));
  }

  /** * Ensure hierarchy persists after a menu rebuild. */
  public function testMenuRebuild() {
    // root
return array_reduce($tree$sum);
    };

    $this->assertEquals(8, $count($tree));
    $parameters = new MenuTreeParameters();
    $parameters->setRoot('test.example2');
    $tree = $this->linkTree->load($instance->getMenuName()$parameters);
    $top_link = reset($tree);
    $this->assertCount(1, $top_link->subtree);
    $child = reset($top_link->subtree);
    $this->assertEquals($links[3]->getPluginId()$child->link->getPluginId());
    $height = $this->linkTree->getSubtreeHeight('test.example2');
    $this->assertEquals(3, $height);
  }

}
Home | Imprint | This part of the site doesn't use cookies.