Dispatching events

Since we have discussed how to subscribe to events in Drupal 8, it would only be fair to quickly take a look at how we can dispatch our own events. After all, the Symfony Event Dispatcher component is one of the principle vectors of extensibility in Drupal 8.

To demonstrate this, we will create an event to be dispatched whenever our HelloWorldSalutation::getSalutation() method is called. The purpose is to inform other modules that this happened and potentially allow them to alter the message that comes out of the configuration object-not really a solid use case, but good enough to demonstrate how we can dispatch events.

The first thing that we will need to do is to create an event class that will be dispatched. It can go ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.