Commit 4e0d7b51 authored by alexpott's avatar alexpott

Issue #2056895 by tim.plunkett, xjm: Fixed Routes with optional placeholders...

Issue #2056895 by tim.plunkett, xjm: Fixed Routes with optional placeholders can fatal when placed in a menu.
parent 9561d880
......@@ -2803,11 +2803,11 @@ function menu_router_build($save = FALSE) {
* The translated path pattern from the route.
*/
function _menu_router_translate_route($route_name) {
$route = Drupal::service('router.route_provider')->getRouteByName($route_name);
$path = trim($route->getPattern(), '/');
// Translate placeholders, e.g. {foo} -> %.
return preg_replace('/{' . DRUPAL_PHP_FUNCTION_PATTERN . '}/', '%', $path);
$outline = Drupal::service('router.route_provider')
->getRouteByName($route_name)
->compile()
->getPatternOutline();
return trim($outline, '/');
}
/**
......
......@@ -614,4 +614,18 @@ function testMenuLoadArgumentsInheritance() {
$this->assertIdentical(unserialize($router_item['load_functions']), $load_functions, format_string('Expected load functions for router %router_path' , array('%router_path' => $router_path)));
}
}
/**
* Test menu links that have optional placeholders.
*/
public function testMenuOptionalPlaceholders() {
$this->drupalGet('menu-test/optional');
$this->assertResponse(200);
$this->assertText('Sometimes there is no placeholder.');
$this->drupalGet('menu-test/optional/foobar');
$this->assertResponse(200);
$this->assertText("Sometimes there is a placeholder: 'foobar'.");
}
}
......@@ -7,6 +7,7 @@
namespace Drupal\menu_test;
use Drupal\Component\Utility\String;
use Drupal\Core\Entity\EntityInterface;
/**
......@@ -35,4 +36,22 @@ public function test2() {
return 'test2';
}
/**
* Prints out test data.
*
* @param string|null $placeholder
* A placeholder for the return string.
*
* @return string
* The string for this route.
*/
public function testDefaults($placeholder = NULL) {
if ($placeholder) {
return String::format("Sometimes there is a placeholder: '@placeholder'.", array('@placeholder' => $placeholder));
}
else {
return String::format('Sometimes there is no placeholder.');
}
}
}
......@@ -438,6 +438,11 @@ function menu_test_menu() {
'title' => 'Local tasks',
'route_name' => 'menu_local_task_test_tasks',
);
$items['menu-test/optional'] = array(
'title' => 'Test optional placeholder',
'route_name' => 'menu_test_optional_placeholder',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
......
......@@ -99,3 +99,11 @@ menu_local_task_test_tasks_settings_sub2:
_content: '\Drupal\menu_test\TestControllers::test1'
requirements:
_access: 'TRUE'
menu_test_optional_placeholder:
pattern: '/menu-test/optional/{placeholder}'
defaults:
_content: '\Drupal\menu_test\TestControllers::testDefaults'
placeholder: NULL
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