Drupal gets better when companies, organizations, and individuals build or fix something they need and then share it with the rest of us. Our community becomes better, stronger, and smarter when others take it upon themselves to make a positive difference contributing their knowledge, time, and energy to Drupal. Acquia is proud to play a part, alongside thousands of others, in some of the stories making tomorrow’s Drupal better than today’s. One of them is Nida Ismail Shah’s.
Nida Ismail Shah calls his approach to technology “trying to always stay a beginner”; he feels he’s always learning and there’s always something new to get on top of. “I started learning Drupal just a couple of years ago, and like everyone else, I started with hooks ... One could say that most of Drupal was about hooks back then :-) ... Object oriented PHP now lets us work with a lot more tools, like Events, Plugins, Services, and more. To further my own knowledge and share my experience with the community I wanted to talk about Drupal 8 hooks and events in my session—when and where you’ll find them and what they’re about.”
“I think the most important thing to note here is that we have a working event model in place in D8 and a module developer can build a stable version of a module without even having to expose a single hook.” - Nida Ismail Shah
jam: What will I learn from your session and why is knowing about hooks and events in Drupal important?
Nida: “The session is targeted at module developers. Most of the work that Drupal developers do is extending or altering pre-existing functionality from modules or Drupal core. In both cases, we end up implementing a hook or subscribing to an event. I find where we are in the overall transition from hooks to events interesting; hence this session.
If you’re working on extending Drupal core functionality, you’re still bound to use whatever the core offers: currently mostly hooks and a few events. But with this session I wanted to show module developers that whenever they want other developers to be able extend their modules, they should be firing events.”
Nida: In this session, I show you how to
- Create their custom events in Drupal 8.
- Subscribe to the events from core and other modules and to their custom events, too.
- Know where to explore events and where we have to still use hooks.
jam: What value do hooks and events in Drupal 8 deliver to ...
- ... developers? Nida: Hooks and events are mainly responsible for making Drupal extensible. With them, we can alter core functionalities, extend, and even customise them. The same goes for module developers, who other developers alter, extend and customise their modules via hooks and events.
- ... site owners and businesses? Nida: We’re talking about real developer stuff here, but this is the stuff that enables us developers to build great customised applications for our clients in the end.
- ... the Drupal community? Nida: We’ve been dealing with hooks for a long time now and we’ve gotten a lot of value out of them. But with the inclusion of the Symfony Event Dispatcher component in Drupal 8 core, I think we are slowly moving towards a much better developer experience. Drupal.org issues about shifting from hooks to events are in fact tagged with “DX.” This shows me that we, as a community, are continuing to evolve and adopt best practices from wherever we can. Having our coding practices much more in line with main-line PHP is a big help here.
DrupalCon Dublin Session [video]
- DrupalCon session description: The State of Hooking into Drupal
- Nida created a simple working demo how to create and subscribe to events, the code is here on GitHub, but he says, “it’s pretty simple stuff.” ;-)
jam: What is the state-of-play and what is coming next regarding hooks and events in Drupal?
Nida: In the past, Drupal was mostly procedural code and older versions of PHP didn’t really do OOP anyway. Drupal’s old hook system that was built as a workaround to that fact. We’re in transition away from hooks now to an object oriented event-based system. I think the most important thing to note here is that we have a working event model in place in D8 and a module developer can build a stable version of a module without even having to expose a single hook. We are not there yet, but the path and transition are going to be interesting. We have discussions going on about moving to the Symfony Event dispatcher completely, but not until Drupal 9.
jam: How has Drupal 8 affected hooks and events in Drupal?
Nida: It hasn’t not affected hooks that much yet, we have some new hooks altogether in D8 and we have gotten rid of some (hook_init, hook_boot, hook_exit) as well. I think the most important thing to note here is that we have a working event model in place in D8 and a module developer can build a stable version of a module without even having to expose a single hook. But we still do have to implement hooks if we want to extend the functionalities from core.