Problem/Motivation

Per the docs, the entityManager() is being deprecated and will be replaced with the entityTypeManager() instead.

in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager() instead in most cases. If the needed method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the deprecated \Drupal\Core\Entity\EntityManager to find the correct interface or service.

Drupal\Core\Controller\ControllerBase class provides now deprecated entityManager() method that is used in controllers across all core modules.

Proposed Solution

Replace calls to entityManager() with entityTypeManager(), or other appropriate service.

Remaining tasks

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Create a patch Instructions
Update the issue summary Instructions
Update the issue summary noting if allowed during the beta Instructions
Manually test the patch Novice Instructions
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards Instructions

API changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

thhafner created an issue. See original summary.

dawehner’s picture

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

20th’s picture

Title: Replace deprecated entityManager() with entityTypeManager(). » Replace deprecated entityManager() in ControllerBase descendents
Issue summary: View changes

@dawehner
You are right, the patch in #2624770: Use more specific entity.manager services in core.services.yml already updates a lot of deprecated code, but it is focused specifically on services definitions and constructors and it is already enormous… And at the moment of this writing, needs a very big rebase.

On the contrary, this issue can focus specifically on usages of deprecated entityManager() method in controller classes, which is very common across all modules in core.

dawehner’s picture

On the contrary, this issue can focus specifically on usages of deprecated entityManager() method in controller classes, which is very common across all modules in core.

Thank you for adding a good scope for this issue. Good point! Let's focus on this one thing here. This makes it easy to review and by that actually its doable to get in. Sorry for my previous comment.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Mile23’s picture

Status: Active » Needs review
FileSize
42.39 KB

I think that's all of them...

I added a follow-up #2914956: Remove usages of EntityManager from EntityViewController because NodeController uses NodeViewController to render some output.

Status: Needs review » Needs work

The last submitted patch, 9: 2691675_9.patch, failed testing. View results

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Berdir’s picture

Status: Needs work » Needs review
FileSize
49.31 KB
10.11 KB

Reroll, fixing the quickedit controller and also converting one more example from book.module.

I think it makes sense to deal with Node/EntityViewController in here too, it's easy to update and backwards compatible for sub-classes (once we add the new trait, which I'll do in the next update). Then we can add a @trigger_error() to ControllerBase::getEntityManager()

Status: Needs review » Needs work

The last submitted patch, 13: 2691675_13.patch, failed testing. View results

Berdir’s picture

Status: Needs work » Needs review
FileSize
49.66 KB
2.22 KB

Fixed that.

Status: Needs review » Needs work

The last submitted patch, 15: 2691675_15.patch, failed testing. View results

Berdir’s picture

Status: Needs work » Needs review
FileSize
49.7 KB
1.27 KB

Actually fixing the quickedit controller.

Berdir’s picture

Status: Needs review » Needs work

The last submitted patch, 18: 2691675_18.patch, failed testing. View results

Berdir’s picture

Status: Needs work » Needs review
FileSize
53.15 KB
940 bytes

Added missing constructor argument.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me

Berdir’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/node/src/Controller/NodePreviewController.php
@@ -35,7 +35,7 @@ public function view(EntityInterface $node_preview, $view_mode_id = 'full', $lan
    */
   public function title(EntityInterface $node_preview) {
-    return $this->entityManager->getTranslationFromContext($node_preview)->label();
+    return $this->entityTypeManager->getTranslationFromContext($node_preview)->label();
   }
 

this is wrong. apparently this method is not actually called in core.

Berdir’s picture

Status: Needs work » Needs review
FileSize
55.09 KB
2.6 KB

this should fix that.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

Good catch
Can you open an issue for removing it/finding out what title callback is used on that route?

Berdir’s picture

The title callback does point to that method, but that specific route sets a #title (through EntityViewBuilder); so going to that URL does not have to call the title callback. And nothing references that route in a way that requires us to resolve the title through the callback (e.g. breadcrumb), not in core anyway, so it's never called.

It possibly was called at some point, but then EntityViewController was refactored to set the #title through the render-trickery, so that quickedit works. Note that it actually can work on the preview page :)

So, not quite sure what to do about that..

Berdir’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/comment/src/Controller/CommentController.php
@@ -53,13 +62,31 @@ class CommentController extends ControllerBase {
+    if ($entity_field_manager) {
+      $this->entityFieldManager = $entity_field_manager;
+    }
+    else {
+      @trigger_error('The entity_field.manager service must be passed to CommentController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
+      $this->entityFieldManager = \Drupal::service('entity_field.manager');
+    }
+    if ($entity_repository) {
+      $this->entityRepository = $entity_repository;
+    }
+    else {
+      @trigger_error('The entity.repository service must be passed to CommentController::__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');
+    }

+++ b/core/modules/content_translation/src/Controller/ContentTranslationController.php
@@ -24,21 +25,40 @@ class ContentTranslationController extends ControllerBase {
+    if ($entity_field_manager) {
+      $this->entityFieldManager = $entity_field_manager;
+    }
+    else {
+      @trigger_error('The entity_field.manager service must be passed to ContentTranslationController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
+      $this->entityFieldManager = \Drupal::service('entity_field.manager');
+    }

+++ b/core/modules/image/src/Controller/QuickEditImageController.php
@@ -50,11 +58,20 @@ class QuickEditImageController extends ControllerBase {
+    if (!$entity_display_repository) {
+      @trigger_error('The entity_display.repository service must be passed to QuickEditImageController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
+      $this->entityDisplayRepository = \Drupal::service('entity_display.repository');
+    }
+    else {
+      $this->entityDisplayRepository = $entity_display_repository;
+    }

+++ b/core/modules/node/src/Controller/NodeController.php
@@ -39,10 +47,19 @@ class NodeController extends ControllerBase implements ContainerInjectionInterfa
+    if ($entity_repository) {
+      $this->entityRepository = $entity_repository;
+    }
+    else {
+      @trigger_error('The entity.repository service must be passed to NodeController::__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');
+    }

+++ b/core/modules/node/src/Controller/NodePreviewController.php
@@ -2,14 +2,57 @@
+    if ($entity_repository) {
+      $this->entityRepository = $entity_repository;
+    }
+    else {
+      @trigger_error('The entity.repository service must be passed to NodePreviewController::__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');
+    }

+++ b/core/modules/quickedit/src/QuickEditController.php
@@ -61,12 +77,30 @@ class QuickEditController extends ControllerBase {
+    if (!$entity_display_repository) {
+      @trigger_error('The entity_display.repository service must be passed to QuickEditController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
+      $this->entityDisplayRepository = \Drupal::service('entity_display.repository');
+    }
+    else {
+      $this->entityDisplayRepository = $entity_display_repository;
+    }
+    if ($entity_repository) {
+      $this->entityRepository = $entity_repository;
+    }
+    else {
+      @trigger_error('The entity.repository service must be passed to QuickEditController::__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');
+    }

I would turn these around to be

if (!$service) {
  $service = \Drupal::service('blah')
}
$this->service = $service;

Because then in the future when we remove the deprecation we have less change to make and less code moves... i.e. we can remove the entire if and the = NULL from the constructor and we are done.

There might be a compelling reason to do it the way round @Berdir has - if there is I'm +1 for re-rtbc :)

Berdir’s picture

I don't have a reason, I think I copied that pattern in #2624770: Use more specific entity.manager services in core.services.yml from somewhere. Will have to update a lot of cases for that, but there are worse things ;)

Berdir’s picture

Updated that, also realized I missed one controller when adding the BC changes, changed that now to just use entityTypeManager() like it used entityManager() before.

alexpott’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the quick fixes. Marking rtbc as the changes are minimal. I've review all the service names in the deprecated code paths and they look good.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed c23c05c and pushed to 8.7.x. Thanks!

alexpott’s picture

Issue tags: -API change

  • alexpott committed c23c05c on 8.7.x
    Issue #2691675 by Berdir, Mile23, alexpott: Replace deprecated...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.