3 namespace Drupal\views\Plugin\views\area;
5 use Drupal\Core\Form\FormStateInterface;
6 use Symfony\Component\HttpFoundation\Response;
9 * Alter the HTTP response status code used by the view.
11 * @ingroup views_area_handlers
13 * @ViewsArea("http_status_code")
15 class HTTPStatusCode extends AreaPluginBase {
20 protected function defineOptions() {
21 $options = parent::defineOptions();
23 $options['status_code'] = ['default' => 200];
31 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
32 parent::buildOptionsForm($form, $form_state);
34 // Get all possible status codes defined by symfony.
35 $options = Response::$statusTexts;
37 // Move 403/404/500 to the top.
39 '404' => $options['404'],
40 '403' => $options['403'],
41 '500' => $options['500'],
44 // Add the HTTP status code, so it's easier for people to find it.
45 array_walk($options, function($title, $code) use(&$options) {
46 $options[$code] = $this->t('@code (@title)', ['@code' => $code, '@title' => $title]);
49 $form['status_code'] = [
50 '#title' => $this->t('HTTP status code'),
52 '#default_value' => $this->options['status_code'],
53 '#options' => $options,
60 public function render($empty = FALSE) {
61 if (!$empty || !empty($this->options['empty'])) {
62 $build['#attached']['http_header'][] = ['Status', $this->options['status_code']];