Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Berdir on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.7.x
Introduced in version:
8.7.0
Issue links:
Description:
The trait simplifies deprecating properties on classes.
Usage
Situation: the deprecated entity.manager service is replaced in a class, to avoid breaking subclasses that might still use it, the trait can be used:
- Remove the
$entityManager
property, add the replacements instead - Change the constructor, in case of entity manager, the entity_type.manager is a backwards compatible replacement as the entity manager implements EntityTypeManager as well.
- Add the
DeprecatedServicePropertyTrait
- Define the removed properties and their service ID in the
$deprecatedProperties
property
The result then looks like this:
class EntityConverter implements ParamConverterInterface {
use DeprecatedServicePropertyTrait;
/**
* {@inheritdoc}
*/
protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
/**
* Entity type manager which performs the upcasting in the end.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a new EntityConverter.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Language\LanguageManagerInterface|null $language_manager
* (optional) The language manager. Defaults to none.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager = NULL, EntityRepositoryInterface $entity_repository = NULL) {
if ($entity_type_manager instanceof EntityManagerInterface) {
@trigger_error('Passing the entity.manager service to EntityConverter::__construct() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Pass the entity_type.manager service instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
}
$this->entityTypeManager = $entity_type_manager;
if ($entity_repository) {
$this->entityRepository = $entity_repository;
}
else {
@trigger_error('The entity.repository service must be passed to EntityConverter::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$this->entityRepository = \Drupal::service('entity.repository');
}
$this->languageManager = $language_manager;
}
Impacts:
Module developers