onlyEnabledLinks example

/** * {@inheritdoc} */
  public function loadSubtreeData($id$max_relative_depth = NULL) {
    $tree = [];
    $root = $this->loadFull($id);
    if (!$root) {
      return $tree;
    }
    $parameters = new MenuTreeParameters();
    $parameters->setRoot($id)->onlyEnabledLinks();
    return $this->loadTreeData($root['menu_name']$parameters);
  }

  /** * {@inheritdoc} */
  public function menuNameInUse($menu_name) {
    $query = $this->connection->select($this->table, NULL, $this->options);
    $query->addField($this->table, 'mlid');
    $query->condition('menu_name', $menu_name);
    $query->range(0, 1);
    
    $link = $this->treeStorage->load('test3');
    $link['enabled'] = FALSE;
    $this->treeStorage->save($link);
    $link = $this->treeStorage->load('test4');
    $link['enabled'] = FALSE;
    $this->treeStorage->save($link);
    $link = $this->treeStorage->load('test5');
    $link['enabled'] = FALSE;
    $this->treeStorage->save($link);

    $parameters = new MenuTreeParameters();
    $parameters->onlyEnabledLinks();
    $data = $this->treeStorage->loadTreeData('tools', $parameters);
    $this->assertCount(1, $data['tree']);
    $this->assertEquals('test1', $data['tree']['test1']['definition']['id']);
    $this->assertCount(1, $data['tree']['test1']['subtree']);
    $this->assertEquals('test2', $data['tree']['test1']['subtree']['test2']['definition']['id']);
    $this->assertEquals([]$data['tree']['test1']['subtree']['test2']['subtree']);

  }

  /** * Tests finding the subtree height with content menu links. */
/** * Loads a menu tree. * * @param \Drupal\system\MenuInterface $menu * A menu for which a tree should be loaded. * * @return \Drupal\Core\Menu\MenuLinkTreeElement[] * A menu link tree. */
  protected function loadMenuTree(MenuInterface $menu) : array {
    $parameters = new MenuTreeParameters();
    $parameters->onlyEnabledLinks();
    $parameters->setMinDepth(0);
    $tree = $this->menuTree->load($menu->id()$parameters);
    $manipulators = [
      ['callable' => 'menu.default_tree_manipulators:checkAccess'],
      ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
    ];
    return $this->menuTree->transform($tree$manipulators);
  }

}

  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));

      

  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
    $parameters->addCondition('provider', 'other_module');
    $this->assertEquals(['expanded' => 1, 'has_children' => 0, 'provider' => 'other_module', 'id' => [1337, '<']]$parameters->conditions);
  }

  /** * Tests onlyEnabledLinks(). * * @covers ::onlyEnabledLinks */
  public function testOnlyEnabledLinks() {
    $parameters = new MenuTreeParameters();
    $parameters->onlyEnabledLinks();
    $this->assertEquals(1, $parameters->conditions['enabled']);
  }

  /** * Tests setTopLevelOnly(). * * @covers ::setTopLevelOnly */
  public function testSetTopLevelOnly() {
    $parameters = new MenuTreeParameters();
    $parameters->setTopLevelOnly();
    
/** * 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 static function preRenderAdministrationTray(array $element) {
    $menu_tree = \Drupal::service('toolbar.menu_tree');
    // Load the administrative menu. The first level is the "Administration"     // link. In order to load the children of that link, start and end on the     // second level.     $parameters = new MenuTreeParameters();
    $parameters->setMinDepth(2)->setMaxDepth(2)->onlyEnabledLinks();
    // @todo Make the menu configurable in https://www.drupal.org/node/1869638.     $tree = $menu_tree->load('admin', $parameters);
    $manipulators = [
      ['callable' => 'menu.default_tree_manipulators:checkAccess'],
      ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
      ['callable' => 'toolbar_menu_navigation_links'],
    ];
    $tree = $menu_tree->transform($tree$manipulators);
    $element['administration_menu'] = $menu_tree->build($tree);
    return $element;
  }

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