diff --git a/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php b/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php index 964f5de..fa83457 100644 --- a/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php +++ b/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php @@ -109,8 +109,12 @@ public function buildConfigurationForm(array $form, array &$form_state) { '#type' => 'item', '#title' => $this->t('This link is provided by the @name module. The label and path cannot be edited.', array('@name' => $this->getModuleName($provider))), ); + $link = array( + '#type' => 'link', + '#title' => $this->menuLink->getTitle(), + ) + $this->menuLink->getUrlObject()->toRenderArray(); $form['path'] = array( - 'link' => $this->menuLink->build(), + 'link' => $link, '#type' => 'item', '#title' => $this->t('Link'), ); diff --git a/core/lib/Drupal/Core/Menu/MenuLinkBase.php b/core/lib/Drupal/Core/Menu/MenuLinkBase.php index 7ad3de5..031b272 100644 --- a/core/lib/Drupal/Core/Menu/MenuLinkBase.php +++ b/core/lib/Drupal/Core/Menu/MenuLinkBase.php @@ -24,25 +24,6 @@ protected $overrideAllowed = array(); /** - * {@inheritdoc} - */ - public function build($title_attribute = TRUE) { - $options = $this->getOptions(); - $description = $this->getDescription(); - if ($title_attribute && $description) { - $options['attributes']['title'] = $description; - } - $build = array( - '#type' => 'link', - '#route_name' => $this->pluginDefinition['route_name'], - '#route_parameters' => $this->pluginDefinition['route_parameters'], - '#title' => $this->getTitle(), - '#options' => $options, - ); - return $build; - } - - /** * Returns the weight of the menu link. * * @return int diff --git a/core/lib/Drupal/Core/Menu/MenuLinkInterface.php b/core/lib/Drupal/Core/Menu/MenuLinkInterface.php index 169a0a5..36d8520 100644 --- a/core/lib/Drupal/Core/Menu/MenuLinkInterface.php +++ b/core/lib/Drupal/Core/Menu/MenuLinkInterface.php @@ -16,17 +16,6 @@ interface MenuLinkInterface extends PluginInspectionInterface, DerivativeInspectionInterface { /** - * Return a localized link render array. - * - * @param bool $title_attribute - * If TRUE, add the link description (if present) as the title attribute. - * - * @return array - * A render array with localized or translated strings. - */ - public function build($title_attribute = TRUE); - - /** * Returns the weight of the menu link. * * @return int diff --git a/core/lib/Drupal/Core/Menu/MenuLinkTree.php b/core/lib/Drupal/Core/Menu/MenuLinkTree.php index baaac0c..a0f3416 100644 --- a/core/lib/Drupal/Core/Menu/MenuLinkTree.php +++ b/core/lib/Drupal/Core/Menu/MenuLinkTree.php @@ -82,11 +82,11 @@ public function getCurrentRouteMenuTreeParameters($menu_name) { $parameters = new MenuTreeParameters(); $parameters->setActiveTrail($active_trail) // We want links in the active trail to be expanded. - ->addExpanded($active_trail) + ->addExpandedParents($active_trail) // We marked the links in the active trail to be expanded, but we also // want their descendants that have the "expanded" flag enabled to be // expanded. - ->addExpanded($this->treeStorage->getExpanded($menu_name, $active_trail)); + ->addExpandedParents($this->treeStorage->getExpanded($menu_name, $active_trail)); return $parameters; } @@ -218,4 +218,11 @@ public function getSubtreeHeight($id) { return $this->treeStorage->getSubtreeHeight($id); } + /** + * {@inheritdoc} + */ + public function getExpanded($menu_name, array $parents) { + return $this->treeStorage->getExpanded($menu_name, $parents); + } + } diff --git a/core/lib/Drupal/Core/Menu/MenuLinkTreeInterface.php b/core/lib/Drupal/Core/Menu/MenuLinkTreeInterface.php index df983fe..9b1f5df 100644 --- a/core/lib/Drupal/Core/Menu/MenuLinkTreeInterface.php +++ b/core/lib/Drupal/Core/Menu/MenuLinkTreeInterface.php @@ -126,4 +126,17 @@ public function maxDepth(); */ public function getSubtreeHeight($id); + /** + * Find expanded links in a menu given a set of possible parents. + * + * @param string $menu_name + * The menu name. + * @param array $parents + * One or more parent IDs to match. + * + * @return array + * The menu link IDs that are flagged as expanded in this menu. + */ + public function getExpanded($menu_name, array $parents); + } diff --git a/core/lib/Drupal/Core/Menu/MenuTreeParameters.php b/core/lib/Drupal/Core/Menu/MenuTreeParameters.php index 4976598..d8adb40 100644 --- a/core/lib/Drupal/Core/Menu/MenuTreeParameters.php +++ b/core/lib/Drupal/Core/Menu/MenuTreeParameters.php @@ -13,8 +13,8 @@ * Menu tree parameters are used to determine the set of definitions to be * loaded from \Drupal\Core\Menu\MenuTreeStorageInterface. Hence they determine * the shape and content of the tree: - * - which links should be expanded, i.e. subtrees will get loaded that may not - * be loaded otherwise + * - which parent IDs should be used to restrict the tree, i.e. only links with + * a parent in the list will be included. * - which menu links are omitted, i.e. minimum and maximum depth * * @todo - add getter methods and make all properties protected. @@ -51,19 +51,19 @@ class MenuTreeParameters { public $maxDepth = NULL; /** - * An array of parent link ids to return only menu links that are children of - * one of the menu link plugin IDs in this list. If empty, the whole menu tree - * is built. - * - * Defaults to the empty array. + * An array of parent link IDs. This restricts the tree to only menu links + * that are at the top level or have a parent ID in this list. If empty, the + * whole menu tree is built. * * @var string[] */ - public $expanded = array(); + public $expandedParents = array(); /** * An array of menu link plugin IDs, representing the trail from the currently - * active menu link to the ("real") root of that menu link's menu. + * active menu link to the ("real") root of that menu link's menu. This does + * not affect the way the tree is built, it only is used to set the value of + * the inActiveTrail property for each tree element. * * Defaults to the empty array. * @@ -125,22 +125,21 @@ public function setMaxDepth($max_depth) { } /** - * Adds menu links to be expanded (whose children to show). + * Adds parent menu links IDs to restrict the tree (only show children). * - * @param string[] $expanded - * An array containing the links to be expanded: a list of menu link plugin - * IDs. + * @param string[] $parents + * An array containing the parent IDs to limit the tree. * * @return $this */ - public function addExpanded(array $expanded) { - $this->expanded = array_merge($this->expanded, $expanded); - $this->expanded = array_unique($this->expanded); + public function addExpandedParents(array $parents) { + $this->expandedParents = array_merge($this->expandedParents, $parents); + $this->expandedParents = array_unique($this->expandedParents); return $this; } /** - * Sets the active trail. + * Sets the active trail IDs used to set the inActiveTrail property. * * @param string[] $active_trail * An array containing the active trail: a list of menu link plugin IDs. diff --git a/core/lib/Drupal/Core/Menu/MenuTreeStorage.php b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php index 5367622..89e0c80 100644 --- a/core/lib/Drupal/Core/Menu/MenuTreeStorage.php +++ b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php @@ -766,7 +766,7 @@ protected function saveRecursive($id, &$children, &$links) { public function loadTreeData($menu_name, MenuTreeParameters $parameters) { // Build the cache id; sort 'expanded' and 'conditions' to prevent duplicate // cache items. - sort($parameters->expanded); + sort($parameters->expandedParents); sort($parameters->conditions); // @todo - may be able to skip hashing after https://drupal.org/node/2224847 $tree_cid = "tree-data:$menu_name:" . hash('sha256', serialize($parameters)); @@ -862,8 +862,8 @@ protected function loadLinks($menu_name, MenuTreeParameters $parameters) { $query->condition('menu_name', $menu_name); - if (!empty($parameters->expanded)) { - $query->condition('parent', $parameters->expanded, 'IN'); + if (!empty($parameters->expandedParents)) { + $query->condition('parent', $parameters->expandedParents, 'IN'); } if (isset($parameters->minDepth) && $parameters->minDepth > 1) { $query->condition('depth', $parameters->minDepth, '>='); diff --git a/core/tests/Drupal/Tests/Core/Menu/MenuLinkTreeParametersTest.php b/core/tests/Drupal/Tests/Core/Menu/MenuLinkTreeParametersTest.php index e4da62d..f0118db 100644 --- a/core/tests/Drupal/Tests/Core/Menu/MenuLinkTreeParametersTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/MenuLinkTreeParametersTest.php @@ -68,29 +68,29 @@ public function testSetMinDepth($min_depth, $expected) { } /** - * Tests addExpanded(). + * Tests addExpandedParents(). * - * @covers ::addExpanded + * @covers ::addExpandedParents */ public function testAddExpanded() { $parameters = new MenuTreeParameters(); // Verify default value. - $this->assertEquals(array(), $parameters->expanded); + $this->assertEquals(array(), $parameters->expandedParents); // Add actual menu link plugin IDs to be expanded. - $parameters->addExpanded(array('foo', 'bar', 'baz')); - $this->assertEquals(array('foo', 'bar', 'baz'), $parameters->expanded); + $parameters->addExpandedParents(array('foo', 'bar', 'baz')); + $this->assertEquals(array('foo', 'bar', 'baz'), $parameters->expandedParents); // Add additional menu link plugin IDs; they should be merged, not replacing // the old ones. - $parameters->addExpanded(array('qux', 'quux')); - $this->assertEquals(array('foo', 'bar', 'baz', 'qux', 'quux'), $parameters->expanded); + $parameters->addExpandedParents(array('qux', 'quux')); + $this->assertEquals(array('foo', 'bar', 'baz', 'qux', 'quux'), $parameters->expandedParents); // Add pre-existing menu link plugin IDs; they should not be added again; // this is a set. - $parameters->addExpanded(array('bar', 'quux')); - $this->assertEquals(array('foo', 'bar', 'baz', 'qux', 'quux'), $parameters->expanded); + $parameters->addExpandedParents(array('bar', 'quux')); + $this->assertEquals(array('foo', 'bar', 'baz', 'qux', 'quux'), $parameters->expandedParents); } /**