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 0000000000000000000000000000000000000000..d44887689529797dfed675b3e4c7dd5fb618f796 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/HTTPStatusCode.php @@ -0,0 +1,71 @@ +<?php + +/** + * @file + * Contains \Drupal\views\Plugin\views\area\HTTPStatusCode. + */ + +namespace Drupal\views\Plugin\views\area; + +use Symfony\Component\HttpFoundation\Response; +use Drupal\Component\Annotation\PluginID; + +/** + * Alter the HTTP response status code used by the view. + * + * @ingroup views_area_handlers + * + * @PluginID("http_status_code") + */ +class HTTPStatusCode extends AreaPluginBase { + + /** + * {@inheritdoc} + */ + protected function defineOptions() { + $options = parent::defineOptions(); + + $options['status_code'] = array('default' => 200); + + return $options; + } + + /** + * {@inheritdoc} + */ + public function buildOptionsForm(&$form, &$form_state) { + parent::buildOptionsForm($form, $form_state); + + // Get all possible status codes defined by symfony. + $options = Response::$statusTexts; + + // Move 403/404/500 to the top. + $options = array( + '404' => $options['404'], + '403' => $options['403'], + '500' => $options['500'], + ) + $options; + + // 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, + ); + } + + /** + * {@inheritdoc} + */ + function render($empty = FALSE) { + if (!$empty || !empty($this->options['empty'])) { + $this->view->getResponse()->setStatusCode($this->options['status_code']); + } + } + +} 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 0000000000000000000000000000000000000000..31348ba8361bb81be79c3c3b105d1569b86ac06e --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaHTTPStatusCodeTest.php @@ -0,0 +1,50 @@ +<?php + +/** + * @file + * Contains \Drupal\views\Tests\Handler\AreaHTTPStatusCodeTest. + */ + +namespace Drupal\views\Tests\Handler; + +/** + * Tests the http_status_code area handler. + * + * @see \Drupal\views\Plugin\views\area\HTTPStatusCode + */ +class AreaHTTPStatusCodeTest extends HandlerTestBase { + + /** + * Views used by this test. + * + * @var array + */ + public static $testViews = array('test_http_status_code'); + + public static function getInfo() { + return array( + 'name' => '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 418. + $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/modules/views_test_config/test_views/views.view.test_http_status_code.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_http_status_code.yml new file mode 100644 index 0000000000000000000000000000000000000000..56ccb7cfa4316f109e3ffe521a4667a7eb869d2f --- /dev/null +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_http_status_code.yml @@ -0,0 +1,81 @@ +base_field: nid +base_table: node +core: 8.x +description: '' +status: '1' +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 +label: test_http_status_code +module: views +id: test_http_status_code +tag: '' diff --git a/core/modules/views/views.views.inc b/core/modules/views/views.views.inc index 99d23c1d2ad8d5744a74dcb9cbeb5421f1c94a9e..9b49b9257898aee004fb8d37aeca8a4468fd279f 100644 --- a/core/modules/views/views.views.inc +++ b/core/modules/views/views.views.inc @@ -90,6 +90,14 @@ function views_views_data() { ), ); + $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( 'title' => t('Combine fields filter'), 'help' => t('Combine two fields together and search by them.'),