Follow-up to #2490966: [Meta] Replace deprecated usage of entity_create with a direct call to the entity type class

Problem/Motivation

According to #2346261: Deprecate entity_create() in favor of a <EntityType>::create($values) or \Drupal::entityManager()->getStorage($entity_type)->create($values), entity_create() function is going to be deprecated so we shouldn't use it anymore. When the entity type is known we should directly call <EntityType>::create(). What to do when the entity type is not known or is variable is upon discussions.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task
Issue priority Normal because it's just about code cleanup and good practices
Prioritized changes The main goal of this issue is DX, performance and removing code already deprecated for 8.0.0. (Direct calls to EntityType::create are better than generic calls to entity_create for readability)
Disruption This change is not disruptive at all as it only replaces deprecated functions call by their exact equivalent.

Proposed resolution

Replace the deprecated call to entity_create() by a proper call to <EntityType>::create().

Before:

entity_create('file', $field_values)->save();

After:

use Drupal\file\Entity\File;
File::create($field_values)->save();

Remaining tasks

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Create a patch Instructions Done
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

User interface changes

None.

API changes

None.

Comments

Mac_Weber created an issue. See original summary.

mac_weber’s picture

StatusFileSize
new24.77 KB

Status: Needs review » Needs work

The last submitted patch, 2: file-2641588-2.patch, failed testing.

mac_weber’s picture

Status: Needs work » Needs review
StatusFileSize
new23.19 KB

Fixed class aliasing

naveenvalecha’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new21.6 KB

patch does not apply anymore. Rerolled the patch.
Although it does not contain my code and it looks good to go, So RTBC it

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

A few entity_create('file'); are missing - for example in core/modules/editor/src/Tests/EditorFileUsageTest.php

naveenvalecha’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new32.33 KB
new32.33 KB

Replaced all the stuff.
Few more findings that I did

+++ b/core/modules/content_translation/src/Tests/ContentTranslationSyncImageTest.php
@@ -137,7 +138,7 @@ function testImageFieldSync() {
+      $file = File::create($field_values);

Used $this->container->get('entity_type.manager')->getStorage('file')->create($field_values); in Tests b/c we used the static functions before DI was not possible in procedural code.

mile23’s picture

Status: Needs review » Needs work

Looks like the interdiff is a repeat of the patch, and ends in .patch, so I'm reviewing https://www.drupal.org/files/issues/2641588-7.patch

I'm split down the middle on whether to use the entity type service or Entity::create() in these issues. So here's my review:

The patch applies and gets all the occurrences but one remaining in FeedsEnclosure.php.

+++ b/core/modules/file/src/Tests/SaveTest.php
@@ -64,14 +64,14 @@ function testFileSave() {
     );
-    $uppercase_file = File::create($uppercase_values);
+    $uppercase_file = $this->container->get('entity_type.manager')->getStorage('file')->create($uppercase_values);
     file_put_contents($uppercase_file->getFileUri(), 'hello world');
     $violations = $uppercase_file->validate();
     $this->assertEqual(count($violations), 0, 'No violations when adding an URI with an existing filename in upper case.');
     $uppercase_file->save();
 
     // Ensure the database URI uniqueness constraint is triggered.
-    $uppercase_file_duplicate = File::create($uppercase_values);
+    $uppercase_file_duplicate = $this->container->get('entity_type.manager')->getStorage('file')->create($uppercase_values);
     file_put_contents($uppercase_file_duplicate->getFileUri(), 'hello world');

In places like this, if you're explicitly using the service to generate the entity more than once, you should get a reference to the entity storage once, and then call create() on that multiple times.

It's obvious in this place in the patch that this is the solution, since this bit is uninterrupted in the patch. But it might also be like this in other places, too that aren't so obvious.

If we make this change so that we get the service once and then generate multiple entities, *or* if we use Entity::create() instead, I'll RTBC. Just so you know. :-) I think the emphasis is on deprecating entity_load() rather than analyzing the code.

Also: Restarting the test.

naveenvalecha’s picture

Status: Needs work » Needs review

#8, Agreed we should use the static method for consistency over all places. EntityType::create()
As per discussion with about #7 in one of the followup issue, we are now sticking with the static method in tests as well.
hiding #7 patch
Resetting test for #5

The last submitted patch, 5: file-2641588-5.patch, failed testing.

mile23’s picture

Status: Needs review » Needs work

Looks like #5 failed testing. I just tried to patch it and it doesn't apply.

heykarthikwithu’s picture

Assigned: Unassigned » heykarthikwithu

working on this.

heykarthikwithu’s picture

Assigned: heykarthikwithu » Unassigned
Status: Needs work » Needs review
StatusFileSize
new25.19 KB
legovaer’s picture

Status: Needs review » Reviewed & tested by the community

Patch applies, successfully replaces entity_create('file').

RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Needs a re-roll.

naveenvalecha’s picture

Issue tags: +Needs reroll
xaiwant’s picture

Assigned: Unassigned » xaiwant
dimaro’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new25.32 KB

Rerolled #13

mile23’s picture

Status: Needs review » Needs work

Missed one:

FeedsEnclosure.php	
      $file = entity_create('file', [      [position 118:15]	
dimaro’s picture

Status: Needs work » Needs review

@Mile23 I haven't been able to locate the file you indicate and I made a recursive search to replace entity_create('file') and I haven't found any new.

Please could you check again?

naveenvalecha’s picture

Status: Needs review » Reviewed & tested by the community

I checked with my IDE and its fine, there's no occurance left. Thanks!

#19, I think its the feeds module FeedsEnclosure.php http://cgit.drupalcode.org/feeds/tree/src/FeedsEnclosure.php?h=8.x-3.x#n118

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs another re-roll...

dimaro’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new25.32 KB

Rerolled #18.

mile23’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the reroll.

Patch applies, and my IDE says it deals with all occurrences of entity_create('file

RTBC.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.1.x, thanks!

  • catch committed dc111b4 on 8.1.x
    Issue #2641588 by naveenvalecha, Mac_Weber, dimaro, heykarthikwithu:...

Status: Fixed » Closed (fixed)

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