Alter, Override, or Extend: Writing custom code that will stand the test of time

stevector

Drupal as software offers many extension points, perhaps too many. Often the experience of trying to fulfill multiple client requests on a tight timeline is akin to button mashing a video game. If you're uncertain of what to do you can simply move from alter hooks, to trying new modules, to a promising snippet from a drupal.org issue until things are working.

There's a better way. One of the reasons Drupal has so many ways of modifying behavior is that they are meant for different situations and offer different trade-offs. Knowing which technique will best fit in your project can get you to completed work faster.

  • Alter hooks and preprocessing render arrays allow for a small amount of code to make potential major changes in behavior. Unfortunately, the more you use them the harder they are to understand and maintain.

  • Overrides of templates, plugins, or services more clearly separate your code from core or contrib than the alter approach does. But overrides may require review as core and contrib update the thing being overridden.

  • Extending a class or plugin allows your modification to surface to the Drupal's user interface. However this option can feel like the most work in the short term.

This presentation will cover how extensibility in Drupal has evolved over the years. Many modes of extensibility have been added, and even a few have been removed. To illustrate the great differences in techniques available, even for the smallest of changes, this presentation will walk through how the output of a field can be changed to the same end result using template overrides, template extension, preprocessing, altering field formatter info to override a plugin, writing a new formatter plugin, and probably more.

Program tags

architecture, back end development, site building

Experience Level

Intermediate

Drupal Version