Skip to Main Content
Menu
Close Menu

Updating deprecated entityManager() calls in Drupal 8

Summary

This covers only the most common use cases.

Replace:

\Drupal::entityManager()

-Or-

$this->entityManager()

With:

\Drupal::service('entity_type.manager')

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.

Example

Replace:

\Drupal::entityManager()->getEntityTypeLabels($group)

With:

\Drupal::service('entity_type.repository')->getEntityTypeLabels($group)

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.

Replace:

$this->entityManager()

With:

$this->entityTypeManager()

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.

More examples

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 $this->entityTypeManager().

Improvement opportunities

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.

Deprecation background

The 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.

The 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 EntityManager and EntityTypeManager.

References

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.

Let’s work together.

Have an exceptional idea? Let's talk and see how we can help.
Contact Us