diff --git a/core/includes/menu.inc b/core/includes/menu.inc index 07792cd..0174882 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -273,7 +273,7 @@ * what the menu blocks offer you. If that's the case, you'll want to: * - Instantiate \Drupal\Core\Menu\MenuTreeParameters, and set its values to * match your needs. Alternatively, you can use - * MenuLinkTree::getDefaultRenderedMenuTreeLinkParameters() to get a typical + * MenuLinkTree::getCurrentRouteMenuTreeParameters() to get a typical * default set of parameters, and then customize them to suit your needs. * - Call \Drupal\Core\MenuLinkTree::load() with your menu link tree parameters, * this will return a menu link tree. @@ -500,7 +500,7 @@ function _menu_get_links_source($name, $default) { function menu_navigation_links($menu_name, $level = 0) { /** @var \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree */ $menu_tree = \Drupal::service('menu.link_tree'); - $parameters = $menu_tree->getDefaultRenderedMenuTreeLinkParameters($menu_name); + $parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name); $parameters->setMaxDepth($level + 1); $tree = $menu_tree->load($menu_name, $parameters); $manipulators = array( diff --git a/core/lib/Drupal/Core/Menu/DefaultMenuLinkTreeManipulators.php b/core/lib/Drupal/Core/Menu/DefaultMenuLinkTreeManipulators.php index 266ee8e..2238a75 100644 --- a/core/lib/Drupal/Core/Menu/DefaultMenuLinkTreeManipulators.php +++ b/core/lib/Drupal/Core/Menu/DefaultMenuLinkTreeManipulators.php @@ -1,5 +1,10 @@ subtree) { $tree[$key]->subtree = $this->generateIndexAndSort($tree[$key]->subtree); } - /** @var \Drupal\Core\Menu\MenuLinkInterface $instance */ $instance = $tree[$key]->link; // The weights are made a uniform 5 digits by adding 50000 as an offset. // After $this->menuLinkCheckAccess(), $instance->getTitle() has the diff --git a/core/lib/Drupal/Core/Menu/MenuLinkTree.php b/core/lib/Drupal/Core/Menu/MenuLinkTree.php index 7f1bed4..baaac0c 100644 --- a/core/lib/Drupal/Core/Menu/MenuLinkTree.php +++ b/core/lib/Drupal/Core/Menu/MenuLinkTree.php @@ -76,7 +76,7 @@ public function __construct(MenuTreeStorageInterface $tree_storage, MenuLinkMana /** * {@inheritdoc} */ - public function getDefaultRenderedMenuTreeLinkParameters($menu_name) { + public function getCurrentRouteMenuTreeParameters($menu_name) { $active_trail = $this->menuActiveTrail->getActiveTrailIds($menu_name); $parameters = new MenuTreeParameters(); diff --git a/core/lib/Drupal/Core/Menu/MenuLinkTreeElement.php b/core/lib/Drupal/Core/Menu/MenuLinkTreeElement.php index beb6446..db5cbc9 100644 --- a/core/lib/Drupal/Core/Menu/MenuLinkTreeElement.php +++ b/core/lib/Drupal/Core/Menu/MenuLinkTreeElement.php @@ -89,8 +89,13 @@ class MenuLinkTreeElement { * @param \Drupal\Core\Menu\MenuLinkInterface $link * The menu link for this element in the menu link tree. * @param bool $has_children + * A flag as to whether this element has children even if they are not + * included in the tree (i.e. this may be TRUE even if $subtree is empty). * @param int $depth + * The depth of this element relative to the tree root. * @param bool $in_active_trail + * A flag as to whether this link was included in the list of active trail + * IDs used to build the tree. * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $subtree * The children of this element in the menu link tree. */ diff --git a/core/lib/Drupal/Core/Menu/MenuLinkTreeInterface.php b/core/lib/Drupal/Core/Menu/MenuLinkTreeInterface.php index 7e99998..df983fe 100644 --- a/core/lib/Drupal/Core/Menu/MenuLinkTreeInterface.php +++ b/core/lib/Drupal/Core/Menu/MenuLinkTreeInterface.php @@ -20,7 +20,7 @@ * language. * Which links are loaded can be specified in the menu link tree parameters that * passed to ::load(). You can build your own set of parameter, but you can also - * start from a typical default (::getDefaultRenderedMenuTreeLinkParameters()). + * start from a typical default (::getCurrentRouteMenuTreeParameters()). * * @see \Drupal\Core\Menu\MenuLinkTreeParameters * @@ -56,10 +56,10 @@ * The menu name, needed for retrieving the active trail and links with the * 'expanded' flag enabled. * - * @return \Drupal\Core\Menu\MenuTreeParameters $parameters + * @return \Drupal\Core\Menu\MenuTreeParameters * The parameters to determine which menu links to be loaded into a tree. */ - public function getDefaultRenderedMenuTreeLinkParameters($menu_name); + public function getCurrentRouteMenuTreeParameters($menu_name); /** * Loads a menu tree with a menu link plugin instance at each element. diff --git a/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php b/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php index 6732a1a..ae22631 100644 --- a/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php +++ b/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php @@ -27,6 +27,14 @@ class MenuParentFormSelector implements MenuParentFormSelectorInterface { */ protected $entityManager; + /** + * Constructs a \Drupal\Core\Menu\MenuParentFormSelector + * + * @param MenuLinkTreeInterface $menu_link_tree + * The menu link tree service. + * @param EntityManagerInterface $entity_manager + * The entity manager. + */ public function __construct(MenuLinkTreeInterface $menu_link_tree, EntityManagerInterface $entity_manager) { $this->menuLinkTree = $menu_link_tree; $this->entityManager = $entity_manager; @@ -61,7 +69,7 @@ public function getParentSelectOptions($id = '', array $menus = NULL) { /** * {@inheritdoc} */ - public function parentSelectElement($menu_parent, $id = '', array $menus = NULL) { + public function parentSelectElement($menu_parent, $id = '', array $menus = NULL) { $options = $this->getParentSelectOptions($id, $menus); // If no options were found, there is nothing to select. if ($options) { @@ -103,7 +111,7 @@ protected function getParentDepthLimit($id) { /** * Iterates over all items in the tree to prepare the parents select options. * - * @param array $tree + * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree * The menu tree. * @param string $menu_name * The menu name. @@ -122,7 +130,6 @@ protected function parentSelectOptionsTreeWalk(array $tree, $menu_name, $indent, // Don't iterate through any links on this level. break; } - /** @var \Drupal\Core\Menu\MenuLinkInterface $link */ $link = $element->link; if ($link->getPluginId() != $exclude) { $title = $indent . ' ' . Unicode::truncate($link->getTitle(), 30, TRUE, FALSE); diff --git a/core/lib/Drupal/Core/Menu/MenuParentFormSelectorInterface.php b/core/lib/Drupal/Core/Menu/MenuParentFormSelectorInterface.php index d88bcba..fdcc4b6 100644 --- a/core/lib/Drupal/Core/Menu/MenuParentFormSelectorInterface.php +++ b/core/lib/Drupal/Core/Menu/MenuParentFormSelectorInterface.php @@ -53,4 +53,4 @@ public function getParentSelectOptions($id = '', array $menus = NULL); */ public function parentSelectElement($menu_parent, $id = '', array $menus = NULL); -} \ No newline at end of file +} diff --git a/core/lib/Drupal/Core/Menu/MenuTreeStorage.php b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php index b2d5c5c..5367622 100644 --- a/core/lib/Drupal/Core/Menu/MenuTreeStorage.php +++ b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php @@ -55,7 +55,7 @@ class MenuTreeStorage implements MenuTreeStorageInterface { protected $options = array(); /** - * Stores the definitions that have already been loaded for better performance. + * Stores definitions that have already been loaded for better performance. */ protected $definitions = array(); @@ -570,7 +570,17 @@ protected function updateParentalStatus(array $link) { } } - protected function prepareLink($link, $intersect = FALSE) { + /** + * Prepare a link by unserializing values and saving the definition. + * + * @param array $link + * The data loaded in the query. + * @param bool $intersect + * If TRUE, filter out values that are not part of the actual definition. + * @return array + * The prepared link data. + */ + protected function prepareLink(array $link, $intersect = FALSE) { foreach ($this->serializedFields() as $name) { $link[$name] = unserialize($link[$name]); } @@ -789,6 +799,7 @@ public function loadTreeData($menu_name, MenuTreeParameters $parameters) { * The parameters to determine which menu links to be loaded into a tree. * ::loadLinks() will set the absolute minimum depth, which is used * ::doBuildTreeData(). + * * @return array * 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 @@ -1035,8 +1046,8 @@ protected function treeDataRecursive(array &$links, array $parents, $depth) { $tree[$tree_link_definition['id']] = array( 'definition' => $this->prepareLink($tree_link_definition, TRUE), 'has_children' => $tree_link_definition['has_children'], - // We need to determine if we're on the path to root so we can later build - // the correct active trail. + // We need to determine if we're on the path to root so we can later + // build the correct active trail. 'in_active_trail' => in_array($tree_link_definition['id'], $parents), 'subtree' => array(), 'depth' => $tree_link_definition['depth'], diff --git a/core/modules/menu_link_content/src/Tests/MenuLinkContentUITest.php b/core/modules/menu_link_content/src/Tests/MenuLinkContentUITest.php index 6d2bed0..052495a 100644 --- a/core/modules/menu_link_content/src/Tests/MenuLinkContentUITest.php +++ b/core/modules/menu_link_content/src/Tests/MenuLinkContentUITest.php @@ -19,7 +19,12 @@ class MenuLinkContentUITest extends ContentTranslationUITest { * * @var array */ - public static $modules = array('language', 'content_translation', 'menu_link_content', 'menu_ui'); + public static $modules = array( + 'language', + 'content_translation', + 'menu_link_content', + 'menu_ui', + ); /** * {@inheritdoc} diff --git a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php index d2f9e84..9a93288 100644 --- a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php @@ -79,7 +79,7 @@ public static function create(ContainerInterface $container, array $configuratio */ public function build() { $menu_name = $this->getDerivativeId(); - $parameters = $this->menuTree->getDefaultRenderedMenuTreeLinkParameters($menu_name); + $parameters = $this->menuTree->getCurrentRouteMenuTreeParameters($menu_name); $tree = $this->menuTree->load($menu_name, $parameters); $manipulators = array( array('callable' => 'menu.default_tree_manipulators:checkAccess'), diff --git a/core/modules/system/src/Tests/Menu/MenuLinkTreeTest.php b/core/modules/system/src/Tests/Menu/MenuLinkTreeTest.php index 4b2af79..86b72ec 100644 --- a/core/modules/system/src/Tests/Menu/MenuLinkTreeTest.php +++ b/core/modules/system/src/Tests/Menu/MenuLinkTreeTest.php @@ -38,7 +38,12 @@ class MenuLinkTreeTest extends KernelTestBase { * * @var array */ - public static $modules = array('system', 'menu_test', 'menu_link_content', 'field'); + public static $modules = array( + 'system', + 'menu_test', + 'menu_link_content', + 'field', + ); /** * {@inheritdoc} @@ -90,6 +95,9 @@ public function testDeleteLinksInMenu() { $this->assertEqual(count($output), 1); } + /** + * Tests creating links with an expected tree structure. + */ public function testCreateLinksInMenu() { /** * This creates a tree with the following structure: diff --git a/core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php b/core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php index 0df4c1e..8064984 100644 --- a/core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php +++ b/core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php @@ -133,7 +133,7 @@ public function testMenuLinkMoving() { $this->assertMenuLink('test1', array('has_children' => 1, 'depth' => 1), array(), array('test2', 'test3')); $this->assertMenuLink('test2', array('has_children' => 1, 'depth' => 2), array('test1'), array('test3')); - $this->assertMenuLink('test4', array('has_children' => 1, 'depth' => 1), array(), array('test5', 'test6')); + $this->assertMenuLink('test4', array('has_children' => 1, 'depth' => 1), array(), array('test5', 'test6')); $this->assertMenuLink('test5', array('has_children' => 1, 'depth' => 2), array('test4'), array('test6')); $this->assertMenuLink('test6', array('has_children' => 0, 'depth' => 3), array('test5', 'test4')); @@ -391,8 +391,8 @@ protected function assertMenuLink($id, array $expected_properties, array $parent $found_children = array_keys($this->treeStorage->loadAllChildren($id)); // We need both these checks since the 2nd will pass if there are extra // IDs loaded in $found_children. - $this->assertEqual(count($children), count($found_children)); //, "Found expected number of children for $id"); - $this->assertEqual(array_intersect($children, $found_children), $children); //, 'Child IDs match'); + $this->assertEqual(count($children), count($found_children), "Found expected number of children for $id"); + $this->assertEqual(array_intersect($children, $found_children), $children, 'Child IDs match'); } } diff --git a/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php b/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php index 8962271..e536f2c 100644 --- a/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php @@ -79,6 +79,8 @@ protected function setUp() { } /** + * Helper function to create a mock tree. + * * This mocks a tree with the following structure: * - 1 * - 2 @@ -133,9 +135,9 @@ public function testGenerateIndexAndSort() { $this->assertEquals($this->links[6]->getPluginId(), $tree['50000 barbar test.example6']->link->getPluginId()); $this->assertEquals($this->links[8]->getPluginId(), $tree['50000 quxqux test.example8']->link->getPluginId()); - // Validate that child element #4 exists at the correct location in the hierarchy. + // Verify that child element #4 is at the correct location in the hierarchy. $this->assertEquals($this->links[4]->getPluginId(), $tree['50000 bar test.example2']->subtree['50000 baz test.example3']->subtree['50000 qux test.example4']->link->getPluginId()); - // Validate that child element #7 exists at the correct location in the hierarchy. + // Verify that child element #7 is at the correct location in the hierarchy. $this->assertEquals($this->links[7]->getPluginId(), $tree['50000 foofoo test.example5']->subtree['50000 bazbaz test.example7']->link->getPluginId()); }