You are here

public function AddHttpHeaders::onResponse in Access unpublished 8

Set HTTP headers configured on admin/config/content/access_unpublished.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event: Event.

File

src/EventSubscriber/AddHttpHeaders.php, line 49

Class

AddHttpHeaders
Add new HTTP headers as added in settings form.

Namespace

Drupal\access_unpublished\EventSubscriber

Code

public function onResponse(FilterResponseEvent $event) {
  if (!$event
    ->isMasterRequest()) {
    return;
  }
  if ($this->tokenGetter
    ->getToken()) {
    foreach ($this->config
      ->get('modify_http_headers') as $key => $header) {

      // Must remove the existing header if settings a new value.
      if ($event
        ->getResponse()->headers
        ->has($key)) {
        $event
          ->getResponse()->headers
          ->remove($key);
      }
      $event
        ->getResponse()->headers
        ->set($key, $header);
    }
  }
}