Problem/Motivation

The EntityManager is @deprecated, and there are lots of usages of \Drupal::entityManager() in core.

Proposed resolution

As a step to remove usages, but keep things manageable,

-   \Drupal::entityManager()->foo();
+  $this->container->get('the_proper_entity_service.manager')->foo();

just in *Test.php, which is about ~230 usages, but only comprising a handful of patterns.

Remaining tasks

TBD.

User interface changes

None.

API changes

None.

Data model changes

None.

Original Report

Replaced with \Drupal::entityTypeManager() instead of \Drupal::entityManager() in date time.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Nikhilesh Gupta created an issue. See original summary.

Status: Needs review » Needs work

The last submitted patch, datetime.patch, failed testing.

mpdonadio’s picture

Issue summary: View changes
Issue tags: +Needs issue summary update
+++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php
@@ -398,7 +398,7 @@ function testDatelistWidget() {
-    \Drupal::entityManager()->clearCachedFieldDefinitions();
+    \Drupal::entityTypeManager()->clearCachedFieldDefinitions();

That method is on the EntityFieldManager, not the EntityTypeManager.

Does this patch catch all of the uses in datetime? Are there any injected uses of the EntityManager service directly?

This will need a proper Issue Summary; I stubbed out the template.

Nikhilesh Gupta’s picture

Assigned: Nikhilesh Gupta » Unassigned
Issue summary: View changes
mpdonadio’s picture

Priority: Normal » Minor
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update
FileSize
3.67 KB

Patch doesn't apply, so can't add interdiff. Reroll + fixes. Passes locally. Did some searches, and think this covers all uses of the EntityManager service.

jhedstrom’s picture

Status: Needs review » Reviewed & tested by the community

I verified there are no additional uses of the deprecated entity manager in the datetime module once this patch is applied. I think it's good to go.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I think we have issues around scope here... for example,

+++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php
@@ -712,7 +712,7 @@ function testDefaultValue() {
-    \Drupal::entityManager()->clearCachedFieldDefinitions();
+    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();

We could do a find and replace on the entire code base for this.

See https://www.drupal.org/core/scope for guidelines and examples for Drupal core issue scope.

mpdonadio’s picture

Title: Replaced deprecated entityManager() in Date time » Replaced \Drupal::entityManager() in Kernel and Web tests
Component: datetime.module » base system
Issue summary: View changes
Priority: Minor » Normal

Had quick chat with @alexpott in IRC. We want to increase the scope of this. Few things to consider for the new scope.

There are a boatload of \Drupal::entityManager() calls in core.

We can do search/replace like in the above, but in a lot of cases it may not be the best thing to do. For example, this issue should probably have done

-   \Drupal::entityManager()->clearCachedFieldDefinitions();
+  $this->container->get('entity_field.manager')->clearCachedFieldDefinitions();

to eliminate the use of \Drupal global. However, there are a lot of place where the \Drupal::entityManager() is used where either the container isn't available, or dependencies aren't injected.

PhpStorm is telling me that \Drupal::entityManager() is used 230 times in *Test.php. These should all be kernel or web tests, where the $this->container is available. In addition, it looks like in tests there are only a handful of usage patterns, so search/replace is a viable option to get most usages. So, I am proposing to rescope this to replace \Drupal::entityManager() with calls to the appropriate service via the container provided by the test.

alexpott’s picture

Title: Replaced \Drupal::entityManager() in Kernel and Web tests » Replace \Drupal::entityManager() with \Drupal::service('entity_field.manager') or $this->container->get('entity_field.manager')

@mpdonadio I think we should focus on \Drupal::entityManager() with \Drupal::service('entity_field.manager'); (or the best equivalent) is a good scope. We should have an issue per new service.

mpdonadio’s picture

Issue per service sounds like a good plan.

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.

tuutti’s picture

Status: Needs work » Needs review
FileSize
43.4 KB

Search/replaced. Let's see if tests pass.

Status: Needs review » Needs work

The last submitted patch, 12: 2721791-12.patch, failed testing.

tuutti’s picture

tuutti’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 14: 2721791-14.patch, failed testing.

tuutti’s picture

Status: Needs work » Needs review
FileSize
3.77 KB
47.3 KB

Should we use \Drupal::entityManager() on Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem rather than mocking the entityManager->getBaseFieldDefinitions() calls twice on Drupal\Tests\views\Unit\EntityViewsDataTest?

This is happening due to two different services calling getBaseFieldDefinitions() (entity.manager and entity_field.manager) on EntityViewsData and EntityReferenceItem classes.

Seems like fixing the tests "properly" would require a lot refactoring on Drupal\views\EntityViewsData and on all the classes extending it.

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.

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.

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

TR’s picture

Status: Needs review » Needs work

Patch no longer applies.

Berdir’s picture

Status: Needs work » Closed (duplicate)

Forgot about this, this is a duplicate of #3035953: Add @trigger_error() to deprecated EntityManager->EntityFieldManager methods at this point, which is more up to date.