Decoupling the model from Drupal
Back in the Drupal 6 days, I built the BOM Weather Drupal module to pull down weather data from the Australian Bureau of Meteorology (BOM) site, and display it to users.
We recently had a requirement for this in a new Drupal 8 site, so decided to take a more modern approach.
by
Kim Pepper
/ 4 July 2018
Not that kind of decoupled Drupal
We often hear the term Decoupled Drupal but I'm not talking about a Javascript front-end and Drupal Web Service API backend.
This kind of decoupling is removing the business logic away from Drupal concepts. Drupal then becomes a wrapper around the library to handle incoming web requests, configuration and display logic.
We can write the business logic as a standalone PHP package, with it's own domain models, and publish it to Packagist.org to be shared by both Drupal and non-Drupal projects.
The Bom Weather Library
We started by writing unit-testable code, that pulled in weather forecast data in an XML format, and produced a model in PHP classes that is much easier for consuming code to use. See the full BOM Weather code on GitHub
For example:
$client = new BomClient($logger);
$forecast = $client->getForecast('IDN10031');
$issueTime = $forecast->getIssueTime();
$regions = $forecast->getRegions();
$metros = $forecast->getMetropolitanAreas();
$locations = $forecast->getLocations();
foreach ($locations as $location) {
$aac = $location->getAac();
$desc = $location->getDescription();
/** @var \BomWeather\Forecast\ForecastPeriod[] $periods */
$periods = $location->getForecastPeriods();
// Usually 7 days of forecast data.
foreach ($periods as $period) {
$date = $period->getStartTime();
$maxTemp = $period->getAirTempMaximum();
$precis = $period->getPrecis();
}
}
The library takes care of fetching the data, and the idiosyncrasies of a fairly crufty API (no offence intended!).
Unit Testing
We can have very high test coverage with our model. We can test the integration with mock data, and ensure a large percentage of the code is tested. As we are using PHPUnit tests, they are lightning fast, and are automated as part of a Pull Request workflow on CircleCI.
Any consuming Drupal code can focus on testing just the Drupal integration, and not need to worry about the library code.
Dependency Management
As this is a library, we need to be very careful not to introduce too many runtime dependencies. Also any versions of those dependencies need to be more flexible than what you would normally use for a project. If you make your dependency versions too high they can introduce incompatibilities when used a project level. Consumers will simply not be able to add your library via composer.
We took a strategy with the BOM Weather library of having high-low automated testing via CircleCI. This means you test using both:
composer update --prefer-lowest
and
composer update
The first will install the lowest possible versions of your dependencies as specified in your composer.json. The second will install the highest possible versions.
This ensures your version constraints are set correctly and your code should work with any versions in between.
Conclusion
At PreviousNext, we have been using the decoupled model approach on our projects for the last few years, and can certainly say it leads to more robust, clean and testable code. We have had projects migrate from Drupal 7 to Drupal 8 and as the library code does not need to change, the effort has been much less.
If you are heading to Drupal Camp Singapore, make sure to see Eric Goodwin's session on Moving your logic out of Drupal.
Tagged