Updating deprecated entityManager() calls in Drupal 8
This covers only the most common use cases.
A little more information
If you are using less common methods
EntityManager was split into 11 new services. Based on which methods you were calling on the service, you will need to load a different service.
See https://www.drupal.org/node/2549139 for the list of methods supported by each service.
If you are replacing usage in a controller class that extends ControllerBase
ControllerBase now injects
EntityTypeManager. If you are using methods used with that new service, you can do a simple replacement.
More complex updates
If you would like to use dependency injection and are not using methods available in
EntityTypeManager within a controller that extends
ControllerBase, you will need to inject the service yourself.
Unfortunately, dependency injection patterns for services, controllers, and plugins (example: block plugin) are different. See https://github.com/damontgomery/d8_examples/tree/develop/d8e_service_dependency_injection for some examples of how dependency injection looks for these.
We have a set of examples as part of the Drupal Rector
rector_examples module. See https://github.com/palantirnet/drupal-rector/tree/master/rector_examples.
Automating updates with Drupal Rector
Drupal Rector is an open source tool built using Rector which can help automate this deprecation as well as other common deprecations.
For more information, see https://github.com/palantirnet/drupal-rector.
What is covered by the rector
Drupal Rector currently replaces static calls to
\Drupal::entityManager() as shown above.
If a class extends the
ControllerBase class and is using the
EntityTypeManager service, the dependency injection pattern will be used. This looks like
Drupal Rector does not inject services into classes that don’t already have the services injected by extending ControllerBase.
Drupal Rector does not handle the complex use case when a service other than
EntityTypeManager is needed and the method does not directly call the service. This most often occurs when a variable is used to store the service and then called on separate statements. It’s possible that several services would need to be called going forward.
EntityManager service was originally narrow in scope and used to discover entity types. This functionality grew and it was decided to split the service into 11 services. To support backwards compatibility, the
EntityManager service was left in place, but serves as a wrapper around those new services.
EntityManager was deprecated in 8.0.0, but was used heavily in Drupal 8. A lot of this use may have been because of its availability as part of controllers that extended
ControllerBase and also because of the similarity in naming between
The Drupal change record for this deprecation is available at https://www.drupal.org/node/2549139.
Credits and thank yous
Thank you to Dan Montgomery for leading Drupal Rector development to support this deprecation.
Thank you to Palantir.net for sponsoring this development.