When Drupal 8.1.0 was released it included the addition of a logger service in
ControllerBase that was not present in Drupal 8.0.0. Which is great because it means less boilerplate code to write when you want to log things from your custom controllers. However, it also means making updates to existing code in some cases. Like for example, the code examples in our Get a Service from the Container tutorial in the Drupal 8 Module Development Essentials series.
Our commitment to on-going reviews
We're committed to keeping the Drupal 8 content on our site up-to-date even as Drupal 8 changes over time. And, with the switch to semantic versioning and a roughly 6-month release cycle for new minor versions of Drupal 8, changes are bound to happen. Best practices change, APIs improve, and, as we've seen in this case, Drupal core continues to iterate by making common tasks easier.
In our opinion these changes are a good thing. However, they can also be really frustrating to anyone trying to learn a new skill. When it doesn't work you probably ask yourself, why is this not working? Is it my code? What am I missing? And then spend a long and frustrating amount of time trying to figure out which step you missed. Only to realize you didn't miss any. Yeah, we hate it when that happens, too.
So we updated the tutorial. Or rather, we passed the information along to our friends over at KnpUniversity who created the original video and let them know it needed an update, and they updated it. Thanks guys, you rock! And now we can all keep learning more about module development instead of spinning the wheels trying to figure out where we got off track.
So what changed?
The short version is: in this tutorial Ryan uses dependency injection and the service container to insert a copy of the
$loggerFactory into his
DinoRoar controller. As of Drupal 8.1.x the
ControllerBase class that is being extended already has a
$loggerFactory property. Adding the logger was such a common use case that instead of having this boilerplate code for many controllers it has now been added to the
ControllerBase class and you can make use of
$this->logger in your code without having to inject it first.
In the previous version of the code the
DinoRoar class had a
private $loggerFactory property that conflicts with the
protected $loggerFactory that's already been added to the parent
ControllerBase class. Their signatures are incompatible, and PHP doesn't like that.
Know of any other issues related to Drupal 8 updates in our tutorials? Let us know so we can make sure they get added to our to-do list and updated. And, rest assured that we've got our sights on the forthcoming 8.2.x and how it will effect the materials already on our site.