Change record status: 
Project: 
Introduced in version: 
11.5.0
Description: 

Developers can now use DeprecatedServicePropertyTrait to deprecate injected service properties on their own release schedule.

The trait lets you remove the property while keeping the service accessible to subclasses. When a subclass reads the removed property, the trait returns the service, triggering a deprecation notice.

Use the trait and declare $deprecatedProperties as an array keyed by the original property names. For each property, specify

  • service_id,
  • deprecation_version,
  • removal_version, and optionally
  • change_record

Keep the exact service ID or alias previously used for the property. For example, DirectoryWithMetadataDiscovery::$fileSystem used FileSystemInterface::class. Keeping that alias preserves the service returned on sites that redirect it.

Prefix both versions with the Drupal.org project name.

For example, Drupal Core declares:

use Drupal\Core\File\FileSystemInterface;

protected array $deprecatedProperties = [
  'fileSystem' => [
    'service_id' => FileSystemInterface::class,
    'deprecation_version' => 'drupal:11.3.0',
    'removal_version' => 'drupal:13.0.0',
    'change_record' => 'https://www.drupal.org/node/3530869',
  ],
];

For PHPStan, add @phpstan-import-type DeprecatedServiceProperty from DeprecatedServicePropertyTrait to the class PHPDoc and @var array<string, DeprecatedServiceProperty> to the property PHPDoc.

BC notice

String definitions of $deprecatedProperties are themselves deprecated in Drupal 11.5.0 and will be removed in Drupal 13.0.0.

To migrate, use the existing string as the value of service_id and add the deprecation and removal versions for the property.

Until then, Drupal still warns that the property is deprecated, albeit without the additional information.

Drupal will however also warn that the string format is deprecated. Migrating to an array removes this second warning. The property notice then includes the deprecation and removal versions you specified.

Impacts: 
Module developers
Themers
Site templates, recipes and distribution developers