You are here

class HTTPStatusCode in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/area/HTTPStatusCode.php \Drupal\views\Plugin\views\area\HTTPStatusCode
  2. 9 core/modules/views/src/Plugin/views/area/HTTPStatusCode.php \Drupal\views\Plugin\views\area\HTTPStatusCode

Alter the HTTP response status code used by the view.

Plugin annotation

@ViewsArea("http_status_code");

Hierarchy

  • class \Drupal\views\Plugin\views\area\AreaPluginBase extends \Drupal\views\Plugin\views\HandlerBase

Expanded class hierarchy of HTTPStatusCode

Related topics

File

core/modules/views/src/Plugin/views/area/HTTPStatusCode.php, line 15

Namespace

Drupal\views\Plugin\views\area
View source
class HTTPStatusCode extends AreaPluginBase {

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['status_code'] = [
      'default' => 200,
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $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 = [
      '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] = $this
        ->t('@code (@title)', [
        '@code' => $code,
        '@title' => $title,
      ]);
    });
    $form['status_code'] = [
      '#title' => $this
        ->t('HTTP status code'),
      '#type' => 'select',
      '#default_value' => $this->options['status_code'],
      '#options' => $options,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function render($empty = FALSE) {
    if (!$empty || !empty($this->options['empty'])) {
      $build['#attached']['http_header'][] = [
        'Status',
        $this->options['status_code'],
      ];
      return $build;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AreaPluginBase::$areaType public property The type of this area handler, i.e. 'header', 'footer', or 'empty'.
AreaPluginBase::adminSummary public function
AreaPluginBase::init public function Overrides Drupal\views\Plugin\views\HandlerBase::init(). 1
AreaPluginBase::isEmpty public function Does that area have nothing to show. 1
AreaPluginBase::preRender public function Performs any operations needed before full rendering. 1
AreaPluginBase::usesGroupBy public function
HTTPStatusCode::buildOptionsForm public function Overrides AreaPluginBase::buildOptionsForm
HTTPStatusCode::defineOptions protected function Overrides AreaPluginBase::defineOptions
HTTPStatusCode::render public function Render the area. Overrides AreaPluginBase::render