Service Container - Drupal 8 code working in Drupal 7 out of the box

Fabianx

Motivation

Drupal 8 is near and it would be great to start using concepts from it. However one important thing is missing, a service container and a new usage of plugins.

Also one of the great things in Drupal 8 is unit testing, but to unit test procedural code needs to be wrapped.

Service_Container has Drupal 8 code

In the service_container module there is a checkout of a recent version of selected components from \Drupal\Core and \Drupal\Component.

And many things like the key-value store, the database layer (with one API change), the lock backend, the logging component can be used out of the box. In render_cache also selected parts of the Cache system are used directly. OpenLayers uses the plugin system (based on ctools plugins).

ModuleHandler / ModuleInstaller can also be used directly, but they mainly are a wrapper around the Drupal 7 functions.

Drupal 7 code will be easier to find

Last there is a BC layer for Drupal 7 code, which means you can do:

\Drupal::service('drupal7')->call('node_load', array(1))

And such you can also inject the service into your class as the '@drupal7' service.

Learn

It is way simpler to learn Drupal 8 concepts while being in a Drupal 7 system. It is also simpler to learn all of this as generic concepts using familiar patterns like ctools plugins than to learn all of OOP, Symfony, components, 1000s of API changes, YML syntax, etc.

With this you can start writing modules that will be ready for Drupal 8 in a way that is conceptual to how core works now.

Goals

  • Learn what a service container is
  • Learn how to leverage plugins to build a powerful container system
  • Learn how to write classes that are unit-testable
  • The advantages of unit testing and why its awesome
  • Drupal 8 components you can start using now.

I am very happy to share this session with another session on the same topic - if track chair preferred, because there are definitely more approaches in the contrib/ eco world that help write D8 ready code.

Session Track

Coding and Development

Experience Level

Beginner

Drupal Version