Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / src / Plugin / views / area / HTTPStatusCode.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\area;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Symfony\Component\HttpFoundation\Response;
7
8 /**
9  * Alter the HTTP response status code used by the view.
10  *
11  * @ingroup views_area_handlers
12  *
13  * @ViewsArea("http_status_code")
14  */
15 class HTTPStatusCode extends AreaPluginBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   protected function defineOptions() {
21     $options = parent::defineOptions();
22
23     $options['status_code'] = ['default' => 200];
24
25     return $options;
26   }
27
28   /**
29    * {@inheritdoc}
30    */
31   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
32     parent::buildOptionsForm($form, $form_state);
33
34     // Get all possible status codes defined by symfony.
35     $options = Response::$statusTexts;
36
37     // Move 403/404/500 to the top.
38     $options = [
39       '404' => $options['404'],
40       '403' => $options['403'],
41       '500' => $options['500'],
42     ] + $options;
43
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]);
47     });
48
49     $form['status_code'] = [
50       '#title' => $this->t('HTTP status code'),
51       '#type' => 'select',
52       '#default_value' => $this->options['status_code'],
53       '#options' => $options,
54     ];
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function render($empty = FALSE) {
61     if (!$empty || !empty($this->options['empty'])) {
62       $build['#attached']['http_header'][] = ['Status', $this->options['status_code']];
63       return $build;
64     }
65   }
66
67 }