From 688f773d3323da16feae73a6864ee6592ac83564 Mon Sep 17 00:00:00 2001
From: webchick <drupal@webchick.net>
Date: Sun, 28 Sep 2014 14:16:51 -0700
Subject: [PATCH] Issue #2340471 by clemens.tolboom, dawehner: Fixed Rest
 export views preview should show some output instead of 500.

---
 .../rest/src/Plugin/views/style/Serializer.php     |  7 ++++++-
 .../rest/src/Tests/Views/StyleSerializerTest.php   | 14 +++++++++++---
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/core/modules/rest/src/Plugin/views/style/Serializer.php b/core/modules/rest/src/Plugin/views/style/Serializer.php
index 51366bdb9677..09e94e646e6f 100644
--- a/core/modules/rest/src/Plugin/views/style/Serializer.php
+++ b/core/modules/rest/src/Plugin/views/style/Serializer.php
@@ -125,7 +125,12 @@ public function render() {
       $rows[] = $this->view->rowPlugin->render($row);
     }
 
-    return $this->serializer->serialize($rows, $this->displayHandler->getContentType());
+    $content_type = $this->displayHandler->getContentType();
+    if (!empty($this->view->live_preview)) {
+      $content_type = $this->options['formats'] ? reset($this->options['formats']) : 'json';
+    }
+
+    return $this->serializer->serialize($rows, $content_type);
   }
 
   /**
diff --git a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php
index ea19d5e50440..0822eabad0d7 100644
--- a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php
+++ b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php
@@ -11,6 +11,7 @@
 use Drupal\views\Views;
 use Drupal\views\Tests\Plugin\PluginTestBase;
 use Drupal\views\Tests\ViewTestData;
+use Symfony\Component\HttpFoundation\Request;
 
 /**
  * Tests the serializer style plugin.
@@ -259,9 +260,17 @@ public function testFieldRawOutput() {
   }
 
   /**
-   * Tests the preview output for json output.
+   * Tests the live preview output for json output.
    */
-  public function testPreview() {
+  public function testLivePreview() {
+    // We set up a request so it looks like an request in the live preview.
+    $request = new Request();
+    $request->setFormat('drupal_ajax', 'application/vnd.drupal-ajax');
+    $request->headers->set('Accept', 'application/vnd.drupal-ajax');
+      /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
+    $request_stack = \Drupal::service('request_stack');
+    $request_stack->push($request);
+
     $view = Views::getView('test_serializer_display_entity');
     $view->setDisplay('rest_export_1');
     $this->executeView($view);
@@ -276,7 +285,6 @@ public function testPreview() {
 
     $expected = String::checkPlain($serializer->serialize($entities, 'json'));
 
-    $view->display_handler->setContentType('json');
     $view->live_preview = TRUE;
 
     $build = $view->preview();
-- 
GitLab