diff --git a/core/modules/rest/src/Plugin/views/display/RestExport.php b/core/modules/rest/src/Plugin/views/display/RestExport.php index fd36a59eca..b251a440d1 100644 --- a/core/modules/rest/src/Plugin/views/display/RestExport.php +++ b/core/modules/rest/src/Plugin/views/display/RestExport.php @@ -70,7 +70,7 @@ class RestExport extends PathPluginBase implements ResponseDisplayPluginInterfac * * @var string */ - protected $mimeType; + protected $mimeType = 'application/json'; /** * The renderer. @@ -140,8 +140,11 @@ public function initDisplay(ViewExecutable $view, array &$display, array &$optio parent::initDisplay($view, $display, $options); $request_content_type = $this->view->getRequest()->getRequestFormat(); - $this->setContentType($request_content_type); - $this->setMimeType($this->view->getRequest()->getMimeType($request_content_type)); + + if ($request_content_type !== 'html') { + $this->setContentType($request_content_type); + $this->setMimeType($this->view->getRequest()->getMimeType($request_content_type)); + } } /** diff --git a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php index 4c004fcafc..a0a8dadb06 100644 --- a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php +++ b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php @@ -89,6 +89,9 @@ public function testRestViewsAuthentication() { $url = $this->buildUrl('test/serialize/auth_with_perm'); $response = \Drupal::httpClient()->get($url, [ 'auth' => [$this->adminUser->getUsername(), $this->adminUser->pass_raw], + 'query' => [ + '_format' => 'json', + ], ]); // Ensure that any changes to variables in the other thread are picked up. @@ -364,14 +367,14 @@ public function testResponseFormatConfiguration() { // Should return a 200. // @todo This should be fixed when we have better content negotiation. - $this->drupalGet('test/serialize/field'); + $this->drupalGetWithFormat('test/serialize/field', 'json'); $this->assertHeader('content-type', 'application/json'); $this->assertResponse(200, 'A 200 response was returned when any format was requested.'); // Should return a 200. Emulates a sample Firefox header. $this->drupalGet('test/serialize/field', array(), array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')); $this->assertHeader('content-type', 'application/json'); - $this->assertResponse(200, 'A 200 response was returned when a browser accept header was requested.'); + $this->assertResponse(406, 'A 200 response was returned when a browser accept header was requested.'); // Should return a 200. $this->drupalGetWithFormat('test/serialize/field', 'json'); @@ -392,7 +395,7 @@ public function testResponseFormatConfiguration() { $this->assertHeader('content-type', 'application/json'); $this->assertResponse(200, 'A 200 response was returned when HTML was requested.'); - // Now configure now format, so all of them should be allowed. + // Now configure no format, so all of them should be allowed. $this->drupalPostForm($style_options, array('style_options[formats][json]' => '0', 'style_options[formats][xml]' => '0'), t('Apply')); // Should return a 200. @@ -606,7 +609,7 @@ public function testSerializerViewsUI() { $this->assertResponse(200); // Check if we receive the expected result. $result = $this->xpath('//div[@id="views-live-preview"]/pre'); - $this->assertIdentical($this->drupalGet('test/serialize/field'), (string) $result[0], 'The expected JSON preview output was found.'); + $this->assertIdentical($this->drupalGetJSON('test/serialize/field'), (string) $result[0], 'The expected JSON preview output was found.'); } /** diff --git a/core/modules/views_ui/src/ViewListBuilder.php b/core/modules/views_ui/src/ViewListBuilder.php index b38a15f75b..cc0bb53264 100644 --- a/core/modules/views_ui/src/ViewListBuilder.php +++ b/core/modules/views_ui/src/ViewListBuilder.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException; /** * Defines a class to build a listing of view entities. @@ -255,9 +256,17 @@ protected function getDisplaysList(EntityInterface $view) { if ($display->hasPath()) { $path = $display->getPath(); if ($view->status() && strpos($path, '%') === FALSE) { - // @todo Views should expect and store a leading /. See: - // https://www.drupal.org/node/2423913 - $rendered_path = \Drupal::l('/' . $path, Url::fromUserInput('/' . $path)); + // Wrap this in a try/catch as tryng to generate links to some + // routes may throw a NotAcceptableHttpException if they do not + // respond to HTML, such as RESTExports. + try { + // @todo Views should expect and store a leading /. See: + // https://www.drupal.org/node/2423913 + $rendered_path = \Drupal::l('/' . $path, Url::fromUserInput('/' . $path)); + } + catch (NotAcceptableHttpException $e) { + $rendered_path = '/' . $path; + } } else { $rendered_path = '/' . $path;