diff --git a/core/includes/menu.inc b/core/includes/menu.inc index d7da9cc..e4015d5 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -322,206 +322,6 @@ function _menu_link_translate(&$item) { } /** - * Renders a menu tree based on the current path. - * - * @param $menu_name - * The name of the menu. - * - * @return - * A structured array representing the specified menu on the current page, to - * be rendered by drupal_render(). - */ -function menu_tree($menu_name) { - return \Drupal::service('menu_link.tree')->getRenderedTree($menu_name); -} - -/** - * Returns a rendered menu tree. - * - * The menu item's LI element is given one of the following classes: - * - expanded: The menu item is showing its submenu. - * - collapsed: The menu item has a submenu which is not shown. - * - leaf: The menu item has no submenu. - * - * @param $tree - * A data structure representing the tree as returned from menu_tree_data. - * - * @return - * A structured array to be rendered by drupal_render(). - */ -function menu_tree_output($tree) { - return \Drupal::service('menu_link.tree')->output($tree); -} - -/** - * Gets the data structure representing a named menu tree. - * - * Since this can be the full tree including hidden items, the data returned - * may be used for generating an an admin interface or a select. - * - * @param $menu_name - * The named menu links to return - * @param $link - * A fully loaded menu link, or NULL. If a link is supplied, only the - * path to root will be included in the returned tree - as if this link - * represented the current page in a visible menu. - * @param $max_depth - * Optional maximum depth of links to retrieve. Typically useful if only one - * or two levels of a sub tree are needed in conjunction with a non-NULL - * $link, in which case $max_depth should be greater than $link['depth']. - * - * @return - * An tree of menu links in an array, in the order they should be rendered. - */ -function menu_tree_all_data($menu_name, $link = NULL, $max_depth = NULL) { - return \Drupal::service('menu_link.tree')->buildAllData($menu_name, $link, $max_depth); -} - -/** - * Sets the path for determining the active trail of the specified menu tree. - * - * This path will also affect the breadcrumbs under some circumstances. - * Breadcrumbs are built using the preferred link returned by - * menu_link_get_preferred(). If the preferred link is inside one of the menus - * specified in calls to menu_tree_set_path(), the preferred link will be - * overridden by the corresponding path returned by menu_tree_get_path(). - * - * Setting this path does not affect the main content; for that use - * menu_set_active_item() instead. - * - * @param $menu_name - * The name of the affected menu tree. - * @param $path - * The path to use when finding the active trail. - */ -function menu_tree_set_path($menu_name, $path = NULL) { - return \Drupal::service('menu_link.tree')->setPath($menu_name, $path); -} - -/** - * Gets the path for determining the active trail of the specified menu tree. - * - * @param $menu_name - * The menu name of the requested tree. - * - * @return - * A string containing the path. If no path has been specified with - * menu_tree_set_path(), NULL is returned. - */ -function menu_tree_get_path($menu_name) { - return \Drupal::service('menu_link.tree')->getPath($menu_name); -} - -/** - * Gets the data structure for a named menu tree, based on the current page. - * - * The tree order is maintained by storing each parent in an individual - * field, see http://drupal.org/node/141866 for more. - * - * @param $menu_name - * The named menu links to return. - * @param $max_depth - * (optional) The maximum depth of links to retrieve. - * @param $only_active_trail - * (optional) Whether to only return the links in the active trail (TRUE) - * instead of all links on every level of the menu link tree (FALSE). Defaults - * to FALSE. - * - * @return - * An array of menu links, in the order they should be rendered. The array - * is a list of associative arrays -- these have two keys, link and below. - * link is a menu item, ready for theming as a link. Below represents the - * submenu below the link if there is one, and it is a subtree that has the - * same structure described for the top-level array. - */ -function menu_tree_page_data($menu_name, $max_depth = NULL, $only_active_trail = FALSE) { - return \Drupal::service('menu_link.tree')->buildPageData($menu_name, $max_depth, $only_active_trail); -} - -/** - * Builds a menu tree, translates links, and checks access. - * - * @param $menu_name - * The name of the menu. - * @param $parameters - * (optional) An associative array of build parameters. Possible keys: - * - expanded: An array of parent link ids to return only menu links that are - * children of one of the plids in this list. If empty, the whole menu tree - * is built, unless 'only_active_trail' is TRUE. - * - active_trail: An array of mlids, representing the coordinates of the - * currently active menu link. - * - only_active_trail: Whether to only return links that are in the active - * trail. This option is ignored, if 'expanded' is non-empty. - * - min_depth: The minimum depth of menu links in the resulting tree. - * Defaults to 1, which is the default to build a whole tree for a menu - * (excluding menu container itself). - * - max_depth: The maximum depth of menu links in the resulting tree. - * - conditions: An associative array of custom database select query - * condition key/value pairs; see _menu_build_tree() for the actual query. - * - * @return - * A fully built menu tree. - */ -function menu_build_tree($menu_name, array $parameters = array()) { - return \Drupal::service('menu_link.tree')->buildTree($menu_name, $parameters); -} - -/** - * Collects node links from a given menu tree recursively. - * - * @param $tree - * The menu tree you wish to collect node links from. - * @param $node_links - * An array in which to store the collected node links. - */ -function menu_tree_collect_node_links(&$tree, &$node_links) { - return \Drupal::service('menu_link.tree')->collectNodeLinks($tree, $node_links); -} - -/** - * Checks access and performs dynamic operations for each link in the tree. - * - * @param $tree - * The menu tree you wish to operate on. - * @param $node_links - * A collection of node link references generated from $tree by - * menu_tree_collect_node_links(). - */ -function menu_tree_check_access(&$tree, $node_links = array()) { - return \Drupal::service('menu_link.tree')->checkAccess($tree, $node_links); -} - -/** - * Sorts and returns the built data representing a menu tree. - * - * @param $links - * A flat array of menu links that are part of the menu. Each array element - * is an associative array of information about the menu link, containing the - * fields from the {menu_links} table, and optionally additional information - * from the {menu_router} table, if the menu item appears in both tables. - * This array must be ordered depth-first. See _menu_build_tree() for a sample - * query. - * @param $parents - * An array of the menu link ID values that are in the path from the current - * page to the root of the menu tree. - * @param $depth - * The minimum depth to include in the returned menu tree. - * - * @return - * An array of menu links in the form of a tree. Each item in the tree is an - * associative array containing: - * - link: The menu link item from $links, with additional element - * 'in_active_trail' (TRUE if the link ID was in $parents). - * - below: An array containing the sub-tree of this item, where each element - * is a tree item array with 'link' and 'below' elements. This array will be - * empty if the menu item has no items in its sub-tree having a depth - * greater than or equal to $depth. - */ -function menu_tree_data(array $links, array $parents = array(), $depth = 1) { - return \Drupal::service('menu_link.tree')->buildTreeData($links, $parents, $depth); -} - -/** * Implements template_preprocess_HOOK() for theme_menu_tree(). */ function template_preprocess_menu_tree(&$variables) { @@ -724,7 +524,9 @@ function menu_navigation_links($menu_name, $level = 0) { } // Get the menu hierarchy for the current page. - $tree = menu_tree_page_data($menu_name, $level + 1); + /** @var \Drupal\menu_link\MenuTreeInterface $menu_tree */ + $menu_tree = \Drupal::service('menu_link.tree'); + $tree = $menu_tree->buildPageData($menu_name, $level + 1); // Go down the active trail until the right level is reached. while ($level-- > 0 && $tree) { @@ -973,6 +775,9 @@ function menu_set_active_item($path) { function menu_set_active_trail($new_trail = NULL) { $trail = &drupal_static(__FUNCTION__); + /** @var \Drupal\menu_link\MenuTreeInterface $menu_tree */ + $menu_tree = \Drupal::service('menu_link.tree'); + if (isset($new_trail)) { $trail = $new_trail; } @@ -996,7 +801,7 @@ function menu_set_active_trail($new_trail = NULL) { // Pass TRUE for $only_active_trail to make menu_tree_page_data() build // a stripped down menu tree containing the active trail only, in case // the given menu has not been built in this request yet. - $tree = menu_tree_page_data($preferred_link['menu_name'], NULL, TRUE); + $tree = $menu_tree->buildPageData($preferred_link['menu_name'], NULL, TRUE); list($key, $curr) = each($tree); } // There is no link for the current path. diff --git a/core/modules/menu/lib/Drupal/menu/MenuFormController.php b/core/modules/menu/lib/Drupal/menu/MenuFormController.php index d6720a7..67803f3 100644 --- a/core/modules/menu/lib/Drupal/menu/MenuFormController.php +++ b/core/modules/menu/lib/Drupal/menu/MenuFormController.php @@ -12,6 +12,7 @@ use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Language\Language; use Drupal\menu_link\MenuLinkStorageControllerInterface; +use Drupal\menu_link\MenuTreeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -34,6 +35,13 @@ class MenuFormController extends EntityFormController { protected $menuLinkStorage; /** + * The menu tree. + * + * @var \Drupal\menu_link\MenuTreeInterface + */ + protected $menuTree; + + /** * The overview tree form. * * @var array @@ -47,10 +55,13 @@ class MenuFormController extends EntityFormController { * The factory for entity queries. * @param \Drupal\menu_link\MenuLinkStorageControllerInterface $menu_link_storage * The menu link storage controller. + * @param \Drupal\menu_link\MenuTreeInterface $menu_tree + * The menu tree. */ - public function __construct(QueryFactory $entity_query_factory, MenuLinkStorageControllerInterface $menu_link_storage) { + public function __construct(QueryFactory $entity_query_factory, MenuLinkStorageControllerInterface $menu_link_storage, MenuTreeInterface $menu_tree) { $this->entityQueryFactory = $entity_query_factory; $this->menuLinkStorage = $menu_link_storage; + $this->menuTree = $menu_tree; } /** @@ -59,7 +70,8 @@ public function __construct(QueryFactory $entity_query_factory, MenuLinkStorageC public static function create(ContainerInterface $container) { return new static( $container->get('entity.query'), - $container->get('entity.manager')->getStorageController('menu_link') + $container->get('entity.manager')->getStorageController('menu_link'), + $container->get('menu_link.tree') ); } @@ -256,13 +268,13 @@ protected function buildOverviewForm(array &$form, array &$form_state) { } $delta = max(count($links), 50); - $tree = menu_tree_data($links); + $tree = $this->menuTree->buildTreeData($links); $node_links = array(); - menu_tree_collect_node_links($tree, $node_links); + $this->menuTree->collectNodeLinks($tree, $node_links); // We indicate that a menu administrator is running the menu access check. $this->getRequest()->attributes->set('_menu_admin', TRUE); - menu_tree_check_access($tree, $node_links); + $this->menuTree->checkAccess($tree, $node_links); $this->getRequest()->attributes->set('_menu_admin', FALSE); $form = array_merge($form, $this->buildOverviewTreeForm($tree, $delta)); diff --git a/core/modules/menu/menu.module b/core/modules/menu/menu.module index 8eb1d34..786dd93 100644 --- a/core/modules/menu/menu.module +++ b/core/modules/menu/menu.module @@ -263,10 +263,13 @@ function _menu_get_options($menus, $available_menus, $item) { $limit = _menu_parent_depth_limit($item); } + /** @var \Drupal\menu_link\MenuTreeInterface $menu_tree */ + $menu_tree = \Drupal::service('menu_link.tree'); + $options = array(); foreach ($menus as $menu_name => $title) { if (isset($available_menus[$menu_name])) { - $tree = menu_tree_all_data($menu_name, NULL); + $tree = $menu_tree->buildAllData($menu_name, NULL); $options[$menu_name . ':0'] = '<' . $title . '>'; _menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit); } diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuTree.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuTree.php index c2b574f..7060e35 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuTree.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuTree.php @@ -147,9 +147,11 @@ public function __construct(Connection $database, CacheBackendInterface $cache_b public function buildAllData($menu_name, $link = NULL, $max_depth = NULL) { $language_interface = $this->languageManager->getCurrentLanguage(); - // Use $mlid as a flag for whether the data being loaded is for the whole tree. + // Use $mlid as a flag for whether the data being loaded is for the whole + // tree. $mlid = isset($link['mlid']) ? $link['mlid'] : 0; - // Generate a cache ID (cid) specific for this $menu_name, $link, $language, and depth. + // Generate a cache ID (cid) specific for this $menu_name, $link, $language, + // and depth. $cid = 'links:' . $menu_name . ':all:' . $mlid . ':' . $language_interface->id . ':' . (int) $max_depth; if (!isset($this->menuFullTrees[$cid])) { @@ -167,8 +169,9 @@ public function buildAllData($menu_name, $link = NULL, $max_depth = NULL) { 'max_depth' => $max_depth, ); if ($mlid) { - // The tree is for a single item, so we need to match the values in its - // p columns and 0 (the top level) with the plid values of other links. + // The tree is for a single item, so we need to match the values in + // its p columns and 0 (the top level) with the plid values of other + // links. $parents = array(0); for ($i = 1; $i < MENU_MAX_DEPTH; $i++) { if (!empty($link["p$i"])) { @@ -220,8 +223,8 @@ public function buildPageData($menu_name, $max_depth = NULL, $only_active_trail // won't be built anymore, as this function is invoked from // template_preprocess_page(). So in order to not build a giant menu tree // that needs to be checked for access on all levels, we simply check - // whether we have the menu already in cache, or otherwise, build a minimum - // tree containing the active trail only. + // whether we have the menu already in cache, or otherwise, build a + // minimum tree containing the active trail only. // @see menu_set_active_trail() if (!isset($this->menuPageTrees[$cid]) && $only_active_trail) { $cid .= ':trail'; @@ -248,22 +251,23 @@ public function buildPageData($menu_name, $max_depth = NULL, $only_active_trail // If this page is accessible to the current user, build the tree // parameters accordingly. if ($page_not_403) { - // Find a menu link corresponding to the current path. If $active_path - // is NULL, let menu_link_get_preferred() determine the path. + // Find a menu link corresponding to the current path. If + // $active_path is NULL, let menu_link_get_preferred() determine + // the path. if ($active_link = $this->menuLinkGetPreferred($menu_name, $active_path)) { // The active link may only be taken into account to build the - // active trail, if it resides in the requested menu. Otherwise, - // we'd needlessly re-run _menu_build_tree() queries for every menu - // on every page. + // active trail, if it resides in the requested menu. + // Otherwise, we'd needlessly re-run _menu_build_tree() queries + // for every menu on every page. if ($active_link['menu_name'] == $menu_name) { - // Use all the coordinates, except the last one because there - // can be no child beyond the last column. + // Use all the coordinates, except the last one because + // there can be no child beyond the last column. for ($i = 1; $i < MENU_MAX_DEPTH; $i++) { if ($active_link['p' . $i]) { $active_trail[$active_link['p' . $i]] = $active_link['p' . $i]; } } - // If we are asked to build links for the active trail only, skip + // If we are asked to build links for the active trail only,skip // the entire 'expanded' handling. if ($only_active_trail) { $tree_parameters['only_active_trail'] = TRUE; @@ -300,8 +304,8 @@ public function buildPageData($menu_name, $max_depth = NULL, $only_active_trail $this->cache->set($cid, $tree_parameters, Cache::PERMANENT, array('menu' => $menu_name)); } - // Build the tree using the parameters; the resulting tree will be cached - // by _menu_build_tree(). + // Build the tree using the parameters; the resulting tree will be + // cached by $tihs->buildTree(). $this->menuPageTrees[$cid] = $this->buildTree($menu_name, $tree_parameters); } return $this->menuPageTrees[$cid]; @@ -418,6 +422,11 @@ public function buildTree($menu_name, array $parameters = array()) { * to further massage the data manually before further processing happens. * menu_tree_check_access() needs to be invoked afterwards. * + * @param string $menu_name + * The name of the menu. + * @param array $parameters + * The parameters passed into static::buildTree() + * * @see static::buildTree() */ protected function doBuildTree($menu_name, array $parameters = array()) { @@ -540,6 +549,9 @@ protected function checkNodeLinksAccess(array $node_links) { /** * Sorts the menu tree and recursively checks access for each item. + * + * @param array $tree + * The menu tree you wish to operate on. */ protected function doCheckAccess(&$tree) { $new_tree = array(); @@ -551,8 +563,9 @@ protected function doCheckAccess(&$tree) { $this->doCheckAccess($tree[$key]['below']); } // The weights are made a uniform 5 digits by adding 50000 as an offset. - // After _menu_link_translate(), $item['title'] has the localized link title. - // Adding the mlid to the end of the index insures that it is unique. + // After _menu_link_translate(), $item['title'] has the localized link + // title. Adding the mlid to the end of the index insures that it is + // unique. $new_tree[(50000 + $item['weight']) . ' ' . $item['title'] . ' ' . $item['mlid']] = $tree[$key]; } } @@ -575,6 +588,21 @@ public function buildTreeData(array $links, array $parents = array(), $depth = 1 * * The function is a bit complex because the rendering of a link depends on * the next menu link. + * + * @param array $links + * A flat array of menu links that are part of the menu. Each array element + * is an associative array of information about the menu link, containing the + * fields from the {menu_links} table, and optionally additional information + * from the {menu_router} table, if the menu item appears in both tables. + * This array must be ordered depth-first. See _menu_build_tree() for a sample + * query. + * @param array $parents + * An array of the menu link ID values that are in the path from the current + * page to the root of the menu tree. + * @param int $depth + * The minimum depth to include in the returned menu tree. + * + * @return array */ protected function doBuildTreeData(&$links, $parents, $depth) { $tree = array(); @@ -587,8 +615,9 @@ protected function doBuildTreeData(&$links, $parents, $depth) { 'link' => $item, 'below' => array(), ); - // Look ahead to the next link, but leave it on the array so it's available - // to other recursive function calls if we return or build a sub-tree. + // Look ahead to the next link, but leave it on the array so it's + // available to other recursive function calls if we return or build a + // sub-tree. $next = end($links); // Check whether the next link is the first in a new sub-tree. if ($next && $next['depth'] > $depth) { diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuTreeInterface.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuTreeInterface.php index f73b466..29cee05 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuTreeInterface.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuTreeInterface.php @@ -40,9 +40,9 @@ public function output($tree); * Setting this path does not affect the main content; for that use * menu_set_active_item() instead. * - * @param $menu_name + * @param string $menu_name * The name of the affected menu tree. - * @param $path + * @param string $path * The path to use when finding the active trail. */ public function setPath($menu_name, $path = NULL); @@ -50,9 +50,9 @@ public function setPath($menu_name, $path = NULL); /** * Checks access and performs dynamic operations for each link in the tree. * - * @param $tree + * @param array $tree * The menu tree you wish to operate on. - * @param $node_links + * @param array $node_links * A collection of node link references generated from $tree by * menu_tree_collect_node_links(). */ @@ -61,12 +61,12 @@ public function checkAccess(&$tree, $node_links = array()); /** * Gets the path for determining the active trail of the specified menu tree. * - * @param $menu_name + * @param string $menu_name * The menu name of the requested tree. * - * @return + * @return string * A string containing the path. If no path has been specified with - * menu_tree_set_path(), NULL is returned. + * static::setPath(), NULL is returned. */ public function getPath($menu_name); @@ -125,9 +125,9 @@ public function buildPageData($menu_name, $max_depth = NULL, $only_active_trail /** * Collects node links from a given menu tree recursively. * - * @param $tree + * @param array $tree * The menu tree you wish to collect node links from. - * @param $node_links + * @param array $node_links * An array in which to store the collected node links. */ public function collectNodeLinks(&$tree, &$node_links); diff --git a/core/modules/menu_link/tests/Drupal/menu_link/Tests/MenuTreeTest.php b/core/modules/menu_link/tests/Drupal/menu_link/Tests/MenuTreeTest.php index 901a8d7..0c39171 100644 --- a/core/modules/menu_link/tests/Drupal/menu_link/Tests/MenuTreeTest.php +++ b/core/modules/menu_link/tests/Drupal/menu_link/Tests/MenuTreeTest.php @@ -28,12 +28,53 @@ class MenuTreeTest extends UnitTestCase { */ protected $menuTree; + /** + * The mocked database connection. + * + * @var \Drupal\Core\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject + */ protected $connection; + + /** + * The mocked cache backend. + * + * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject + */ protected $cacheBackend; + + /** + * The mocked language manager. + * + * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject + */ protected $languageManager; + + /** + * The test request stack. + * + * @var \Symfony\Component\HttpFoundation\RequestStack. + */ protected $requestStack; + + /** + * The mocked entity manager. + * + * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject + */ protected $entityManager; + + /** + * The mocked entity query factor.y + * + * @var \Drupal\Core\Entity\Query\QueryFactory|\PHPUnit_Framework_MockObject_MockObject + */ protected $entityQueryFactory; + + /** + * The mocked state. + * + * @var \Drupal\Core\KeyValueStore\StateInterface|\PHPUnit_Framework_MockObject_MockObject + */ protected $state; /** @@ -330,12 +371,18 @@ class TestMenuTree extends MenuTree { */ public $menuLinkTranslateCallable; + /** + * {@inheritdoc} + */ protected function menuLinkTranslate(&$item) { if (isset($this->menuLinkTranslateCallable)) { call_user_func_array($this->menuLinkTranslateCallable, array(&$item)); } } + /** + * {@inheritdoc} + */ protected function menuLinkGetPreferred($menu_name, $active_path) { } diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index a97b447..93fa4b2 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -314,8 +314,6 @@ function shortcut_valid_link($path) { * * @return \Drupal\shortcut\ShortcutInterface[] * An array of shortcut links, in the format returned by the menu system. - * - * @see menu_tree() */ function shortcut_renderable_links($shortcut_set = NULL) { $shortcut_links = array(); diff --git a/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php b/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php index 8298193..3b919ae 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php +++ b/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php @@ -8,7 +8,10 @@ namespace Drupal\system\Plugin\Block; use Drupal\block\BlockBase; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\menu_link\MenuTreeInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a generic Menu block. @@ -20,14 +23,43 @@ * derivative = "Drupal\system\Plugin\Derivative\SystemMenuBlock" * ) */ -class SystemMenuBlock extends BlockBase { +class SystemMenuBlock extends BlockBase implements ContainerFactoryPluginInterface { + + /** + * The menu tree. + * + * @var \Drupal\menu_link\MenuTreeInterface + */ + protected $menuTree; + + /** + * Constructs a new SystemMenuBlock. + * + * @param \Drupal\menu_link\MenuTreeInterface $menu_tree + * The menu tree. + */ + public function __construct(MenuTreeInterface $menu_tree) { + $this->menuTree = $menu_tree; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('menu_link.tree') + ); + } /** * {@inheritdoc} */ public function build() { $menu = $this->getDerivativeId(); - return menu_tree($menu); + return $this->menuTree->getRenderedTree($menu); } } diff --git a/core/modules/system/tests/modules/menu_test/menu_test.module b/core/modules/system/tests/modules/menu_test/menu_test.module index 08e6382..1c8a295 100644 --- a/core/modules/system/tests/modules/menu_test/menu_test.module +++ b/core/modules/system/tests/modules/menu_test/menu_test.module @@ -219,8 +219,10 @@ function menu_test_callback() { */ function menu_test_menu_trail_callback() { $menu_path = \Drupal::state()->get('menu_test.menu_tree_set_path') ?: array(); + /** @var \Drupal\menu_link\MenuTreeInterface $menu_tree */ + $menu_tree = \Drupal::service('menu_link.tree'); if (!empty($menu_path)) { - menu_tree_set_path($menu_path['menu_name'], $menu_path['path']); + $menu_tree->setPath($menu_path['menu_name'], $menu_path['path']); } return 'This is menu_test_menu_trail_callback().'; } diff --git a/core/modules/toolbar/toolbar.module b/core/modules/toolbar/toolbar.module index 7217986..ff0bf64 100644 --- a/core/modules/toolbar/toolbar.module +++ b/core/modules/toolbar/toolbar.module @@ -358,6 +358,9 @@ function toolbar_toolbar() { // Add attributes to the links before rendering. toolbar_menu_navigation_links($tree); + /** @var \Drupal\menu_link\MenuTreeInterface $menu_tree */ + $menu_tree = \Drupal::service('menu_link.tree'); + $menu = array( '#heading' => t('Administration menu'), 'toolbar_administration' => array( @@ -365,7 +368,7 @@ function toolbar_toolbar() { '#attributes' => array( 'class' => array('toolbar-menu-administration'), ), - 'administration_menu' => menu_tree_output($tree), + 'administration_menu' => $menu_tree->output($tree), ), ); @@ -417,6 +420,8 @@ function toolbar_toolbar() { */ function toolbar_get_menu_tree() { $tree = array(); + /** @var \Drupal\menu_link\MenuTreeInterface $menu_tree */ + $menu_tree = \Drupal::service('menu_link.tree'); $query = \Drupal::entityQuery('menu_link') ->condition('menu_name', 'admin') ->condition('module', 'system') @@ -424,7 +429,7 @@ function toolbar_get_menu_tree() { $result = $query->execute(); if (!empty($result)) { $admin_link = menu_link_load(reset($result)); - $tree = menu_build_tree('admin', array( + $tree = $menu_tree->buildTree('admin', array( 'expanded' => array($admin_link['mlid']), 'min_depth' => $admin_link['depth'] + 1, 'max_depth' => $admin_link['depth'] + 1, @@ -467,6 +472,8 @@ function toolbar_menu_navigation_links(&$tree) { */ function toolbar_get_rendered_subtrees() { $subtrees = array(); + /** @var \Drupal\menu_link\MenuTreeInterface $menu_tree */ + $menu_tree = \Drupal::service('menu_link.tree'); $tree = toolbar_get_menu_tree(); foreach ($tree as $tree_item) { $item = $tree_item['link']; @@ -479,9 +486,9 @@ function toolbar_get_rendered_subtrees() { $query->condition('p' . $i, $item['p' . $i]); } $parents = $query->execute()->fetchCol(); - $subtree = menu_build_tree($item['menu_name'], array('expanded' => $parents, 'min_depth' => $item['depth']+1)); + $subtree = $menu_tree->buildTreeData($item['menu_name'], array('expanded' => $parents, 'min_depth' => $item['depth']+1)); toolbar_menu_navigation_links($subtree); - $subtree = menu_tree_output($subtree); + $subtree = $menu_tree->output($subtree); $subtree = drupal_render($subtree); } else { diff --git a/core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php b/core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php index bbe5a71..c9dcd30 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php @@ -67,7 +67,9 @@ function testSecondaryMenu() { $this->drupalGet(''); // For a logged-out user, expect no secondary links. - $tree = menu_build_tree('account'); + /** @var \Drupal\menu_link\MenuTreeInterface $menu_tree */ + $menu_tree = \Drupal::service('menu_link.tree'); + $tree = $menu_tree->buildTree('account'); $this->assertEqual(count($tree), 1, 'The secondary links menu contains only one menu link.'); $link = reset($tree); $link = $link['link'];