diff --git a/core/modules/rest/src/Plugin/views/display/RestExport.php b/core/modules/rest/src/Plugin/views/display/RestExport.php index 9188415..247b60c 100644 --- a/core/modules/rest/src/Plugin/views/display/RestExport.php +++ b/core/modules/rest/src/Plugin/views/display/RestExport.php @@ -235,13 +235,9 @@ public function collectRoutes(RouteCollection $collection) { public static function buildResponse($view_id, $display_id, array $args = []) { $build = static::buildBasicRenderable($view_id, $display_id, $args); - - /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = \Drupal::service('renderer'); - - $output = $renderer->renderRoot($build); $response = new CacheableResponse($output, 200); @@ -269,19 +265,16 @@ public function render() { $build = array(); $build['#markup'] = $this->view->style_plugin->render(); + $this->view->element['#content_type'] = $this->getMimeType(); + $this->view->element['#cache_properties'][] = '#content_type'; + // Wrap the output in a pre tag if this is for a live preview. if (!empty($this->view->live_preview)) { $build['#prefix'] = '
';
       $build['#markup'] = SafeMarkup::checkPlain($build['#markup']);
       $build['#suffix'] = '
'; } - - parent::applyDisplayCachablityMetadata($build); - - $this->view->element['#content_type'] = $this->getMimeType(); - $this->view->element['#cache_properties'][] = '#content_type'; - - if ($this->view->getRequest()->getFormat($build['#cache-content-type']) !== 'html') { + elseif ($this->view->getRequest()->getFormat($this->view->element['#content_type']) !== 'html') { // This display plugin is primarily for returning non-HTML formats. // However, we still invoke the renderer to collect cacheability metadata. // Because the renderer is designed for HTML rendering, it filters @@ -295,6 +288,8 @@ public function render() { $build['#markup'] = SafeMarkup::set($build['#markup']); } + parent::applyDisplayCachablityMetadata($build); + return $build; } diff --git a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php index 23bd8ed..1eca0d2 100644 --- a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php +++ b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php @@ -253,7 +253,7 @@ public function testRestRenderCaching() { $this->assertCacheTags($cache_tags); $this->assertTrue($render_cache->get($original)); - $result_xml = $this->drupalGet('test/serialize/entity', [], ['Accept: application/xml']); + $result_xml = $this->drupalGetWithFormat('test/serialize/entity', 'xml'); $this->addRequestWithFormat('xml'); $this->assertHeader('content-type', 'text/xml; charset=UTF-8'); $this->assertCacheContexts($cache_contexts); @@ -339,7 +339,9 @@ public function testResponseFormatConfiguration() { $this->assertTrue(strpos($headers['content-type'], 'text/xml') !== FALSE, 'The header Content-type is correct.'); // Should return a 406. $this->drupalGetWithFormat('test/serialize/field', 'html'); - $this->assertHeader('content-type', 'text/html; charset=UTF-8'); + // We want to show the first format by default, see + // \Drupal\rest\Plugin\views\style\Serializer::render. + $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. @@ -355,8 +357,10 @@ public function testResponseFormatConfiguration() { $this->assertResponse(200, 'A 200 response was returned when XML was requested'); // Should return a 200. $this->drupalGetWithFormat('test/serialize/field', 'html'); - $this->assertHeader('content-type', 'text/html; charset=UTF-8'); - $this->assertResponse(406, 'A 406 response was returned when HTML was requested.'); + // We want to show the first format by default, see + // \Drupal\rest\Plugin\views\style\Serializer::render. + $this->assertHeader('content-type', 'application/json'); + $this->assertResponse(200, 'A 200 response was returned when HTML was requested.'); } /**