Change record status: 
Project: 
Introduced in branch: 
10.2.x
Introduced in version: 
10.1.3
Description: 

The contributed module ecosystem has had to craft ways to provide backward compatibility between minor and major versions of Drupal core.

  • Minor versions: silence runtime deprecations triggered while supporting the current major version's minor versions with security coverage.
  • Major versions: prevent calling deprecated code that was removed while bridging support with the latest minor version of the last major version (9.5 && 10.0.)

It often looks like this:

if (version_compare(\Drupal::VERSION, '9.4', '>=')) {
  // Do the new way introduced in 9.4, and supported in 9.5 and 10.0.
}
else {
  // @phpstan-ignore-next-line
  // Call the deprecated code for 9.2 and 9.3.
}

This is painful to copy around. By adding a new Drupal\Component\Utility\DeprecationHelper utility class, any module can now call the correct code based on the provided version.

$result = DeprecationHelper::backwardsCompatibleCall(
  currentVersion: \Drupal::VERSION,
  deprecatedVersion: '10.3',
  currentCallable: fn() => Role::loadMultiple(),
  deprecatedCallable: fn() => user_roles(),
);

PHPStan's deprecation rule package can now define custom deprecation scopes (see https://github.com/phpstan/phpstan-deprecation-rules/pull/99.) The phpstan-drupal package can then consider this as a deprecated scope, removing the need for @phpstan-ignore-next-line throughout backward compatibility layer code. This will be available soon as we work towards Drupal 11 readiness.

Drupal rector support was merged into main in september 2023.

Impacts: 
Module developers
Site templates, recipes and distribution developers

Comments

johnv’s picture

When installing a module (that implements above pattern) on an installation with older Drupal version (8, 9, 10, 11), (how) will this DeprecationHelper be installed, called?

avpaderno’s picture

DeprecationHelper does not exist before Drupal 10.2.x; it cannot be used with Drupal 9.x nor Drupal 8.x.