Drupal 10.1.3 Introduces New Utility Class to Simplify Backward-Compatible Deprecation Fixes for Modules
Drupal contributors and maintainers preparing their modules for Drupal 11 compatibility have faced the challenge of maintaining support for Drupal 9.5, which remained in security support until November 1, 2023. The article, "Writing backward-compatible deprecation fixes for contributed modules will be much easier for Drupal 11," credited to Matt Glaman, addresses this challenge in detail. With deprecated code removed in Drupal 10, maintainers had to find workarounds and employ if/else statements with version_compare. Drupal 10.1.3 has introduced a new utility class, \Drupal\Component\Utility\DeprecationHelper, and its method, backwardsCompatibleCall, to make supporting multiple Drupal core versions more straightforward while addressing deprecated code.
This utility class allows developers to execute code conditionally based on the current Drupal core version, simplifying the process of making backwards-compatible calls. For example, the deprecated user_roles() function in Drupal 10.2.0 can be replaced using DeprecationHelper::backwardsCompatibleCall, ensuring compatibility with newer and older versions of Drupal core. Modules using DeprecationHelper must support a minimum version of 10.1.3 for Drupal core, marking a shift towards enhanced automation and reduced manual upkeep for Drupal sites.
Drupal developer Matt Glaman acknowledges the importance of discussions and refinements made to this tool, emphasizing that it's essential for a tool of this nature. While it may involve only a few lines of code, nailing the developer experience is crucial. Furthermore, introducing backward compatibility to automated code fixes provided through Rector, an initiative led by Björn Brala, signifies a significant leap forward in innovation for the Drupal community. This effort aims to minimize manual maintenance and enhance Drupal site management as the community prepares for Drupal 11.
Read more about the content here.