Commit 50197bc8 authored by alexpott's avatar alexpott

Issue #2258299 by juampy: Dblog_menu_link_defaults_alter() must set...

Issue #2258299 by juampy: Dblog_menu_link_defaults_alter() must set 'machine_name' to avoid duplicates.
parent 82ecfadf
......@@ -49,8 +49,12 @@ public function getLinks() {
}
$this->moduleHandler->alter('menu_link_defaults', $all_links);
// Change the key to match the DB column for now.
foreach ($all_links as $machine_name => $menu_link) {
// Set the machine_name to the menu links added dynamically.
if (!isset($menu_link['machine_name'])) {
$all_links[$machine_name]['machine_name'] = $machine_name;
}
// Change the key to match the DB column for now.
$all_links[$machine_name]['link_title'] = $all_links[$machine_name]['title'];
unset($all_links[$machine_name]['title']);
}
......
......@@ -58,6 +58,7 @@ public function testMenuIntegration() {
$this->doTestMenuOptionalPlaceholders();
$this->doTestMenuOnRoute();
$this->doTestMenuName();
$this->doTestMenuLinkDefaultsAlter();
$this->doTestMenuItemTitlesCases();
$this->doTestMenuLinkMaintain();
$this->doTestMenuLinkOptions();
......@@ -178,6 +179,22 @@ protected function doTestMenuName() {
$this->assertEqual($menu_link->menu_name, 'changed', 'Menu name was successfully changed after rebuild.');
}
/**
* Tests menu links added in hook_menu_link_defaults_alter().
*/
protected function doTestMenuLinkDefaultsAlter() {
// Check that machine name does not need to be defined since it is already
// set as the key of each menu link.
$menu_links = entity_load_multiple_by_properties('menu_link', array('route_name' => 'menu_test.custom'));
$menu_link = reset($menu_links);
$this->assertEqual($menu_link->machine_name, 'menu_test.custom', 'Menu links added at hook_menu_link_defaults_alter() obtain the machine name from the $links key.');
// Make sure that rebuilding the menu tree does not produce duplicates of
// links added by hook_menu_link_defaults_alter().
\Drupal::service('router.builder')->rebuild();
$this->drupalGet('menu-test');
$this->assertUniqueText('Custom link', 'Menu links added by hook_menu_link_defaults_alter() do not duplicate after a menu rebuild.');
}
/**
* Tests for menu hierarchy.
*/
......
......@@ -9,14 +9,21 @@
/**
* Implements hook_menu_link_defaults_alter().
*
* Many of the machine names here are slightly different from the route name.
* Since the machine name is arbitrary, this helps ensure that core does not
* add mistaken assumptions about the correlation.
*/
function menu_test_menu_link_defaults_alter(&$links) {
// Many of the machine names here are slightly different from the route name.
// Since the machine name is arbitrary, this helps ensure that core does not
// add mistaken assumptions about the correlation.
$links['menu_test.menu_name_test']['menu_name'] = menu_test_menu_name();
$links['menu_test.context']['title'] = \Drupal::config('menu_test.menu_item')->get('title');
// Adds a custom menu link.
$links['menu_test.custom'] = array(
'title' => 'Custom link',
'route_name' => 'menu_test.custom',
'description' => 'Custom link used to check the integrity of manually added menu links.',
'parent' => 'menu_test',
);
}
/**
......
......@@ -358,3 +358,10 @@ menu_test.context:
_content: '\Drupal\menu_test\Controller\MenuTestController::menuTestCallback'
requirements:
_access: 'TRUE'
menu_test.custom:
path: '/menu-test/custom'
defaults:
_content: '\Drupal\menu_test\Controller\MenuTestController::menuTestCallback'
requirements:
_access: 'TRUE'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment