diff --git a/core/modules/system/lib/Drupal/system/Plugin/views/style/Json.php b/core/modules/system/lib/Drupal/system/Plugin/views/style/Json.php new file mode 100644 index 0000000..34c139e --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/views/style/Json.php @@ -0,0 +1,57 @@ + array( + 'type' => array('default' => 'json'), + 'options' => array('default' => array()), + ), + ); + + return $options; + } + + /** + * Overrides Drupal\views\Plugin\views\display\PathPluginBase::optionsSummary(). + */ + public function optionsSummary(&$categories, &$options) { + parent::optionsSummary($categories, $options); + + unset($categories['page']); + unset($options['hide_admin_links'], $options['analyze-theme']); + + $categories['json'] = array( + 'title' => t('Json settings'), + 'column' => 'second', + 'build' => array( + '#weight' => -10, + ), + ); + + $options['path']['category'] = 'json'; + $options['path']['title'] = t('JSON'); + } + + /** + * Overrides Drupal\views\Plugin\views\display\PathPluginBase::execute(). + */ + public function execute() { + parent::execute(); + + $this->view->execute(); + return new JsonResponse($this->render()); + } + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::execute(). + */ + public function render() { + // @todo For now ... + return $this->view->result; + } + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::preview(). + */ + function preview() { + $this->view->execute(); + + if (!empty($this->view->result)) { + $response = new JsonResponse($this->render()); + return $response->getContent(); + } + + return ''; + } + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::buildOptionsForm(). + */ + public function buildOptionsForm(&$form, &$form_state) { + parent::buildOptionsForm($form, $form_state); + } + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::validateOptionsForm(). + */ + public function validateOptionsForm(&$form, &$form_state) { + parent::validateOptionsForm($form, $form_state); + } + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase:submitOptionsForm(). + */ + public function submitOptionsForm(&$form, &$form_state) { + parent::submitOptionsForm($form, $form_state); + } + +} 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..3c0491a --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayJsonTest.php @@ -0,0 +1,60 @@ + '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_json = json_encode($view->result); + $this->assertIdentical($actual_json, $expected_json, 'The expected JSON output was found.'); + + // Test a 403 callback. + $this->drupalGet('test/json/denied'); + $this->assertResponse(403); + } + +} 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..1f086f6 --- /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: default + row: + type: fields + 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: json + id: json_1 + display_title: Json + position: '' + display_options: + defaults: + access: false + path: test/json + access: + type: none + json_2: + display_plugin: json + 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