Drupal in version 8 adapted Symfony Event dispatcher to have a better alternative to the hook system already in-place until version 7. The hook system is still there, but it is slowly being replaced by the Event dispatcher. The Event Dispatcher allows various components and modules to interact with each other without having to directly modify the code.
Basically, subscribers can listen to specific events which are then fired via the dispatcher. Each subscriber listening to the specific event is the notified and passed an Event object which they can use to manipulate the dispatcher’s behaviour.
For each user, Hawk Auth module builds a list of credentials which are then rendered by the theme. The list is a PHP array containing various information about the page’s elements and looks. This list has no way of being modified (at least not any I could find). Previously in Drupal 7, a hook called hook_page_alter could be used to modify all the contents in a page’s build but it was removed as it could be used to change almost any content to almost anything else. To work around this, I built my own event called hawk_auth.view_credentials. This event is passed a HawkAuthCredentialsViewEvent object which contains the page’s build array. Any event listening can change and set the build.
I implemented the above event in order to allow other modules to modify hawk’s credential build. For example, I also implemented a module which adds QR codes for each Hawk credentials. The QR codes can be scanned via a device and can easily add the credential instead of typing the key manually. The code can be seen at the module’s repository, the services file define the classes, namely HawkAuthQrEventSubscriber which tells Drupal that the module will be listening to hawk’s events. The class also handles the event’s callback and adds QR code’s link to the credential view.
This took me a while to implement, mostly because I spent a good amount of time trying to figure out if there was a way to implement what I wanted to do with existing events or hooks, however those attempts were futile and I ultimately decided to implement my own Event.