/**
  * Loads the whole menu tree.
  */
 public function expandAll($tree)
 {
     foreach ($tree as $key => $element) {
         if ($element->hasChildren && null !== $element->link && !$element->link instanceof InaccessibleMenuLink) {
             $menu_tree = \Drupal::menuTree();
             $parameters = new MenuTreeParameters();
             $parameters->setRoot($element->link->getPluginId())->excludeRoot()->setMaxDepth(1)->onlyEnabledLinks();
             $subtree = $menu_tree->load(NULL, $parameters);
             if ($subtree) {
                 $tree[$key]->subtree = $this->expandAll($subtree);
             }
         }
     }
     return $tree;
 }
예제 #2
0
 /**
  * Provide a single block on the administration overview page.
  *
  * @param \Drupal\Core\Menu\MenuLinkInterface $instance
  *   The menu item to be displayed.
  *
  * @return array
  *   An array of menu items, as expected by theme_admin_block_content().
  */
 public function getAdminBlock(MenuLinkInterface $instance)
 {
     $content = array();
     // Only find the children of this link.
     $link_id = $instance->getPluginId();
     $parameters = new MenuTreeParameters();
     $parameters->setRoot($link_id)->excludeRoot()->setTopLevelOnly()->excludeHiddenLinks();
     $tree = $this->menuTree->load(NULL, $parameters);
     $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'));
     $tree = $this->menuTree->transform($tree, $manipulators);
     foreach ($tree as $key => $element) {
         /** @var $link \Drupal\Core\Menu\MenuLinkInterface */
         $link = $element->link;
         $content[$key]['title'] = $link->getTitle();
         $content[$key]['options'] = $link->getOptions();
         $content[$key]['description'] = $link->getDescription();
         $content[$key]['url'] = $link->getUrlObject();
     }
     ksort($content);
     return $content;
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 public function loadAllChildren($id, $max_relative_depth = NULL)
 {
     $parameters = new MenuTreeParameters();
     $parameters->setRoot($id)->excludeRoot()->setMaxDepth($max_relative_depth)->onlyEnabledLinks();
     $links = $this->loadLinks(NULL, $parameters);
     foreach ($links as $id => $link) {
         $links[$id] = $this->prepareLink($link);
     }
     return $links;
 }
 /**
  * Tests creating links with an expected tree structure.
  */
 public function testCreateLinksInMenu()
 {
     // This creates a tree with the following structure:
     // - 1
     // - 2
     //   - 3
     //     - 4
     // - 5
     //   - 7
     // - 6
     // - 8
     // With link 6 being the only external link.
     $links = array(1 => MenuLinkMock::create(array('id' => 'test.example1', 'route_name' => 'example1', 'title' => 'foo', 'parent' => '')), 2 => MenuLinkMock::create(array('id' => 'test.example2', 'route_name' => 'example2', 'title' => 'bar', 'parent' => 'test.example1', 'route_parameters' => array('foo' => 'bar'))), 3 => MenuLinkMock::create(array('id' => 'test.example3', 'route_name' => 'example3', 'title' => 'baz', 'parent' => 'test.example2', 'route_parameters' => array('baz' => 'qux'))), 4 => MenuLinkMock::create(array('id' => 'test.example4', 'route_name' => 'example4', 'title' => 'qux', 'parent' => 'test.example3')), 5 => MenuLinkMock::create(array('id' => 'test.example5', 'route_name' => 'example5', 'title' => 'foofoo', 'parent' => '')), 6 => MenuLinkMock::create(array('id' => 'test.example6', 'route_name' => '', 'url' => 'https://drupal.org/', 'title' => 'barbar', 'parent' => '')), 7 => MenuLinkMock::create(array('id' => 'test.example7', 'route_name' => 'example7', 'title' => 'bazbaz', 'parent' => '')), 8 => MenuLinkMock::create(array('id' => 'test.example8', 'route_name' => 'example8', 'title' => 'quxqux', 'parent' => '')));
     foreach ($links as $instance) {
         $this->menuLinkManager->addDefinition($instance->getPluginId(), $instance->getPluginDefinition());
     }
     $parameters = new MenuTreeParameters();
     $tree = $this->linkTree->load('mock', $parameters);
     $count = function (array $tree) {
         $sum = function ($carry, MenuLinkTreeElement $item) {
             return $carry + $item->count();
         };
         return array_reduce($tree, $sum);
     };
     $this->assertEqual($count($tree), 8);
     $parameters = new MenuTreeParameters();
     $parameters->setRoot('test.example2');
     $tree = $this->linkTree->load($instance->getMenuName(), $parameters);
     $top_link = reset($tree);
     $this->assertEqual(count($top_link->subtree), 1);
     $child = reset($top_link->subtree);
     $this->assertEqual($child->link->getPluginId(), $links[3]->getPluginId());
     $height = $this->linkTree->getSubtreeHeight('test.example2');
     $this->assertEqual($height, 3);
 }
예제 #5
0
 /**
  * Provide the administration overview page.
  *
  * @param string $link_id
  *   The ID of the administrative path link for which to display child links.
  *
  * @return array
  *   A renderable array of the administration overview page.
  */
 public function overview($link_id)
 {
     // Check for status report errors.
     if ($this->systemManager->checkRequirements() && $this->currentUser()->hasPermission('administer site configuration')) {
         drupal_set_message($this->t('One or more problems were detected with your Drupal installation. Check the <a href="@status">status report</a> for more information.', array('@status' => $this->url('system.status'))), 'error');
     }
     // Load all menu links below it.
     $parameters = new MenuTreeParameters();
     $parameters->setRoot($link_id)->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks();
     $tree = $this->menuLinkTree->load(NULL, $parameters);
     $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'));
     $tree = $this->menuLinkTree->transform($tree, $manipulators);
     $tree_access_cacheability = new CacheableMetadata();
     $blocks = array();
     foreach ($tree as $key => $element) {
         $tree_access_cacheability = $tree_access_cacheability->merge(CacheableMetadata::createFromObject($element->access));
         // Only render accessible links.
         if (!$element->access->isAllowed()) {
             continue;
         }
         $link = $element->link;
         $block['title'] = $link->getTitle();
         $block['description'] = $link->getDescription();
         $block['content'] = array('#theme' => 'admin_block_content', '#content' => $this->systemManager->getAdminBlock($link));
         if (!empty($block['content']['#content'])) {
             $blocks[$key] = $block;
         }
     }
     if ($blocks) {
         ksort($blocks);
         $build = ['#theme' => 'admin_page', '#blocks' => $blocks];
         $tree_access_cacheability->applyTo($build);
         return $build;
     } else {
         $build = ['#markup' => $this->t('You do not have any administrative items.')];
         $tree_access_cacheability->applyTo($build);
         return $build;
     }
 }
예제 #6
0
 /**
  * Provide a single block on the administration overview page.
  *
  * @param \Drupal\Core\Menu\MenuLinkInterface $instance
  *   The menu item to be displayed.
  *
  * @return array
  *   An array of menu items, as expected by admin-block-content.html.twig.
  */
 public function getAdminBlock(MenuLinkInterface $instance)
 {
     $content = array();
     // 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 = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('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
             //   blocks are rendered.
             continue;
         }
         /** @var $link \Drupal\Core\Menu\MenuLinkInterface */
         $link = $element->link;
         $content[$key]['title'] = $link->getTitle();
         $content[$key]['options'] = $link->getOptions();
         $content[$key]['description'] = $link->getDescription();
         $content[$key]['url'] = $link->getUrlObject();
     }
     ksort($content);
     return $content;
 }
예제 #7
0
 /**
  * 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()->excludeHiddenLinks();
     $tree = $menu_tree->load(NULL, $parameters);
     $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:flatten'));
     $tree = $menu_tree->transform($tree, $manipulators);
     // Transform the tree to a list of menu links.
     $menu_links = array();
     foreach ($tree as $element) {
         $menu_links[] = $element->link;
     }
     return $menu_links;
 }