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

naveenvalecha created an issue. See original summary.

naveenvalecha’s picture

Issue tags: +Drupalcon Asia 2016
arunkumark’s picture

Assigned: Unassigned » arunkumark
arunkumark’s picture

Hi,
I have patched to overwrite older entity_create() function by Node::create() on the patch deprecation_entity_create_2672600_6.patch

Thanks & Regards
Arunkumar K

arunkumark’s picture

Status: Active » Needs review
arunkumark’s picture

The last submitted patch, 4: 2672600_deprecation_entity_create.diff, failed testing.

Status: Needs review » Needs work

The last submitted patch, 6: deprecation_entity_create_2672600_6.patch, failed testing.

arunkumark’s picture

Status: Needs work » Needs review
FileSize
19 KB

Status: Needs review » Needs work

The last submitted patch, 9: enity_create_depricate_2672600_7.patch, failed testing.

Mile23’s picture

Thanks, @arunkumark

  1. +++ b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php
    @@ -162,10 +162,10 @@ public function preRender(&$values) {
    +        'type' => $node_type
    
    +++ b/core/modules/comment/src/Tests/CommentBookTest.php
    @@ -38,11 +38,11 @@ protected function setUp() {
    +      'body' => 'Book body'
    
    +++ b/core/modules/editor/src/Tests/EditorFileUsageTest.php
    @@ -107,12 +107,12 @@ public function testEditorEntityHooks() {
    +      'uid' => 1
    
    +++ b/core/modules/field/src/Tests/Number/NumberFieldTest.php
    @@ -436,16 +436,12 @@ function testNumberFormatter() {
    +      $integer_field => ['value' => $random_integer]
    
    +++ b/core/modules/hal/src/Tests/EntityTest.php
    @@ -60,19 +60,19 @@ public function testNode() {
    +        'format' => $this->randomMachineName()
    +      ],
    +      'revision_log' => $this->randomString()
    
    @@ -156,18 +156,18 @@ public function testComment() {
    +        'format' => $this->randomMachineName()
    +      ]]
    
    +++ b/core/modules/system/src/Tests/Entity/EntityCrudHookTest.php
    @@ -158,8 +158,8 @@ public function testCommentHooks() {
    +      'changed' => REQUEST_TIME
    
    @@ -303,8 +303,8 @@ public function testNodeHooks() {
    +      'changed' => REQUEST_TIME
    
    +++ b/core/modules/system/src/Tests/Entity/EntityFieldTest.php
    @@ -674,11 +674,11 @@ public function testEntityConstraintValidation() {
    +      'title' => $this->randomString()
    
    @@ -695,11 +695,11 @@ public function testEntityConstraintValidation() {
    +      'title' => $this->randomString()
    
    +++ b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php
    @@ -84,35 +84,38 @@ public function testNodeHandler() {
    +      'uid' => 1
    ...
    +      'uid' => 1
    ...
    +      'uid' => 1
    
    @@ -350,27 +353,25 @@ public function testCommentHandler() {
    +      'uid' => 1
    ...
    +      'uid' => 1
    
    +++ b/core/modules/taxonomy/src/Tests/TermTranslationFieldViewTest.php
    @@ -74,16 +74,16 @@ public function testTranslatedTaxonomyTermReferenceDisplay() {
    +        'format' => 'basic_html'
    ...
    +      'langcode' => $this->baseLangcode
    

    For multiline arrays, we always leave a trailing comma. That way future patches will affect fewer lines.

  2. +++ b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php
    @@ -84,35 +84,38 @@ public function testNodeHandler() {
    -    foreach ($node_values as $key => $values) {
    -      $node = entity_create('node', $values);
    -      $node->save();
    -      $nodes[$key] = $node;
    -      $node_labels[$key] = Html::escape($node->label());
    -    }
    
    @@ -350,27 +353,25 @@ public function testCommentHandler() {
    -    foreach ($node_values as $key => $values) {
    -      $node = entity_create('node', $values);
    -      $node->save();
    -      $nodes[$key] = $node;
    -    }
    

    Why are these loops being removed? It seems like the test would expect those nodes.

  3. +++ b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php
    @@ -84,35 +84,38 @@ public function testNodeHandler() {
    +    ¶
    
    @@ -350,27 +353,25 @@ public function testCommentHandler() {
    +    ¶
    

    Unnecessary whitespace.

borisson_’s picture

Status: Needs work » Needs review
FileSize
9.06 KB
20.4 KB

#12.2 - the test passes locally, so that shouldn't be a problem. I fixed the comma issues and it looked like a couple of use Drupal\node\Entity\Node; were missing.

Status: Needs review » Needs work

The last submitted patch, 13: replace_deprecated-2672600-13.patch, failed testing.

borisson_’s picture

Status: Needs work » Needs review
FileSize
20.43 KB

Looks like I was behind on 8.1.x, rebased patch.

Status: Needs review » Needs work

The last submitted patch, 15: replace_deprecated-2672600-15.patch, failed testing.

borisson_’s picture

Assigned: arunkumark » Unassigned
Status: Needs work » Needs review
FileSize
5.75 KB
22.16 KB

More imports of code.

Mile23’s picture

Status: Needs review » Needs work
+++ b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php
@@ -84,35 +85,38 @@ public function testNodeHandler() {
-    foreach ($node_values as $key => $values) {

@@ -350,27 +354,25 @@ public function testCommentHandler() {
-    foreach ($node_values as $key => $values) {

Well, again, the scope here is only to remove the usages of entity_create(), not unwrap loops.

Please revert these changes so they leave the loops in.

Thanks.

borisson_’s picture

Status: Needs work » Needs review
FileSize
3.56 KB
19.64 KB

ok.

Mile23’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #19 applies. My IDE says no more occurrences of entity_create('node') exist. The patch only changes entity_create('node') to Node::create.

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

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

catch’s picture

Version: 8.2.x-dev » 8.1.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.2.x and cherry-picked to 8.1.x. Thanks!

  • catch committed 348aa68 on 8.2.x
    Issue #2672600 by borisson_, arunkumark, Mile23: Replace deprecated...

  • catch committed 45a4df9 on 8.1.x
    Issue #2672600 by borisson_, arunkumark, Mile23: Replace deprecated...
catch’s picture

Version: 8.2.x-dev » 8.1.x-dev
Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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