diff --git a/core/includes/menu.inc b/core/includes/menu.inc index 6aae567..d7da9cc 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -435,7 +435,7 @@ function menu_tree_get_path($menu_name) { * 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')->buildAllData($menu_name, $max_depth, $only_active_trail); + return \Drupal::service('menu_link.tree')->buildPageData($menu_name, $max_depth, $only_active_trail); } /** 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 c9f69b7..c2b574f 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuTree.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuTree.php @@ -17,7 +17,10 @@ use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\RequestStack; -class MenuTree { +/** + * Provides the default implementation of a menu tree. + */ +class MenuTree implements MenuTreeInterface { /** * The database connection. @@ -110,6 +113,24 @@ class MenuTree { */ protected $menuPageTrees; + /** + * Constructs a new MenuTree. + * + * @param \Drupal\Core\Database\Connection $database + * The database connection. + * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend + * The cache backend. + * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager + * The language manager. + * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack + * The request stack. + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager. + * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query_factory + * The entity query factory. + * @param \Drupal\Core\KeyValueStore\StateInterface $state + * The state. + */ public function __construct(Connection $database, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager, RequestStack $request_stack, EntityManagerInterface $entity_manager, QueryFactory $entity_query_factory, StateInterface $state) { $this->database = $database; $this->cache = $cache_backend; @@ -121,24 +142,7 @@ public function __construct(Connection $database, CacheBackendInterface $cache_b } /** - * 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 string $menu_name - * The named menu links to return - * @param array $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 int $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 array - * An tree of menu links in an array, in the order they should be rendered. + * {@inheritdoc} */ public function buildAllData($menu_name, $link = NULL, $max_depth = NULL) { $language_interface = $this->languageManager->getCurrentLanguage(); @@ -189,26 +193,7 @@ public function buildAllData($menu_name, $link = NULL, $max_depth = NULL) { } /** - * 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 string $menu_name - * The named menu links to return. - * @param int $max_depth - * (optional) The maximum depth of links to retrieve. - * @param bool $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 array - * 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. + * {@inheritdoc} */ public function buildPageData($menu_name, $max_depth = NULL, $only_active_trail = FALSE) { $language_interface = $this->languageManager->getCurrentLanguage(); @@ -326,21 +311,7 @@ public function buildPageData($menu_name, $max_depth = NULL, $only_active_trail } /** - * 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. + * {@inheritdoc} */ public function setPath($menu_name, $path = NULL) { if (isset($path)) { @@ -349,28 +320,14 @@ public function setPath($menu_name, $path = NULL) { } /** - * 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. + * {@inheritdoc} */ public function getPath($menu_name) { return isset($this->trailPaths[$menu_name]) ? $this->trailPaths[$menu_name] : NULL; } /** - * Renders a menu tree based on the current path. - * - * @param string $menu_name - * The name of the menu. - * - * @return array - * A structured array representing the specified menu on the current page, to - * be rendered by drupal_render(). + * {@inheritdoc} */ public function getRenderedTree($menu_name) { @@ -382,18 +339,7 @@ public function 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 array $tree - * A data structure representing the tree as returned from menu_tree_data. - * - * @return array - * A structured array to be rendered by drupal_render(). + * {@inheritdoc} */ public function output($tree) { $build = array(); @@ -455,28 +401,7 @@ public function output($tree) { } /** - * Builds a menu tree, translates links, and checks access. - * - * @param string $menu_name - * The name of the menu. - * @param array $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 array - * A fully built menu tree. + * {@inheritdoc} */ public function buildTree($menu_name, array $parameters = array()) { // Build the menu tree. @@ -558,12 +483,7 @@ protected function doBuildTree($menu_name, array $parameters = array()) { } /** - * 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. + * {@inheritdoc} */ public function collectNodeLinks(&$tree, &$node_links) { foreach ($tree as $key => $v) { @@ -581,13 +501,7 @@ public function 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(). + * {@inheritdoc} */ public function checkAccess(&$tree, $node_links = array()) { if ($node_links) { @@ -596,7 +510,16 @@ public function checkAccess(&$tree, $node_links = array()) { $this->doCheckAccess($tree); } - protected function checkNodeLinksAccess($node_links) { + /** + * Checks access on node links using the node_access query tag. + * + * @param array $node_links + * An array of node links keyed by NID. + * + * @return mixed + * The node links. + */ + protected function checkNodeLinksAccess(array $node_links) { $nids = array_keys($node_links); $select = $this->database->select('node_field_data', 'n'); $select->addField('n', 'nid'); @@ -639,30 +562,7 @@ protected function doCheckAccess(&$tree) { } /** - * Sorts and returns the built data representing a menu tree. - * - * @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 - * 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. + * {@inheritdoc} */ public function buildTreeData(array $links, array $parents = array(), $depth = 1) { // Reverse the array so we can use the more efficient array_pop() function. @@ -705,12 +605,18 @@ protected function doBuildTreeData(&$links, $parents, $depth) { return $tree; } + /** + * Wraps menu_link_get_preferred(). + */ protected function menuLinkGetPreferred($menu_name, $active_path) { return menu_link_get_preferred($active_path, $menu_name); } + /** + * Wraps _menu_link_translate(). + */ protected function menuLinkTranslate(&$item) { - return _menu_link_translate($item); + _menu_link_translate($item); } } 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 5ddfce6..f73b466 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuTreeInterface.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuTreeInterface.php @@ -1,17 +1,17 @@