diff --git a/core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php b/core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php index 8119efa07b..4456157254 100644 --- a/core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php +++ b/core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php @@ -26,9 +26,17 @@ public function filter(RouteCollection $collection, Request $request) { $format = $request->getRequestFormat('html'); /** @var \Symfony\Component\Routing\Route $route */ foreach ($collection as $name => $route) { + $supported_formats = array_filter(explode('|', $route->getRequirement('_format'))); + $default_format = $route->getDefault('_default_format'); + // If the route has no _format specification, we move it to the end. If it // does, then no match means the route is removed entirely. - if ($supported_formats = array_filter(explode('|', $route->getRequirement('_format')))) { + if ($supported_formats) { + // If the route has a matching default format, use it + if ($default_format && in_array($default_format, $supported_formats)) { + $request->setRequestFormat($default_format); + $format = $default_format; + } if (!in_array($format, $supported_formats)) { $collection->remove($name); } diff --git a/core/modules/rest/src/Routing/ResourceRoutes.php b/core/modules/rest/src/Routing/ResourceRoutes.php index 8aedfba59e..df07625916 100644 --- a/core/modules/rest/src/Routing/ResourceRoutes.php +++ b/core/modules/rest/src/Routing/ResourceRoutes.php @@ -116,6 +116,14 @@ protected function getRoutesForResourceConfig(RestResourceConfigInterface $rest_ // authentication provider and add the route. $route->setOption('_auth', $rest_resource_config->getAuthenticationProviders($method)); $route->setDefault('_rest_resource_config', $rest_resource_config->id()); + + if (($config = $rest_resource_config->get('configuration')) && + isset($config['default_format']) + ) { + $default_format = $config['default_format']; + $route->setDefault('_default_format', $default_format); + } + $collection->add("rest.$name", $route); } diff --git a/core/tests/Drupal/Tests/Core/Routing/RequestFormatRouteFilterTest.php b/core/tests/Drupal/Tests/Core/Routing/RequestFormatRouteFilterTest.php index 39a960b545..50114cfe2e 100644 --- a/core/tests/Drupal/Tests/Core/Routing/RequestFormatRouteFilterTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/RequestFormatRouteFilterTest.php @@ -61,6 +61,27 @@ public function testFilter() { /** * @covers ::filter + */ + public function testDefaultRoute() { + $route_filter = new RequestFormatRouteFilter(); + + $route_with_default_format = $route = new Route('/test'); + $route_with_default_format->setRequirement('_format', 'json'); + $route_with_default_format->setDefault('_default_format', 'json'); + + $collection = new RouteCollection(); + $collection->add('test_r_with_default', $route_with_default_format); + + $request = new Request(); + $request->setRequestFormat('html'); + $collection = $route_filter->filter($collection, $request); + + $this->assertCount(1, $collection); + $this->assertEquals(array_keys($collection->all())[0], 'test_r_with_default'); + } + + /** + * @covers ::filter * @expectedException \Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException * @expectedExceptionMessage No route found for the specified format xml. */