I need to set the user timezone on all pages in the JS
drupalSettings object, so that it can be accessed on any page. As
drupalSettings.user is provided by core, I want to set
drupalSettings.user.timezone, with the user timezone from the server.
While this can quickly and easily be done in hook_page_attachments(), I am trying to switch from using hooks, to using Event Subscribers, as Drupal hooks are the old paradigm, and event subscribers is the new paradigm.
Can someone explain how to add attached assets (a library, or drupalSettings) to all pages from an event subscriber?
As mentioned by 4uk4 in the comments, this event is not dispatched by Drupal core, but rather by Symfony underneath the hood. This means it is not a true 'Drupal' methodology, as core maintainers have not chosen to dispatch an event that is equivalent to hook_page_attachments(). So which is better? The answer is 'neither' at this point, as core is ambivalent as to whether it invokes a hook or dispatches an event. It does however seem that core is moving towards creating events that match existing hooks, as can be seen in this issue: https://www.drupal.org/project/drupal/issues/2551893. And here is a discussion that considers the differences between hooks and events: https://www.previousnext.com.au/blog/alter-or-dispatch-drupal-8-events-versus-alter-hooks