diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/HTTPStatusCode.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/HTTPStatusCode.php new file mode 100644 index 0000000..7fc4419 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/HTTPStatusCode.php @@ -0,0 +1,67 @@ + 200); + + return $options; + } + + /** + * Overrides \Drupal\views\Plugin\views\area\AreaPluginBase::buildOptionsForm(). + */ + public function buildOptionsForm(&$form, &$form_state) { + parent::buildOptionsForm($form, $form_state); + + // Get all possible status code defined by symfony. + $options = Response::$statusTexts; + + // Add the HTTP status code, so it's easier for people to find it. + array_walk($options, function($title, $code) use(&$options) { + $options[$code] = t('@code (@title)', array('@code' => $code, '@title' => $title)); + }); + + $form['status_code'] = array( + '#title' => t('HTTP status code'), + '#type' => 'select', + '#default_value' => $this->options['status_code'], + '#options' => $options, + ); + } + + /** + * Overrides \Drupal\views\Plugin\views\area\AreaPluginBase::render(). + */ + function render($empty = FALSE) { + if (!$empty || !empty($this->options['empty'])) { + $response = $this->view->getResponse(); + $response->setStatusCode($this->options['status_code']); + } + } + +} diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/display/Page.php b/core/modules/views/lib/Drupal/views/Plugin/views/display/Page.php index 889a14f..3802663 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/display/Page.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/display/Page.php @@ -92,7 +92,10 @@ public function execute() { // And the title, which is much easier. drupal_set_title(filter_xss_admin($this->view->getTitle()), PASS_THROUGH); - return $render; + $response = $this->view->getResponse(); + $response->setContent(drupal_render_page($render)); + + return $response; } /** diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/AreaHTTPStatusCodeTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaHTTPStatusCodeTest.php new file mode 100644 index 0000000..3e49153 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaHTTPStatusCodeTest.php @@ -0,0 +1,43 @@ + 'Area: HTTP Status Code', + 'description' => 'Tests the http_status_code area handler.', + 'group' => 'Views Handlers', + ); + } + + /** + * Tests the area handler. + */ + public function testHTTPStatusCodeHandler() { + $this->drupalGet('test-http-status-code'); + $this->assertResponse(200); + + // Change the HTTP status code to 403. + $view = views_get_view('test_http_status_code'); + $display = &$view->storage->getDisplay('default'); + $display['display_options']['empty']['http_status_code']['status_code'] = 418; + $view->save(); + + // Test that the HTTP response is "I'm a teapot". + $this->drupalGet('test-http-status-code'); + $this->assertResponse(418); + } + +} diff --git a/core/modules/views/tests/views_test_config/config/views.view.test_http_status_code.yml b/core/modules/views/tests/views_test_config/config/views.view.test_http_status_code.yml new file mode 100644 index 0000000..02b44d6 --- /dev/null +++ b/core/modules/views/tests/views_test_config/config/views.view.test_http_status_code.yml @@ -0,0 +1,82 @@ +api_version: '3.0' +base_field: nid +base_table: node +core: 8.x +description: '' +disabled: '0' +display: + default: + display_plugin: default + id: default + display_title: Master + position: '' + display_options: + access: + type: perm + cache: + type: none + query: + type: views_query + exposed_form: + type: basic + pager: + type: full + style: + type: default + row: + type: fields + fields: + title: + id: title + table: node + field: title + label: '' + alter: + alter_text: '0' + make_link: '0' + absolute: '0' + trim: '0' + word_boundary: '0' + ellipsis: '0' + strip_tags: '0' + html: '0' + hide_empty: '0' + empty_zero: '0' + link_to_node: '1' + filters: + status: + value: '1' + table: node + field: status + id: status + expose: + operator: '0' + group: '1' + sorts: + created: + id: created + table: node + field: created + order: DESC + empty: + http_status_code: + id: http_status_code + table: views + field: http_status_code + relationship: none + group_type: group + admin_label: '' + label: '' + empty: '1' + status_code: '200' + page_1: + display_plugin: page + id: page_1 + display_title: Page + position: '' + display_options: + path: test-http-status-code +human_name: test_http_status_code +module: views +name: test_http_status_code +tag: '' diff --git a/core/modules/views/views.views.inc b/core/modules/views/views.views.inc index 3a145b2..e799787 100644 --- a/core/modules/views/views.views.inc +++ b/core/modules/views/views.views.inc @@ -88,6 +88,14 @@ function views_views_data() { 'area' => array( 'id' => 'result', ), +) ; + + $data['views']['http_status_code'] = array( + 'title' => t('Response status code'), + 'help' => t('Alter the HTTP response status code used by this view, mostly helpful for empty results.'), + 'area' => array( + 'id' => 'http_status_code', + ), ); $data['views']['combine'] = array(