diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/display/Data.php b/core/modules/views/lib/Drupal/views/Plugin/views/display/Data.php new file mode 100644 index 0000000..aef3655 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Plugin/views/display/Data.php @@ -0,0 +1,129 @@ + t('Path settings'), + 'column' => 'second', + 'build' => array( + '#weight' => -10, + ), + ); + + $options['path']['category'] = 'path'; + $options['path']['title'] = t('Path'); + } + + /** + * Overrides Drupal\views\Plugin\views\display\PathPluginBase::execute(). + */ + public function execute() { + parent::execute(); + $output = $this->view->render(); + + $response = $this->view->getResponse(); + $response->setContent($output); + + return $response; + } + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::render(). + */ + public function render() { + $output = $this->view->style_plugin->render(); + if (!empty($this->view->live_preview)) { + return '
' . $output . '
'; + } + return $output; + } + +} diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/row/DataEntityRow.php b/core/modules/views/lib/Drupal/views/Plugin/views/row/DataEntityRow.php new file mode 100644 index 0000000..5ede700 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Plugin/views/row/DataEntityRow.php @@ -0,0 +1,41 @@ +_entity; + } + +} diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/row/DataFieldRow.php b/core/modules/views/lib/Drupal/views/Plugin/views/row/DataFieldRow.php new file mode 100644 index 0000000..187a832 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Plugin/views/row/DataFieldRow.php @@ -0,0 +1,52 @@ +view->field as $id => $field) { + $output[$field->field_alias] = $row->{$field->field_alias}; + } + + return $output; + } + +} diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/style/Json.php b/core/modules/views/lib/Drupal/views/Plugin/views/style/Json.php new file mode 100644 index 0000000..6cbbe64 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Plugin/views/style/Json.php @@ -0,0 +1,66 @@ +view->setResponse($response); + } + + + /** + * Overrides Drupal\views\Plugin\views\style\StylePluginBase::render(). + */ + public function render() { + $rows = array(); + + foreach ($this->view->result as $row) { + $rows[] = $this->row_plugin->render($row); + } + $response = $this->view->getResponse(); + $response->setData($rows); + + return $response->getContent(); + } + +} diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayJsonTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayJsonTest.php new file mode 100644 index 0000000..308babc --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayJsonTest.php @@ -0,0 +1,68 @@ + 'Display: JSON plugin', + 'description' => 'Tests the JSON display plugin.', + 'group' => 'Views Plugins', + ); + } + + protected function setUp() { + parent::setUp(); + + for ($i = 1; $i <= 10; $i++) { + $this->drupalCreateNode(); + } + + $this->enableViewsTestModule(); + } + + /** + * Checks the behavior of JSON callback paths. + */ + public function testJsonResponses() { + $view = views_get_view('test_json_display'); + $view->initDisplay(); + $this->executeView($view); + + // Test the JSON callback. + $actual_json = $this->drupalGet('test/json'); + $this->assertResponse(200); + // Test the http content-type. + $headers = $this->drupalGetHeaders(); + $this->assertEqual($headers['content-type'], 'application/json', 'The header content-type is correct.'); + + $expected = array(); + foreach ($view->result as $row) { + $expected_row = array(); + foreach ($view->field as $id => $field) { + $expected_row[$field->field_alias] = $row->{$field->field_alias}; + } + $expected[] = $expected_row; + } + + $this->assertIdentical($actual_json, json_encode($expected), 'The expected JSON output was found.'); + + // Test a 403 callback. + $this->drupalGet('test/json/denied'); + $this->assertResponse(403); + } + +} diff --git a/core/modules/views/lib/Drupal/views/ViewExecutable.php b/core/modules/views/lib/Drupal/views/ViewExecutable.php index 0367905..29377a9 100644 --- a/core/modules/views/lib/Drupal/views/ViewExecutable.php +++ b/core/modules/views/lib/Drupal/views/ViewExecutable.php @@ -1241,9 +1241,6 @@ public function render($display_id = NULL) { drupal_theme_initialize(); $config = config('views.settings'); - // Set the response so other parts can alter it. - $this->response = new Response('', 200); - $start = microtime(TRUE); if (!empty($this->live_preview) && $config->get('ui.show.additional_queries')) { $this->startQueryCapture(); @@ -1274,6 +1271,11 @@ public function render($display_id = NULL) { // Initialize the style plugin. $this->initStyle(); + if (!isset($this->response)) { + // Set the response so other parts can alter it. + $this->response = new Response('', 200); + } + // Give field handlers the opportunity to perform additional queries // using the entire resultset prior to rendering. if ($this->style_plugin->usesFields()) { diff --git a/core/modules/views/tests/views_test_config/config/views.view.test_json_display.yml b/core/modules/views/tests/views_test_config/config/views.view.test_json_display.yml new file mode 100644 index 0000000..7dc391b --- /dev/null +++ b/core/modules/views/tests/views_test_config/config/views.view.test_json_display.yml @@ -0,0 +1,74 @@ +base_table: views_test_data +name: test_json_display +description: '' +tag: '' +human_name: 'Test JSON display' +core: 8.x +api_version: '3.0' +display: + default: + display_plugin: default + id: default + display_title: Master + position: '' + display_options: + access: + type: perm + options: + perm: 'view published content' + cache: + type: none + query: + type: views_query + exposed_form: + type: basic + pager: + type: full + options: + items_per_page: '10' + style: + type: json + row: + type: data_field + fields: + name: + id: name + table: views_test_data + field: name + label: '' + sorts: + created: + id: created + table: views_test_data + field: created + order: DESC + title: 'Test JSON' + arguments: { } + json_1: + display_plugin: data + id: json_1 + display_title: Json + position: '' + display_options: + defaults: + access: false + path: test/json + access: + type: none + json_2: + display_plugin: data + id: json_2 + display_title: 'Json - access denied' + position: '' + display_options: + defaults: + access: false + path: test/json/denied + access: + type: perm + options: + perm: 'administer views' +base_field: id +disabled: '0' +module: views +langcode: und