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('field_config', $field_values)->save();

After:

use Drupal\field\Entity\FieldConfig;
FieldConfig::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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Mac_Weber created an issue. See original summary.

Mac_Weber’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 2: contact_form-2641520-2.patch, failed testing.

Mac_Weber’s picture

Status: Needs work » Needs review
FileSize
2.29 KB

Fixed class aliasing

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 5: contact_form-2641520-5.patch, failed testing.

The last submitted patch, 5: contact_form-2641520-5.patch, failed testing.

Mac_Weber’s picture

Issue tags: ++Needs reroll

I think I can reroll it myself by tomorrow

Mac_Weber’s picture

Status: Needs work » Needs review
Issue tags: -+Needs reroll
FileSize
2.29 KB

Rerolled

Xano’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/config_translation/src/Tests/ConfigTranslationListUiTest.php
    @@ -211,7 +212,7 @@ public function doCustomContentTypeListTest() {
    +    $contact_form = ContactForm::create(array(
    

    This should use $this->container->get('entity_type.manager')->getStorage('contact_form')->create(). Using the static ::create() method on entity classes is for procedural code only, because such code does not support this kind of dependency injection.

  2. +++ b/core/modules/contact/src/Tests/Views/ContactFieldsTest.php
    @@ -40,7 +41,7 @@ protected function setUp() {
    +    ContactForm::create(array(
    

    Same here.

darkdim’s picture

Assigned: Unassigned » darkdim
Issue tags: +CodeSprintUA
darkdim’s picture

darkdim’s picture

Assigned: darkdim » Unassigned
Status: Needs work » Needs review
Issue tags: +SprintWeekend2016

Status: Needs review » Needs work
andypost’s picture

+++ b/core/modules/config_translation/src/Tests/ConfigTranslationListUiTest.php
@@ -11,6 +11,7 @@
+use Drupal\contact\Entity\ContactForm;

+++ b/core/modules/contact/src/Tests/Views/ContactFieldsTest.php
@@ -8,6 +8,7 @@
+use Drupal\contact\Entity\ContactForm;

useless use...

rakesh.gectcr’s picture

Assigned: Unassigned » rakesh.gectcr
tim.plunkett’s picture

+++ b/core/modules/config_translation/src/Tests/ConfigTranslationListUiTest.php
@@ -11,6 +11,7 @@
+use Drupal\contact\Entity\ContactForm;

@@ -210,7 +211,7 @@ public function doCustomContentTypeListTest() {
-    $contact_form = entity_create('contact_form', array(
+    $contact_form = $this->container->get('entity_type.manager')->getStorage('contact_form')->create(array(

Which is it? Why not just use ContactForm::create?

tim.plunkett’s picture

Ignore #11, that's wrong. We didn't do that in any of the other ::create conversion issues.

rakesh.gectcr’s picture

@tim.plunkett, Thanks for the help in IRC.

I have re rolled the patch, Please review it.

rakesh.gectcr’s picture

Status: Needs review » Needs work

Wrong Patch attched

The last submitted patch, 20: 2599454-19.patch, failed testing.

rakesh.gectcr’s picture

Status: Needs work » Needs review
FileSize
2 KB

Status: Needs review » Needs work

The last submitted patch, 23: 2641520-19.patch, failed testing.

tim.plunkett’s picture

+++ b/core/modules/config_translation/src/Tests/ConfigTranslationListUiTest.php
@@ -210,10 +211,10 @@ public function doCustomContentTypeListTest() {
-      'id' => Unicode::strtolower($this->randomMachineName(16)),
...
+      'id'=> Unicode::strtolower($this->randomMachineName(16)),

Please restore the space after 'id'

Also the patch fail says it does not apply.
Are you rolling it against 8.1.x?

rakesh.gectcr’s picture

Status: Needs work » Needs review
FileSize
1.94 KB

Sorry, The previous one i did against 8.0.x,

The latest patch I rolled against 8.1.x, Please review it

Mile23’s picture

Status: Needs review » Reviewed & tested by the community

The patch in #26 applies, and does the job of replacing all occurrences of entity_load('contact_form with ContactForm::create().

Setting RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs review

Seeing if I can persuade the bot to test #26.

Mile23’s picture

Hmm. Don't see it going.

Here's a re-upload of the file. All credit to @rakesh.gectcr #26.

RTBC if it passes. :-)

catch’s picture

Status: Needs review » Reviewed & tested by the community

  • catch committed 3f6c974 on 8.1.x
    Issue #2641520 by Mac_Weber, rakesh.gectcr, darkdim: Replace deprecated...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.1.x, thanks!

Status: Fixed » Closed (fixed)

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