diff --git a/core/core.services.yml b/core/core.services.yml index d756158..098e715 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -266,7 +266,10 @@ services: arguments: ['@menu.tree_storage', '@menu_link.static.overrides', '@module_handler'] menu.link_tree: class: Drupal\Core\Menu\MenuLinkTree - arguments: ['@menu.tree_storage', '@plugin.manager.menu.link', '@request_stack', '@router.route_provider', '@cache.menu', '@access_manager', '@current_user', '@entity.manager', '@config.factory'] + arguments: ['@menu.tree_storage', '@plugin.manager.menu.link', '@request_stack', '@router.route_provider', '@cache.menu', '@access_manager', '@current_user', '@config.factory'] + menu.parent_form_selector: + class: Drupal\Core\Menu\MenuParentFormSelector + arguments: ['@menu.link_tree', '@entity.manager'] plugin.manager.menu.local_action: class: Drupal\Core\Menu\LocalActionManager arguments: ['@controller_resolver', '@request_stack', '@router.route_provider', '@module_handler', '@cache.discovery', '@language_manager', '@access_manager', '@current_user'] diff --git a/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php b/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php index baa50ed..964f5de 100644 --- a/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php +++ b/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php @@ -10,7 +10,7 @@ use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Menu\MenuLinkInterface; use Drupal\Core\Menu\MenuLinkManagerInterface; -use Drupal\Core\Menu\MenuLinkTreeInterface; +use Drupal\Core\Menu\MenuParentFormSelectorInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; @@ -42,9 +42,9 @@ class MenuLinkDefaultForm implements MenuLinkFormInterface, ContainerInjectionIn /** * The menu tree service. * - * @var \Drupal\Core\Menu\MenuLinkTreeInterface + * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface */ - protected $menuTree; + protected $menuParentSelector; /** * The module handler service. @@ -65,16 +65,16 @@ class MenuLinkDefaultForm implements MenuLinkFormInterface, ContainerInjectionIn * * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager * The menu link manager. - * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree - * The menu tree service. + * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector + * The menu parent form selector service. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The string translation. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler; */ - public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuLinkTreeInterface $menu_tree, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler) { + public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuParentFormSelectorInterface $menu_parent_selector, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler) { $this->menuLinkManager = $menu_link_manager; - $this->menuTree = $menu_tree; + $this->menuParentSelector = $menu_parent_selector; $this->stringTranslation = $string_translation; $this->moduleHandler = $module_handler; } @@ -85,7 +85,7 @@ public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuLin public static function create(ContainerInterface $container) { return new static( $container->get('plugin.manager.menu.link'), - $container->get('menu.link_tree'), + $container->get('menu.parent_form_selector'), $container->get('string_translation'), $container->get('module_handler') ); @@ -138,9 +138,9 @@ public function buildConfigurationForm(array $form, array &$form_state) { ); $menu_parent = $this->menuLink->getMenuName() . ':' . $this->menuLink->getParent(); - $form['menu_parent'] = $this->menuTree->parentSelectElement($menu_parent, $this->menuLink->getPluginId()); + $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($menu_parent, $this->menuLink->getPluginId()); $form['menu_parent']['#title'] = $this->t('Parent link'); - $form['menu_parent']['#description'] = $this->t('The maximum depth for a link and all its children is fixed at !maxdepth. Some menu links may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => $this->menuTree->maxDepth())); + $form['menu_parent']['#description'] = $this->t('The maximum depth for a link and all its children is fixed. Some menu links may not be available as parents if selecting them would exceed this limit.'); $form['menu_parent']['#attributes']['class'][] = 'menu-title-select'; return $form; diff --git a/core/lib/Drupal/Core/Menu/MenuLinkTree.php b/core/lib/Drupal/Core/Menu/MenuLinkTree.php index 8997161..30942f2 100644 --- a/core/lib/Drupal/Core/Menu/MenuLinkTree.php +++ b/core/lib/Drupal/Core/Menu/MenuLinkTree.php @@ -129,13 +129,6 @@ class MenuLinkTree implements MenuLinkTreeInterface { protected $account; /** - * The entity manager. - * - * @var \Drupal\Core\Entity\EntityManagerInterface - */ - protected $entityManager; - - /** * The configuration factory. * * @var \Drupal\Core\Config\ConfigFactoryInterface @@ -198,12 +191,10 @@ class MenuLinkTree implements MenuLinkTreeInterface { * The access manager. * @param \Drupal\Core\Session\AccountInterface $account * The current user. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * Configuration factory. */ - public function __construct(MenuTreeStorageInterface $tree_storage, MenuLinkManagerInterface $menu_link_manager, RequestStack $request_stack, RouteProviderInterface $route_provider, CacheBackendInterface $tree_cache_backend, AccessManager $access_manager, AccountInterface $account, EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory) { + public function __construct(MenuTreeStorageInterface $tree_storage, MenuLinkManagerInterface $menu_link_manager, RequestStack $request_stack, RouteProviderInterface $route_provider, CacheBackendInterface $tree_cache_backend, AccessManager $access_manager, AccountInterface $account, ConfigFactoryInterface $config_factory) { $this->treeStorage = $tree_storage; $this->menuLinkManager = $menu_link_manager; $this->requestStack = $request_stack; @@ -211,7 +202,6 @@ public function __construct(MenuTreeStorageInterface $tree_storage, MenuLinkMana $this->accessManager = $access_manager; $this->account = $account; $this->treeCacheBackend = $tree_cache_backend; - $this->entityManager = $entity_manager; $this->configFactory = $config_factory; } @@ -225,6 +215,13 @@ public function maxDepth() { /** * {@inheritdoc} */ + public function getSubtreeHeight($id) { + return $this->treeStorage->getSubtreeHeight($id); + } + + /** + * {@inheritdoc} + */ public function buildRenderTree($tree) { $build = array(); @@ -647,110 +644,6 @@ protected function menuLinkCheckAccess(array $definition) { /** * {@inheritdoc} */ - public function getParentSelectOptions($id = '', array $menus = NULL) { - if (!isset($menus)) { - $menus = $this->getMenuOptions(); - } - - $options = array(); - $depth_limit = $this->getParentDepthLimit($id); - foreach ($menus as $menu_name => $menu_title) { - $options[$menu_name . ':'] = '<' . $menu_title . '>'; - - $tree = $this->buildAllData($menu_name, NULL, $depth_limit); - $this->parentSelectOptionsTreeWalk($tree, $menu_name, '--', $options, $id, $depth_limit); - } - return $options; - } - - /** - * {@inheritdoc} - */ - 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) { - if (!isset($options[$menu_parent])) { - // Try putting it at the top level in the current menu. - list($menu_name, $parent) = explode(':', $menu_parent, 2); - $menu_parent = $menu_name . ':'; - } - if (isset($options[$menu_parent])) { - return array( - '#type' => 'select', - '#options' => $options, - '#default_value' => $menu_parent, - ); - } - } - return array(); - } - - /** - * {@inheritdoc} - */ - public function getParentDepthLimit($id) { - if ($id) { - $limit = $this->treeStorage->maxDepth() - $this->treeStorage->getSubtreeHeight($id); - } - else { - $limit = $this->treeStorage->maxDepth() - 1; - } - return $limit; - } - - /** - * Iterates over all items in the tree to prepare the parents select options. - * - * @param array $tree - * The menu tree. - * @param string $menu_name - * The menu name. - * @param string $indent - * The indentation string used for the label. - * @param array $options - * The select options. - * @param string $exclude - * An excluded menu link. - * @param int $depth_limit - * The maximum depth of menu links considered for the select options. - */ - protected function parentSelectOptionsTreeWalk(array $tree, $menu_name, $indent, array &$options, $exclude, $depth_limit) { - foreach ($tree as $data) { - if ($data['depth'] > $depth_limit) { - // Don't iterate through any links on this level. - break; - } - /** @var \Drupal\Core\Menu\MenuLinkInterface $link */ - $link = $data['link']; - if ($link->getPluginId() != $exclude) { - $title = $indent . ' ' . Unicode::truncate($link->getTitle(), 30, TRUE, FALSE); - if ($link->isHidden()) { - $title .= ' (' . t('disabled') . ')'; - } - $options[$menu_name . ':' . $link->getPluginId()] = $title; - if ($data['below']) { - $this->parentSelectOptionsTreeWalk($data['below'], $menu_name, $indent . '--', $options, $exclude, $depth_limit); - } - } - } - } - - /** - * {@inheritdoc} - */ - public function getMenuOptions(array $menu_names = NULL) { - $menus = $this->entityManager->getStorage('menu')->loadMultiple($menu_names); - $options = array(); - foreach ($menus as $menu) { - $options[$menu->id()] = $menu->label(); - } - return $options; - } - - /** - * {@inheritdoc} - */ public function resetStaticCache() { $this->menuTree = array(); $this->buildAllDataParameters = array(); diff --git a/core/lib/Drupal/Core/Menu/MenuLinkTreeInterface.php b/core/lib/Drupal/Core/Menu/MenuLinkTreeInterface.php index 7b49790..01dd634 100644 --- a/core/lib/Drupal/Core/Menu/MenuLinkTreeInterface.php +++ b/core/lib/Drupal/Core/Menu/MenuLinkTreeInterface.php @@ -21,6 +21,18 @@ public function maxDepth(); /** + * Finds the height of a subtree rooted by of the given ID. + * + * @param string $id + * The the ID of an item in the storage. + * + * @return int + * Returns the height of the subtree. This will be at least 1 if the ID + * exists, or 0 if the ID does not exist in the storage. + */ + public function getSubtreeHeight($id); + + /** * Returns a menu tree ready to be rendered. * * The menu item's LI element is given one of the following classes: @@ -187,69 +199,6 @@ public function getChildLinks($id, $max_relative_depth = NULL); public function menuLinkGetPreferred($route_name = NULL, array $route_parameters = array(), $selected_menu = NULL); /** - * Gets the options for a select element to choose a menu and parent. - * - * @param string $id - * Optional ID of a link plugin. This will exclude the link and its - * children from the select options. - * @param array $menus - * Optional array of menu names as keys and titles as values to limit - * the select options. If NULL, all menus will be included. - * - * @return array - * Keyed array where the keys are contain a menu name and parent ID and - * the values are a menu name or link title indented by depth. - */ - public function getParentSelectOptions($id = '', array $menus = NULL); - - /** - * Get a form element to choose a menu and parent. - * - * The specific type of form element will vary depending on the - * implementation, but callers will normally need to set the #title for the - * element. - * - * @param string $menu_parent - * A menu name and parent ID concatenated with a ':' character to use as the - * default value. - * @param string $id - * Optional ID of a link plugin. This will exclude the link and its - * children from being selected. - * @param array $menus - * Optional array of menu names as keys and titles as values to limit - * the values that may be selected. If NULL, all menus will be included. - * - * @return array - * A form element to choose a parent, or an empty array if no possible - * parents exist for the given parameters. The resulting form value will be - * a single string containing the chosen menu name and parent ID separated - * by a ':' character. - */ - public function parentSelectElement($menu_parent, $id = '', array $menus = NULL); - - /** - * Gets a list of menu names for use as options. - * - * @param array $menu_names - * Optional array of menu names to limit the options, or NULL to load all. - * - * @return array - * Keys are menu names (ids) values are the menu labels. - */ - public function getMenuOptions(array $menu_names = NULL); - - /** - * Returns the maximum depth of the possible parents of the menu link. - * - * @param string $id - * The menu link plugin ID or an empty value for a new link. - * - * @return int - * The depth related to the depth of the given menu link. - */ - public function getParentDepthLimit($id); - - /** * For test purposes, clear any static data caches. */ public function resetStaticCache(); diff --git a/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php b/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php new file mode 100644 index 0000000..46bf257 --- /dev/null +++ b/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php @@ -0,0 +1,151 @@ +menuLinkTree = $menu_link_tree; + $this->entityManager = $entity_manager; + } + + /** + * {@inheritdoc} + */ + public function getParentSelectOptions($id = '', array $menus = NULL) { + if (!isset($menus)) { + $menus = $this->getMenuOptions(); + } + + $options = array(); + $depth_limit = $this->getParentDepthLimit($id); + foreach ($menus as $menu_name => $menu_title) { + $options[$menu_name . ':'] = '<' . $menu_title . '>'; + + $tree = $this->menuLinkTree->buildAllData($menu_name, NULL, $depth_limit); + $this->parentSelectOptionsTreeWalk($tree, $menu_name, '--', $options, $id, $depth_limit); + } + return $options; + } + + /** + * {@inheritdoc} + */ + 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) { + if (!isset($options[$menu_parent])) { + // Try putting it at the top level in the current menu. + list($menu_name, $parent) = explode(':', $menu_parent, 2); + $menu_parent = $menu_name . ':'; + } + if (isset($options[$menu_parent])) { + return array( + '#type' => 'select', + '#options' => $options, + '#default_value' => $menu_parent, + ); + } + } + return array(); + } + + /** + * Returns the maximum depth of the possible parents of the menu link. + * + * @param string $id + * The menu link plugin ID or an empty value for a new link. + * + * @return int + * The depth related to the depth of the given menu link. + */ + protected function getParentDepthLimit($id) { + if ($id) { + $limit = $this->menuLinkTree->maxDepth() - $this->menuLinkTree->getSubtreeHeight($id); + } + else { + $limit = $this->menuLinkTree->maxDepth() - 1; + } + return $limit; + } + + /** + * Iterates over all items in the tree to prepare the parents select options. + * + * @param array $tree + * The menu tree. + * @param string $menu_name + * The menu name. + * @param string $indent + * The indentation string used for the label. + * @param array $options + * The select options. + * @param string $exclude + * An excluded menu link. + * @param int $depth_limit + * The maximum depth of menu links considered for the select options. + */ + protected function parentSelectOptionsTreeWalk(array $tree, $menu_name, $indent, array &$options, $exclude, $depth_limit) { + foreach ($tree as $data) { + if ($data['depth'] > $depth_limit) { + // Don't iterate through any links on this level. + break; + } + /** @var \Drupal\Core\Menu\MenuLinkInterface $link */ + $link = $data['link']; + if ($link->getPluginId() != $exclude) { + $title = $indent . ' ' . Unicode::truncate($link->getTitle(), 30, TRUE, FALSE); + if ($link->isHidden()) { + $title .= ' (' . t('disabled') . ')'; + } + $options[$menu_name . ':' . $link->getPluginId()] = $title; + if ($data['below']) { + $this->parentSelectOptionsTreeWalk($data['below'], $menu_name, $indent . '--', $options, $exclude, $depth_limit); + } + } + } + } + + /** + * Gets a list of menu names for use as options. + * + * @param array $menu_names + * Optional array of menu names to limit the options, or NULL to load all. + * + * @return array + * Keys are menu names (ids) values are the menu labels. + */ + protected function getMenuOptions(array $menu_names = NULL) { + $menus = $this->entityManager->getStorage('menu')->loadMultiple($menu_names); + $options = array(); + foreach ($menus as $menu) { + $options[$menu->id()] = $menu->label(); + } + return $options; + } + +} \ No newline at end of file diff --git a/core/lib/Drupal/Core/Menu/MenuParentFormSelectorInterface.php b/core/lib/Drupal/Core/Menu/MenuParentFormSelectorInterface.php new file mode 100644 index 0000000..d88bcba --- /dev/null +++ b/core/lib/Drupal/Core/Menu/MenuParentFormSelectorInterface.php @@ -0,0 +1,56 @@ +menuTree = $menu_tree; + $this->menuParentSelector = $menu_parent_selector; $this->pathAliasManager = $alias_manager; $this->moduleHandler = $module_handler; $this->requestContext = $request_context; @@ -91,7 +91,7 @@ public function __construct(EntityManagerInterface $entity_manager, MenuLinkTree public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), - $container->get('menu.link_tree'), + $container->get('menu.parent_form_selector'), $container->get('path.alias_manager'), $container->get('module_handler'), $container->get('router.request_context'), @@ -161,7 +161,7 @@ public function submitEditForm(array &$form, array &$form_state) { form_state_values_clean($form_state); $this->entity = $this->buildEntity($form, $form_state); $this->entity->save(); - return $this->menuTree->createInstance($this->entity->getPluginId()); + return $form_state; } /** @@ -304,9 +304,9 @@ public function form(array $form, array &$form_state) { '#required' => TRUE, ); $default = $this->entity->getMenuName() . ':' . $this->entity->getParentId(); - $form['menu_parent'] = $this->menuTree->parentSelectElement($default, $this->entity->getPluginId()); + $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $this->entity->getPluginId()); $form['menu_parent']['#title'] = $this->t('Parent link'); - $form['menu_parent']['#description'] = $this->t('The maximum depth for a link and all its children is fixed at !maxdepth. Some menu links may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => $this->menuTree->maxDepth())); + $form['menu_parent']['#description'] = $this->t('The maximum depth for a link and all its children is fixed. Some menu links may not be available as parents if selecting them would exceed this limit.'); $form['menu_parent']['#attributes']['class'][] = 'menu-title-select'; return $form; diff --git a/core/modules/menu_ui/menu_ui.module b/core/modules/menu_ui/menu_ui.module index 284cca0..b777559 100644 --- a/core/modules/menu_ui/menu_ui.module +++ b/core/modules/menu_ui/menu_ui.module @@ -356,8 +356,8 @@ function menu_ui_form_node_form_alter(&$form, $form_state) { $node = $form_state['controller']->getEntity(); $definition = $form_state['menu_link']; $type = $node->getType(); - /** @var \Drupal\Core\Menu\MenuLinkTreeInterface $link_tree */ - $link_tree = \Drupal::menuTree(); + /** @var \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector */ + $menu_parent_selector = \Drupal::service('menu.parent_form_selector'); $menu_names = menu_ui_get_menus(); $type_menus = \Drupal::config("menu.entity.node.$type")->get('available_menus'); $available_menus = array(); @@ -370,7 +370,7 @@ function menu_ui_form_node_form_alter(&$form, $form_state) { else { $default = \Drupal::config('menu.entity.node.'.$type)->get('parent'); } - $parent_element = $link_tree->parentSelectElement($default, $definition['id'], $available_menus); + $parent_element = $menu_parent_selector->parentSelectElement($default, $definition['id'], $available_menus); // If no possible parent menu items were found, there is nothing to display. if (empty($parent_element)) { return; @@ -473,8 +473,8 @@ function menu_ui_node_submit(EntityInterface $node, $form, $form_state) { * @see menu_ui_form_node_type_form_submit(). */ function menu_ui_form_node_type_form_alter(&$form, $form_state) { - /** @var \Drupal\Core\Menu\MenuLinkTreeInterface $link_tree */ - $link_tree = \Drupal::menuTree(); + /** @var \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector */ + $menu_parent_selector = \Drupal::service('menu.parent_form_selector'); $menu_options = menu_ui_get_menus(); $type = $form_state['controller']->getEntity(); if ($type->id()) { @@ -506,7 +506,7 @@ function menu_ui_form_node_type_form_alter(&$form, $form_state) { // all available menu items. // Otherwise it is not possible to dynamically add options to the list. // @todo Convert getParentSelectOptions() into a #process callback. - $options = $link_tree->getParentSelectOptions(''); + $options = $menu_parent_selector->getParentSelectOptions(''); $form['menu']['menu_parent'] = array( '#type' => 'select', '#title' => t('Default parent item'), diff --git a/core/modules/menu_ui/src/Controller/MenuController.php b/core/modules/menu_ui/src/Controller/MenuController.php index 02c59d0..f8b1580 100644 --- a/core/modules/menu_ui/src/Controller/MenuController.php +++ b/core/modules/menu_ui/src/Controller/MenuController.php @@ -34,9 +34,9 @@ public function getParentOptions(Request $request) { $available_menus[$menu] = $menu; } } - /** @var \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree */ - $menu_tree = \Drupal::service('menu.link_tree'); - $options = $menu_tree->getParentSelectOptions('', $available_menus); + /** @var \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector */ + $menu_parent_selector = \Drupal::service('menu.parent_form_selector'); + $options = $menu_parent_selector->getParentSelectOptions('', $available_menus); return new JsonResponse($options); } diff --git a/core/modules/system/src/Tests/Menu/MenuLinkTreeTest.php b/core/modules/system/src/Tests/Menu/MenuLinkTreeTest.php index 3cc692d..caabb22 100644 --- a/core/modules/system/src/Tests/Menu/MenuLinkTreeTest.php +++ b/core/modules/system/src/Tests/Menu/MenuLinkTreeTest.php @@ -24,7 +24,7 @@ class MenuLinkTreeTest extends KernelTestBase { protected $linkTree; /** - * The menu link plugin maanger + * The menu link plugin mananger * * @var \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager */ @@ -88,35 +88,4 @@ public function testDeleteLinksInMenu() { $this->assertEqual(count($output), 1); } - /** - * Tests finding the parent depth limit. - */ - public function testGetParentDepthLimit() { - \Drupal::service('router.builder')->rebuild(); - - $storage = \Drupal::entityManager()->getStorage('menu_link_content'); - - // root - // - child1 - // -- child2 - // --- child3 - // ---- child4 - $root = $storage->create(array('route_name' => 'menu_test.menu_name_test', 'menu_name' => 'menu1', 'bundle' => 'menu_link_content')); - $root->save(); - $child1 = $storage->create(array('route_name' => 'menu_test.menu_name_test', 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'parent' => $root->getPluginId())); - $child1->save(); - $child2 = $storage->create(array('route_name' => 'menu_test.menu_name_test', 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'parent' => $child1->getPluginId())); - $child2->save(); - $child3 = $storage->create(array('route_name' => 'menu_test.menu_name_test', 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'parent' => $child2->getPluginId())); - $child3->save(); - $child4 = $storage->create(array('route_name' => 'menu_test.menu_name_test', 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'parent' => $child3->getPluginId())); - $child4->save(); - - $this->assertEqual($this->linkTree->getParentDepthLimit($root->getPluginId()), 4); - $this->assertEqual($this->linkTree->getParentDepthLimit($child1->getPluginId()), 5); - $this->assertEqual($this->linkTree->getParentDepthLimit($child2->getPluginId()), 6); - $this->assertEqual($this->linkTree->getParentDepthLimit($child3->getPluginId()), 7); - $this->assertEqual($this->linkTree->getParentDepthLimit($child4->getPluginId()), 8); - } - } diff --git a/core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php b/core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php index 04572cf..d31707e 100644 --- a/core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php +++ b/core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php @@ -37,7 +37,7 @@ class MenuTreeStorageTest extends KernelTestBase { * * @var array */ - public static $modules = array('system'); + public static $modules = array('system', 'menu_link_content'); /** * {@inheritdoc} @@ -58,6 +58,7 @@ protected function setUp() { $this->treeStorage = new MenuTreeStorage($this->container->get('database'), $this->container->get('cache.menu'), 'menu_tree'); $this->connection = $this->container->get('database'); + $this->installEntitySchema('menu_link_content'); } /** @@ -283,6 +284,36 @@ public function testLoadTree() { } /** + * Tests finding the subtree height with content menu links. + */ + public function testSubtreeHeight() { + + $storage = \Drupal::entityManager()->getStorage('menu_link_content'); + + // root + // - child1 + // -- child2 + // --- child3 + // ---- child4 + $root = $storage->create(array('route_name' => 'menu_test.menu_name_test', 'menu_name' => 'menu1', 'bundle' => 'menu_link_content')); + $root->save(); + $child1 = $storage->create(array('route_name' => 'menu_test.menu_name_test', 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'parent' => $root->getPluginId())); + $child1->save(); + $child2 = $storage->create(array('route_name' => 'menu_test.menu_name_test', 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'parent' => $child1->getPluginId())); + $child2->save(); + $child3 = $storage->create(array('route_name' => 'menu_test.menu_name_test', 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'parent' => $child2->getPluginId())); + $child3->save(); + $child4 = $storage->create(array('route_name' => 'menu_test.menu_name_test', 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'parent' => $child3->getPluginId())); + $child4->save(); + + $this->assertEqual($this->treeStorage->getSubtreeHeight($root->getPluginId()), 5); + $this->assertEqual($this->treeStorage->getSubtreeHeight($child1->getPluginId()), 4); + $this->assertEqual($this->treeStorage->getSubtreeHeight($child2->getPluginId()), 3); + $this->assertEqual($this->treeStorage->getSubtreeHeight($child3->getPluginId()), 2); + $this->assertEqual($this->treeStorage->getSubtreeHeight($child4->getPluginId()), 1); + } + + /** * Adds a link with the given ID and supply defaults. */ protected function addMenuLink($id, $parent = '', $route_name = 'test', $route_parameters = array(), $menu_name = 'tools', $extra = array()) {