I am puzzled how to alter the response headers from specific hook (hook_file_download)?
/**
* Implements hook_file_download().
*/
function example_file_download($uri) {
$scheme = FileSystem::uriScheme($uri);
if ($scheme === 'public') {
// Set custom headers here.
}
}
Basically I want under certain conditions to set additional headers and in D7 this would happen with drupal_add_http_header() but in D8 as far as I can see this can happen only by implementing custom subscriber and using getSubscribedEvents and then KernelEvents. However this does not solve my case as I want to add custom headers only for specific file types.
If I use custom subscriber then this would be possible by:
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = ['exampleFunc'];
return $events;
}
And then:
/**
*
*/
public function exampleFunc(FilterResponseEvent $event) {
$response->headers->set('Header', 'Value');
}