In drupal 8, we now have the ability to use event subscribers. What's the reason for this? Is it to work with the new cache API better?

Recently I had a task of needing to extract data from a node when it was viewed, but hook_node_view() is cached (like nearly everything) in Drupal 8. So I ended up using an event subscriber. Why is using event subscribers a better solution? Is it just that it's more object orientated?

  • 3
    I can imagine there will come a day when just about everything is an event subscriber. IMO the intent is more clear than alter hooks.
    – Kevin
    Commented Nov 27, 2017 at 0:31

1 Answer 1


You can see this from two perspectives. First of someone who is looking for a way to communicate with Drupal and so is using the hooks and events that exist. The second one is that of a developer who designs these APIs.

Drupal is in the middle of both, on the one side Drupal is a consumer of the Symfony API, almost every piece of code is placed in Symfony events, and on the other side Drupal provides APIs, in form of legacy hooks or custom Symfony events and other methods like plugins.

You probably didn't use one of the Drupal APIs, more likely a Symfony kernel event, KernelEvents::REQUEST. Then you are in the same position as a Drupal developer and this is not a question about hooks or events, because Symfony doesn't provide hooks.

Even if you were using one of the Drupal APIs, you wouldn't have much choice. You would have to select what is provided to you. Only if you design an API in your module to interact with other modules, then you can choose between an event or a hook.

  • Yea I see that Symfony doesn't provide hooks but I now have the option to to use hook_node_view, or a Symfony event. But which is preferred and why is what I'm interested in. Commented Nov 27, 2017 at 10:15
  • This depends on where you want to hook in. If you know exactly where this should be most times you have no choice, you have to take what the API is offering you. A discussion of the preferred method only makes sense if you design an API.
    – 4uk4
    Commented Nov 27, 2017 at 10:24
  • 1
    To make life easier for people coming from D7?
    – 4uk4
    Commented Nov 27, 2017 at 10:32
  • 3
    @Kevinhowbrook Further reading: previousnext.com.au/blog/…
    – Clive
    Commented Nov 27, 2017 at 11:37
  • 1
    This is not a duplicate method, both are completely different places to hook into Drupal. If you want to have priority over a kernel event subscriber, eg DynamicPageCacheSubscriber, then you need to subscribe to the same kernel event and use a higher priority. The hook here is in the early stages of the render pipeline of an entity. This is not called again, like any other code involved, when the rendered result is delivered from cache.
    – 4uk4
    Commented Nov 27, 2017 at 12:53

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.