XIO Experiences @DrupalCon - Part 1
Ballerina style development in Drupal 8: Flexible building with Dependency Injection by Matthias Vandermaesen.
Intended audience: Developers
With DrupalCon Amsterdam, the very first beta of Drupal 8 has been released to world. This is a huge milestone towards the next major version of Drupal. With this release, developers can expect serious changes under the hood. While familiar concepts such as modules, nodes, entities, fields and formatters are still there, the APIs that allow you to ‘hook’ your custom code in Drupal have undergone massive improvements.
Drupal has always been a project with its own peculiarities and quirks in the world of PHP frameworks. However, the PHP community has evolved rapidly. Instead of reinventing the wheel, code reuse between projects has become standard practice. And so, as Larry Garfield put it, the Drupal project is ‘coming from its island’ with this new release. The backbone of Drupal 8 is composed by third party code from large projects such as Symfony.
Introducing Dependency Injection
One of the biggest changes for developers, will be leaving the classic hook system we’ve all grown to love and/or hate. Dependency Injection is the new sheriff in town. But what is it? And why should you use it? Fabien Potencier, lead of the Symfony project, explained the concept succinctly in his DrupalCon Amsterdam presentation.
The new APIs are loosely coupled sets of object oriented classes instead of tightly interlocking chains of functions. Dependency Injection is the mechanism that allows objects to easily exchange information or alter their behaviour without hard coding dependencies between chunks of code. This allows developers to build componentized code which has a high maintainability, easily swap out or extend most components shipped with Drupal 8 with their custom implementations and introduce third party libraries into their projects.
Modules interact with the Drupal through a pattern called Dependency Injection. Instead of directly creating new objects within functions and methods, we instantiate them outside and pass (or inject) them via class constructors or setter methods. This solves the issue of hard coding dependencies between objects.
Drupal 8 introduces a new API called the Dependency Inject Container (DIC). This central object dynamically manages all dependencies between components. Developers interact with the container via an abstract layer. Instead of writing PHP code, you just define them in a YAML configuration file. Drupal 8 itself will generate - or compile - all the PHP code based on that configuration. This really lowers the bar for developers to deal with dependencies.
However, understanding what a Dependency Injection Container is and what it does, is can be challenging. After Fabien Potenciers’ session, I noticed some confusion among the audience. So, here’s a few pointers towards good background documentation about the concept:
I’ve also created a small, documented PHP example application. It demos Symfonys’ service container which can be found at the heart of Drupal 8.
About "XIO-experiences @DrupalCon":
The XIO team had something to look forward to this summer: DrupalCon was coming up! It’s our annual tradition to go to the conference. DrupalCon 2014 took place in Amsterdam and featured the launch of the Drupal 8 beta. To get the most out of their visit, each team member wrote a small report about their favourite session... for knowledge sharing, inspiration and fun! Read more about their findings, insights and tips that might be useful to you as well...
More experiences are coming up!