From 4527055218681bc07b360e2b33e1de031cf2be45 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Fri, 5 Apr 2013 15:25:18 -0700
Subject: [PATCH] Issue #1938380 by damiankloip, dawehner: Let
 ViewExecutable->preview() return a render array.

---
 .../history/Tests/Views/HistoryTimestampTest.php   |  2 +-
 .../rest/Plugin/views/display/RestExport.php       | 12 ++++++++----
 .../rest/Tests/Views/StyleSerializerTest.php       |  3 ++-
 core/modules/views/includes/ajax.inc               |  3 ++-
 .../lib/Drupal/views/Plugin/views/area/View.php    | 10 ++--------
 .../Plugin/views/display/DisplayPluginBase.php     |  3 +--
 .../Drupal/views/Tests/Handler/AreaEntityTest.php  |  6 ++++--
 .../lib/Drupal/views/Tests/Handler/AreaTest.php    |  2 ++
 .../Drupal/views/Tests/Handler/FieldUnitTest.php   |  2 ++
 .../Drupal/views/Tests/Handler/FieldWebTest.php    |  8 ++++++++
 .../lib/Drupal/views/Tests/Plugin/CacheTest.php    | 14 ++++++++------
 .../lib/Drupal/views/Tests/Plugin/DisplayTest.php  |  2 ++
 .../Drupal/views/Tests/Plugin/StyleMappingTest.php |  3 ++-
 .../lib/Drupal/views/Tests/Plugin/StyleTest.php    |  6 ++++--
 .../views/Tests/Plugin/StyleUnformattedTest.php    |  4 ++--
 .../Drupal/views/Tests/UI/CustomBooleanTest.php    |  1 +
 .../lib/Drupal/views/Tests/ViewRenderTest.php      |  3 ++-
 .../Plugin/views/display/DisplayTest.php           |  3 +--
 core/modules/views/views.module                    |  2 +-
 .../views/views_ui/lib/Drupal/views_ui/ViewUI.php  |  1 +
 20 files changed, 56 insertions(+), 34 deletions(-)

diff --git a/core/modules/history/lib/Drupal/history/Tests/Views/HistoryTimestampTest.php b/core/modules/history/lib/Drupal/history/Tests/Views/HistoryTimestampTest.php
index d92d02488fd8..182174c42e91 100644
--- a/core/modules/history/lib/Drupal/history/Tests/Views/HistoryTimestampTest.php
+++ b/core/modules/history/lib/Drupal/history/Tests/Views/HistoryTimestampTest.php
@@ -76,7 +76,7 @@ public function testHandlers() {
     $this->executeView($view);
     $this->assertEqual(count($view->result), 2);
     $output = $view->preview();
-    $this->drupalSetContent($output);
+    $this->drupalSetContent(drupal_render($output));
     $result = $this->xpath('//span[@class=:class]', array(':class' => 'marker'));
     $this->assertEqual(count($result), 1, 'Just one node is marked as new');
 
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 3496ee0192cf..e7e38719d94a 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
@@ -202,20 +202,24 @@ public function optionsSummary(&$categories, &$options) {
   public function execute() {
     parent::execute();
 
-    return new Response($this->view->render(), 200, array('Content-type' => $this->getMimeType()));
+    $output = $this->view->render();
+    return new Response(drupal_render($output), 200, array('Content-type' => $this->getMimeType()));
   }
 
   /**
    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::render().
    */
   public function render() {
-    $output = $this->view->style_plugin->render();
+    $build = array();
+    $build['#markup'] = $this->view->style_plugin->render();
 
+    // Wrap the output in a pre tag if this is for a live preview.
     if (!empty($this->view->live_preview)) {
-      return '<pre>' . $output . '</pre>';
+      $build['#prefix'] = '<pre>';
+      $build['#suffix'] = '</pre>';
     }
 
-    return $output;
+    return $build;
   }
 
   /**
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 c38db82e8eb1..9bdfbbea2c26 100644
--- a/core/modules/rest/lib/Drupal/rest/Tests/Views/StyleSerializerTest.php
+++ b/core/modules/rest/lib/Drupal/rest/Tests/Views/StyleSerializerTest.php
@@ -100,7 +100,8 @@ public function testSerializerResponses() {
     $view->setDisplay('rest_export_1');
     // Mock the request content type by setting it on the display handler.
     $view->display_handler->setContentType('json');
-    $this->assertIdentical($actual_json, $view->preview(), 'The expected JSON preview output was found.');
+    $output = $view->preview();
+    $this->assertIdentical($actual_json, drupal_render($output), 'The expected JSON preview output was found.');
 
     // Test a 403 callback.
     $this->drupalGet('test/serialize/denied');
diff --git a/core/modules/views/includes/ajax.inc b/core/modules/views/includes/ajax.inc
index 65ecd3f914cf..d938e34b7379 100644
--- a/core/modules/views/includes/ajax.inc
+++ b/core/modules/views/includes/ajax.inc
@@ -80,7 +80,8 @@ function views_ajax() {
       // Reuse the same DOM id so it matches that in Drupal.settings.
       $view->dom_id = $dom_id;
 
-      $response->addCommand(new ReplaceCommand(".view-dom-id-$dom_id", $view->preview($display_id, $args)));
+      $preview = $view->preview($display_id, $args);
+      $response->addCommand(new ReplaceCommand(".view-dom-id-$dom_id", drupal_render($preview)));
     }
     return $response;
   }
diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/View.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/View.php
index f4eba6abd9ee..7d5e994b847d 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/views/area/View.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/View.php
@@ -78,17 +78,11 @@ function render($empty = FALSE) {
         drupal_set_message(t("Recursion detected in view @view display @display.", array('@view' => $view_name, '@display' => $display_id)), 'error');
       }
       else {
-        // Current $view->preview() does not return a render array, so we have
-        // to build a markup out if it.
         if (!empty($this->options['inherit_arguments']) && !empty($this->view->args)) {
-          return array(
-            '#markup' => $view->preview($display_id, $this->view->args),
-          );
+          return $view->preview($display_id, $this->view->args);
         }
         else {
-          return array(
-            '#markup' => $view->preview($display_id),
-          );
+          return $view->preview($display_id);
         }
       }
     }
diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php
index 8238287bb7da..eb22ed29a468 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php
@@ -2549,8 +2549,7 @@ public function execute() { }
    * some other AJAXy reason.
    */
   function preview() {
-    $element = $this->view->render();
-    return drupal_render($element);
+    return $this->view->render();
   }
 
   /**
diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/AreaEntityTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaEntityTest.php
index d6656617b2f7..9cf013439351 100644
--- a/core/modules/views/lib/Drupal/views/Tests/Handler/AreaEntityTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaEntityTest.php
@@ -88,7 +88,8 @@ public function testEntityArea() {
     }
 
     $view = views_get_view('test_entity_area');
-    $this->drupalSetContent($view->preview('default', array($entities[1]->id())));
+    $preview = $view->preview('default', array($entities[1]->id()));
+    $this->drupalSetContent(drupal_render($preview));
 
     $result = $this->xpath('//div[@class = "view-header"]');
     $this->assertTrue(strpos(trim((string) $result[0]), $entities[0]->label()) !== FALSE, 'The rendered entity appears in the header of the view.');
@@ -104,7 +105,8 @@ public function testEntityArea() {
     $item['view_mode'] = 'test';
     $view->setItem('default', 'header', 'entity_entity_test_render', $item);
 
-    $this->drupalSetContent($view->preview('default', array($entities[1]->id())));
+    $preview = $view->preview('default', array($entities[1]->id()));
+    $this->drupalSetContent(drupal_render($preview));
 
     $result = $this->xpath('//div[@class = "view-header"]');
     $this->assertTrue(strpos(trim((string) $result[0]), $entities[0]->label()) !== FALSE, 'The rendered entity appears in the header of the view.');
diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php
index 21804c577a9f..1ee08da333e1 100644
--- a/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php
@@ -111,6 +111,7 @@ public function testRenderArea() {
 
     // Check whether the strings exists in the output.
     $output = $view->preview();
+    $output = drupal_render($output);
     $this->assertTrue(strpos($output, $header_string) !== FALSE);
     $this->assertTrue(strpos($output, $footer_string) !== FALSE);
     $this->assertTrue(strpos($output, $empty_string) !== FALSE);
@@ -149,6 +150,7 @@ public function testRenderAreaToken() {
 
     // Test we have the site:name token in the output.
     $output = $view->preview();
+    $output = drupal_render($output);
     $expected = token_replace('[site:name]');
     $this->assertTrue(strpos($output, $expected) !== FALSE);
   }
diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php
index afff1b33d081..5dfa29b4a1cf 100644
--- a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php
@@ -218,6 +218,7 @@ public function testExclude() {
     $view->field['name']->options['exclude'] = TRUE;
 
     $output = $view->preview();
+    $output = drupal_render($output);
     foreach ($this->dataSet() as $entry) {
       $this->assertNotSubString($output, $entry['name']);
     }
@@ -226,6 +227,7 @@ public function testExclude() {
     $view->field['name']->options['exclude'] = FALSE;
 
     $output = $view->preview();
+    $output = drupal_render($output);
     foreach ($this->dataSet() as $entry) {
       $this->assertSubString($output, $entry['name']);
     }
diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php
index 9d8c66e11c0a..43acdabe08e0 100644
--- a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php
@@ -331,11 +331,13 @@ public function testFieldClasses() {
 
     $id_field->options['element_default_classes'] = FALSE;
     $output = $view->preview();
+    $output = drupal_render($output);
     $this->assertFalse($this->xpathContent($output, '//div[contains(@class, :class)]', array(':class' => 'field-content')));
     $this->assertFalse($this->xpathContent($output, '//div[contains(@class, :class)]', array(':class' => 'field-label')));
 
     $id_field->options['element_default_classes'] = TRUE;
     $output = $view->preview();
+    $output = drupal_render($output);
     // Per default the label and the element of the field are spans.
     $this->assertTrue($this->xpathContent($output, '//span[contains(@class, :class)]', array(':class' => 'field-content')));
     $this->assertTrue($this->xpathContent($output, '//span[contains(@class, :class)]', array(':class' => 'views-label')));
@@ -351,11 +353,13 @@ public function testFieldClasses() {
       // Set a custom wrapper element css class.
       $id_field->options['element_wrapper_class'] = $random_class;
       $output = $view->preview();
+      $output = drupal_render($output);
       $this->assertTrue($this->xpathContent($output, "//{$element_type}[contains(@class, :class)]", array(':class' => $random_class)));
 
       // Set no custom css class.
       $id_field->options['element_wrapper_class'] = '';
       $output = $view->preview();
+      $output = drupal_render($output);
       $this->assertFalse($this->xpathContent($output, "//{$element_type}[contains(@class, :class)]", array(':class' => $random_class)));
       $this->assertTrue($this->xpathContent($output, "//li[contains(@class, views-row)]/{$element_type}"));
     }
@@ -369,11 +373,13 @@ public function testFieldClasses() {
       // Set a custom label element css class.
       $id_field->options['element_label_class'] = $random_class;
       $output = $view->preview();
+      $output = drupal_render($output);
       $this->assertTrue($this->xpathContent($output, "//li[contains(@class, views-row)]//{$element_type}[contains(@class, :class)]", array(':class' => $random_class)));
 
       // Set no custom css class.
       $id_field->options['element_label_class'] = '';
       $output = $view->preview();
+      $output = drupal_render($output);
       $this->assertFalse($this->xpathContent($output, "//li[contains(@class, views-row)]//{$element_type}[contains(@class, :class)]", array(':class' => $random_class)));
       $this->assertTrue($this->xpathContent($output, "//li[contains(@class, views-row)]//{$element_type}"));
     }
@@ -387,11 +393,13 @@ public function testFieldClasses() {
       // Set a custom label element css class.
       $id_field->options['element_class'] = $random_class;
       $output = $view->preview();
+      $output = drupal_render($output);
       $this->assertTrue($this->xpathContent($output, "//li[contains(@class, views-row)]//div[contains(@class, views-field)]//{$element_type}[contains(@class, :class)]", array(':class' => $random_class)));
 
       // Set no custom css class.
       $id_field->options['element_class'] = '';
       $output = $view->preview();
+      $output = drupal_render($output);
       $this->assertFalse($this->xpathContent($output, "//li[contains(@class, views-row)]//div[contains(@class, views-field)]//{$element_type}[contains(@class, :class)]", array(':class' => $random_class)));
       $this->assertTrue($this->xpathContent($output, "//li[contains(@class, views-row)]//div[contains(@class, views-field)]//{$element_type}"));
     }
diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/CacheTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/CacheTest.php
index 6ac76d7d20f8..886d81529896 100644
--- a/core/modules/views/lib/Drupal/views/Tests/Plugin/CacheTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/CacheTest.php
@@ -139,14 +139,16 @@ function testHeaderStorage() {
       )
     ));
 
-    $view->preview();
+    $output = $view->preview();
+    drupal_render($output);
     unset($view->pre_render_called);
     drupal_static_reset('drupal_add_css');
     drupal_static_reset('drupal_add_js');
     $view->destroy();
 
     $view->setDisplay();
-    $view->preview();
+    $output = $view->preview();
+    drupal_render($output);
     $css = drupal_add_css();
     $css_path = drupal_get_path('module', 'views_test_data') . '/views_cache.test.css';
     $js_path = drupal_get_path('module', 'views_test_data') . '/views_cache.test.js';
@@ -166,14 +168,14 @@ function testHeaderStorage() {
     drupal_add_js($system_js_path);
     $view->destroy();
 
-    $view->setDisplay();
-    $view->preview();
+    $output = $view->preview();
+    drupal_render($output);
     drupal_static_reset('drupal_add_css');
     drupal_static_reset('drupal_add_js');
     $view->destroy();
 
-    $view->setDisplay();
-    $view->preview();
+    $output = $view->preview();
+    drupal_render($output);
 
     $css = drupal_add_css();
     $js = drupal_add_js();
diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayTest.php
index 6b023f78f0b2..4ccd11d6bbb4 100644
--- a/core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayTest.php
@@ -82,6 +82,7 @@ function testDisplayPlugin() {
     $this->assertIdentical($view->display_handler->getOption('test_option'), '');
 
     $output = $view->preview();
+    $output = drupal_render($output);
 
     $this->assertTrue(strpos($output, '<h1></h1>') !== FALSE, 'An empty value for test_option found in output.');
 
@@ -90,6 +91,7 @@ function testDisplayPlugin() {
 
     $view->save();
     $output = $view->preview();
+    $output = drupal_render($output);
 
     // Test we have our custom <h1> tag in the output of the view.
     $this->assertTrue(strpos($output, '<h1>Test option title</h1>') !== FALSE, 'The test_option value found in display output title.');
diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleMappingTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleMappingTest.php
index f745a08e2935..5cea609751d3 100644
--- a/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleMappingTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleMappingTest.php
@@ -54,7 +54,8 @@ public function testMappedOutput() {
    *   The view rendered as HTML.
    */
   protected function mappedOutputHelper($view) {
-    $rendered_output = $view->preview();
+    $output = $view->preview();
+    $rendered_output = drupal_render($output);
     $this->storeViewPreview($rendered_output);
     $rows = $this->elements->body->div->div->div;
     $data_set = $this->dataSet();
diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleTest.php
index 2e5f4d2a83da..f567068bb413 100644
--- a/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleTest.php
@@ -64,6 +64,7 @@ public function testStyle() {
     // rendered.
     $view->style_plugin->setOutput($random_text);
     $output = $view->preview();
+    $output = drupal_render($output);
     $this->assertTrue(strpos($output, $random_text) !== FALSE, 'Take sure that the rendering of the style plugin appears in the output of the view.');
 
     // This run use the test row plugin and render with it.
@@ -86,6 +87,7 @@ public function testStyle() {
     $view->rowPlugin->setOutput($random_text);
 
     $output = $view->preview();
+    $output = drupal_render($output);
     $this->assertTrue(strpos($output, $random_text) !== FALSE, 'Take sure that the rendering of the row plugin appears in the output of the view.');
   }
 
@@ -225,8 +227,8 @@ function testCustomRowClasses() {
     $random_name = $this->randomName();
     $view->style_plugin->options['row_class'] = $random_name . " test-token-[name]";
 
-    $rendered_output = $view->preview();
-    $this->storeViewPreview($rendered_output);
+    $output = $view->preview();
+    $this->storeViewPreview(drupal_render($output));
 
     $rows = $this->elements->body->div->div->div;
     $count = 0;
diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleUnformattedTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleUnformattedTest.php
index 5348fbc76ab5..72d6a93684c4 100644
--- a/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleUnformattedTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleUnformattedTest.php
@@ -33,8 +33,8 @@ public static function getInfo() {
   function testDefaultRowClasses() {
     $view = views_get_view('test_view');
     $view->setDisplay();
-    $rendered_output = $view->preview();
-    $this->storeViewPreview($rendered_output);
+    $output = $view->preview();
+    $this->storeViewPreview(drupal_render($output));
 
     $rows = $this->elements->body->div->div->div;
     $count = 0;
diff --git a/core/modules/views/lib/Drupal/views/Tests/UI/CustomBooleanTest.php b/core/modules/views/lib/Drupal/views/Tests/UI/CustomBooleanTest.php
index 0b2ff19b1017..2416c427fead 100644
--- a/core/modules/views/lib/Drupal/views/Tests/UI/CustomBooleanTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/UI/CustomBooleanTest.php
@@ -103,6 +103,7 @@ public function testCustomOption() {
 
       $view = views_get_view('test_view');
       $output = $view->preview();
+      $output = drupal_render($output);
 
       $replacements = array('%type' => $type);
       $this->{$values['test']}(strpos($output, $values['true']), format_string('Expected custom boolean TRUE value in output for %type.', $replacements));
diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewRenderTest.php b/core/modules/views/lib/Drupal/views/Tests/ViewRenderTest.php
index d70468b33419..8c7f07138cf3 100644
--- a/core/modules/views/lib/Drupal/views/Tests/ViewRenderTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/ViewRenderTest.php
@@ -41,7 +41,8 @@ public function testRender() {
 
     // Make sure that the rendering just calls the preprocess function once.
     $view = views_get_view('test_view_render');
-    $view->preview();
+    $output = $view->preview();
+    drupal_render($output);
 
     $this->assertEqual(state()->get('views_render.test'), 1);
   }
diff --git a/core/modules/views/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/display/DisplayTest.php b/core/modules/views/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/display/DisplayTest.php
index 0c42a58e930f..7497a8c6df10 100644
--- a/core/modules/views/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/display/DisplayTest.php
+++ b/core/modules/views/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/display/DisplayTest.php
@@ -127,8 +127,7 @@ public function execute() {
    * Override so preview and execute are the same output.
    */
   public function preview() {
-    $element = $this->execute();
-    return drupal_render($element);
+    return $this->execute();
   }
 
 }
diff --git a/core/modules/views/views.module b/core/modules/views/views.module
index cd313c6c80ef..58a7f358a2ad 100644
--- a/core/modules/views/views.module
+++ b/core/modules/views/views.module
@@ -69,7 +69,7 @@ function views_pre_render_view_element($element) {
 
   $view = views_get_view($element['#name']);
   if ($view && $view->access($element['#display_id'])) {
-    $element['view']['#markup'] = $view->preview($element['#display_id'], $element['#arguments']);
+    $element['view'] = $view->preview($element['#display_id'], $element['#arguments']);
   }
 
   return $element;
diff --git a/core/modules/views/views_ui/lib/Drupal/views_ui/ViewUI.php b/core/modules/views/views_ui/lib/Drupal/views_ui/ViewUI.php
index fe49e411c593..e4bd339a0f25 100644
--- a/core/modules/views/views_ui/lib/Drupal/views_ui/ViewUI.php
+++ b/core/modules/views/views_ui/lib/Drupal/views_ui/ViewUI.php
@@ -585,6 +585,7 @@ public function renderPreview($display_id, $args = array()) {
 
       // Execute/get the view preview.
       $preview = $this->executable->preview($display_id, $args);
+      $preview = drupal_render($preview);
 
       if ($show_additional_queries) {
         $this->endQueryCapture();
-- 
GitLab