diff --git a/core/modules/rest/lib/Drupal/rest/Plugin/views/display/RestExport.php b/core/modules/rest/lib/Drupal/rest/Plugin/views/display/RestExport.php index 5ce4a52..6fd2170 100644 --- a/core/modules/rest/lib/Drupal/rest/Plugin/views/display/RestExport.php +++ b/core/modules/rest/lib/Drupal/rest/Plugin/views/display/RestExport.php @@ -59,7 +59,7 @@ class RestExport extends PathPluginBase { * * @var string */ - protected $contentType; + protected $contentType = 'json'; /** * The mime type for the response. @@ -78,7 +78,17 @@ public function initDisplay(ViewExecutable $view, array &$display, array &$optio $negotiation = $container->get('content_negotiation'); $request = $container->get('request'); - $this->setContentType($negotiation->getContentType($request)); + $request_content_type = $negotiation->getContentType($request); + // Only set the requested content type if it's not html. + if ($request_content_type === 'html') { + if (\Drupal::moduleHandler()->moduleExists('hal')) { + $this->setContentType('hal_json'); + } + } + else { + $this->setContentType($request_content_type); + } + $this->setMimeType($request->getMimeType($this->contentType)); } diff --git a/core/modules/rest/lib/Drupal/rest/Tests/Views/StyleSerializerTest.php b/core/modules/rest/lib/Drupal/rest/Tests/Views/StyleSerializerTest.php index 5349955..4824307 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/Views/StyleSerializerTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/Views/StyleSerializerTest.php @@ -126,6 +126,16 @@ public function testSerializerResponses() { $expected = $serializer->serialize($entities, 'hal_json'); $actual_json = $this->drupalGet('test/serialize/entity', array(), array('Accept: application/hal+json')); $this->assertIdentical($actual_json, $expected, 'The expected HAL output was found.'); + + // Test that by default the output will be JSON if hal is enabled. + $expected = $serializer->serialize($entities, 'hal_json'); + $actual = $this->drupalGet('test/serialize/entity'); + $this->assertIdentical($actual, $expected, 'If hal module is enabled by default hal_json output is returned.'); + + module_disable(array('hal')); + $expected = $serializer->serialize($entities, 'json'); + $actual = $this->drupalGet('test/serialize/entity'); + $this->assertIdentical($actual, $expected, 'By default json output is returned.'); } /**