class AddHttpHeaders in Access unpublished 8
Add new HTTP headers as added in settings form.
Hierarchy
- class \Drupal\access_unpublished\EventSubscriber\AddHttpHeaders implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of AddHttpHeaders
1 string reference to 'AddHttpHeaders'
1 service uses AddHttpHeaders
File
- src/
EventSubscriber/ AddHttpHeaders.php, line 14
Namespace
Drupal\access_unpublished\EventSubscriberView source
class AddHttpHeaders implements EventSubscriberInterface {
/**
* Access Unpublished custom configurations.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $config;
/**
* The token getter service.
*
* @var \Drupal\access_unpublished\TokenGetter
*/
protected $tokenGetter;
/**
* Constructs a new response subscriber.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory service.
* @param \Drupal\access_unpublished\TokenGetter $tokenGetter
* The token getter service.
*/
public function __construct(ConfigFactoryInterface $configFactory, TokenGetter $tokenGetter) {
$this->config = $configFactory
->get('access_unpublished.settings');
$this->tokenGetter = $tokenGetter;
}
/**
* Set HTTP headers configured on admin/config/content/access_unpublished.
*
* @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
* Event.
*/
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);
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = [
'onResponse',
];
return $events;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddHttpHeaders:: |
protected | property | Access Unpublished custom configurations. | |
AddHttpHeaders:: |
protected | property | The token getter service. | |
AddHttpHeaders:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
AddHttpHeaders:: |
public | function | Set HTTP headers configured on admin/config/content/access_unpublished. | |
AddHttpHeaders:: |
public | function | Constructs a new response subscriber. |