setTopLevelOnly example

/** * Returns all top level menu links. * * @return \Drupal\Core\Menu\MenuLinkInterface[] */
  protected function getTopLevelMenuLinks() {
    $menu_tree = \Drupal::menuTree();

    // The system.admin link is normally the parent of all top-level admin links.     $parameters = new MenuTreeParameters();
    $parameters->setRoot('system.admin')->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks();
    $tree = $menu_tree->load(NULL, $parameters);
    $manipulators = [
      ['callable' => 'menu.default_tree_manipulators:checkAccess'],
      ['callable' => 'menu.default_tree_manipulators:flatten'],
    ];
    $tree = $menu_tree->transform($tree$manipulators);

    // Transform the tree to a list of menu links.     $menu_links = [];
    foreach ($tree as $element) {
      $menu_links[] = $element->link;
    }

  public function overview($link_id) {
    // Check for status report errors.     if ($this->currentUser()->hasPermission('administer site configuration') && $this->systemManager->checkRequirements()) {
      $this->messenger()->addError($this->t('One or more problems were detected with your Drupal installation. Check the <a href=":status">status report</a> for more information.', [':status' => Url::fromRoute('system.status')->toString()]));
    }
    // Load all menu links below it.     $parameters = new MenuTreeParameters();
    $parameters->setRoot($link_id)->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks();
    $tree = $this->menuLinkTree->load(NULL, $parameters);
    $manipulators = [
      ['callable' => 'menu.default_tree_manipulators:checkAccess'],
      ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
    ];
    $tree = $this->menuLinkTree->transform($tree$manipulators);
    $tree_access_cacheability = new CacheableMetadata();
    $blocks = [];
    foreach ($tree as $key => $element) {
      $tree_access_cacheability = $tree_access_cacheability->merge(CacheableMetadata::createFromObject($element->access));

      
$parameters->onlyEnabledLinks();
    $this->assertEquals(1, $parameters->conditions['enabled']);
  }

  /** * Tests setTopLevelOnly(). * * @covers ::setTopLevelOnly */
  public function testSetTopLevelOnly() {
    $parameters = new MenuTreeParameters();
    $parameters->setTopLevelOnly();
    $this->assertEquals(1, $parameters->maxDepth);
  }

  /** * Tests excludeRoot(). * * @covers ::excludeRoot */
  public function testExcludeRoot() {
    $parameters = new MenuTreeParameters();
    $parameters->excludeRoot();
    

  public function getAdminBlock(MenuLinkInterface $instance) {
    $content = [];
    // Only find the children of this link.     $link_id = $instance->getPluginId();
    $parameters = new MenuTreeParameters();
    $parameters->setRoot($link_id)->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks();
    $tree = $this->menuTree->load(NULL, $parameters);
    $manipulators = [
      ['callable' => 'menu.default_tree_manipulators:checkAccess'],
      ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
    ];
    $tree = $this->menuTree->transform($tree$manipulators);
    foreach ($tree as $key => $element) {
      // Only render accessible links.       if (!$element->access->isAllowed()) {
        // @todo Bubble cacheability metadata of both accessible and         // inaccessible links. Currently made impossible by the way admin
Home | Imprint | This part of the site doesn't use cookies.