I want to alter a HTTP header and I am looking for the appropriate hook to use. I can tell a general time of when some hooks are called but not exactly when. How do I know exactly when hooks are called, and what hook I should be using to change my header?

edit: I have managed to change my HTTP header. But something else is adding to my header and that is taking precedent over my header portion. Is there a way to know where this is coming from?

  • 1
    The simplest way would probably be to fire up XDebug and put breakpoints in ModuleHandler::invoke and ModuleHandler::invokeAll
    – Clive
    May 14, 2015 at 16:32
  • api.drupal.org/api pages for hooks also show where hooks are invoked from which can be useful in some cases.
    – rooby
    May 14, 2015 at 16:33
  • @rooby I am looking on the page and thats where I am finding my hooks, but I dont see a tree or hierarchy of hooks or anything. They seem to be sorted alphabetically May 14, 2015 at 18:31
  • You have to search for the hook you are interested in (for example hook_theme), then look for the collapsed fieldset that lists invocations of the hook, then you can follow the links in that fieldset.
    – rooby
    May 14, 2015 at 23:06

1 Answer 1


You're looking for an event, not a hook to do this in 8.x

Specifically, you're looking for the RESPONSE event, see \Drupal\Core\EventSubscriber\AnonymousUserResponseSubscriber for an example. Create a class like that, register it in yourmodule.services.yml and then do whatever you want with the $response object.

  • How do I create a class like this? Do you mean I would define the class in my .module file? or is it in the service.yml file? I am not sure what all is necessary in the module.services.yml file? May 18, 2015 at 14:48
  • You need to create the class in the src/ folder of your module, according to the PSR-4 standards, like every other class as well. In services.yml, you need to register it. Look at core.services.yml, search for AnonymousUserResponseSubscriber, then use the definition there as an example.
    – Berdir
    May 18, 2015 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.