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 now deprecated so we shouldn't use it anymore. When the entity type is known we should directly call <EntityType>::create(). When the entity type is variable, we should use \Drupal::entityManager()->getStorage($entity_type)->create($values);.

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

DuaelFr’s picture

Assigned: DuaelFr » Unassigned
Issue summary: View changes
Status: Active » Needs review
FileSize
53.67 KB

Status: Needs review » Needs work

The last submitted patch, 1: replace_deprecated-2502917-1.patch, failed testing.

Status: Needs work » Needs review
DuaelFr’s picture

Issue tags: +Avoid commit conflicts
DuaelFr’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Needs review » Postponed
Issue tags: -Avoid commit conflicts

As @alexpott said in #2494775-8: Replace deprecated usage of entity_create('action') with a direct call to Action::create(), this issue and it's meta are now postponed to Drupal 8.1.

Mac_Weber’s picture

Status: Postponed » Needs work
Issue tags: +Needs reroll
suhel.rangnekar’s picture

Assigned: Unassigned » suhel.rangnekar

Working

suhel.rangnekar’s picture

Assigned: suhel.rangnekar » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
49.12 KB

Updated the patch against the 8.1.x-dev version.
Please make a review on attached patch

naveenvalecha’s picture

there is already an meta issue for replacing entity_create #2490966: [Meta] Replace deprecated usage of entity_create with a direct call to the entity type class
This may not be the duplicate ?

Status: Needs review » Needs work
Mile23’s picture

Patch in #8 does not apply.

naveenvalecha’s picture

Issue tags: +Novice, +Needs reroll
dimaro’s picture

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

Rerolled #8.
Hope it works.

Status: Needs review » Needs work

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

dimaro’s picture

Console output:
11:12:32 PHP Fatal error: Namespace declaration statement has to be the very first statement in the script in ./core/modules/system/src/Tests/Entity/EntityAutocompleteTest.php on line 8

However I checked the namespace and everything seems to be correct.
Any idea?

DuaelFr’s picture

There are two spaces before the <?php tag in core/modules/system/src/Tests/Entity/EntityAutocompleteTest.php

DuaelFr’s picture

+++ b/core/modules/system/src/Tests/Entity/EntityAutocompleteTest.php
@@ -1,4 +1,4 @@
-<?php
+  <?php

Kaboom!

dimaro’s picture

Status: Needs work » Needs review
FileSize
45.75 KB
362 bytes

Update the patch with this change.
Hope it works.

@DuaelFr Thanks .

Status: Needs review » Needs work

The last submitted patch, 18: replace_deprecated-2502917-18.patch, failed testing.

Mile23’s picture

Thanks for the reroll.

+++ b/core/modules/config/src/Tests/ConfigEntityStaticCacheTest.php
@@ -45,7 +45,10 @@ protected function setUp() {
-    entity_create($this->entityTypeId, array('id' => $this->entityId, 'label' => 'Original label'))->save();
+    \Drupal::entityManager()

For tests you want to use $this->container to explicitly get ahold of the service container supplied by the test class, instead of using \Drupal.

naveenvalecha’s picture

Issue tags: +@deprecated, +Needs reroll
Mile23’s picture

Issue tags: -Needs reroll

The patch still applies, so no reroll is needed.

It just needs some work.

dimaro’s picture

@Mile23 then we should use $this->container instead of \Drupal in all test present in the patch?
If so, maybe we should change the description given in this issue?

naveenvalecha’s picture

@Mile23 then we should use $this->container instead of \Drupal in all test present in the patch?

Heading up, yes exactly.

neerajsingh’s picture

Assigned: Unassigned » neerajsingh
neerajsingh’s picture

Assigned: neerajsingh » Unassigned
Issue tags: +drupalconasia2016

patch at #18 does not be applied.

error: while searching for:
      FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
    );
    // Create the parent entity.
    $entity = entity_create($this->entityType, array('type' => $this->bundle));

    // Create the default target entity.
    $target_entity = entity_create('entity_test_string_id', array('id' => $this->randomString(), 'type' => $this->bundle));

error: patch failed: core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php:199
error: core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php: patch does not apply
naveenvalecha’s picture

The patch needs rework, please do so.

vaibhavsagar’s picture

Assigned: Unassigned » vaibhavsagar
vaibhavsagar’s picture

Assigned: vaibhavsagar » Unassigned

I'm getting at least one test failure (EntityAutocompleteTest) from #18, unassigning.

Mile23’s picture

Issue tags: +Needs reroll

Now it needs a reroll again. :-)

We also need to swap out \Drupal:: and replace it with $this->container within tests.

And the entity.manager service is deprecated, and we want to use entity_type.manager instead.

So:

  1. +++ b/core/modules/config/src/Tests/ConfigEntityStaticCacheTest.php
    @@ -45,7 +45,10 @@ protected function setUp() {
    -    entity_create($this->entityTypeId, array('id' => $this->entityId, 'label' => 'Original label'))->save();
    +    \Drupal::entityManager()
    +      ->getStorage($this->entityTypeId)
    +      ->create(array('id' => $this->entityId, 'label' => 'Original label'))
    +      ->save();
    

    In tests:

    $this->container->get('entity_type.manager')->getStorage($entityType)....

  2. +++ b/core/modules/field/field.module
    @@ -292,7 +292,9 @@ function _field_create_entity_from_ids($ids) {
    -  return entity_create($ids->entity_type, $id_properties);
    +  return \Drupal::entityManager()
    +    ->getStorage($ids->entity_type)
    +    ->create($id_properties);
    

    And in a procedural context:

    /Drupal::entityTypeManager()->getStorage($entity_type)...

In other contexts, you'd use the injected container rather than \Drupal, but that doesn't appear to be a concern in this issue.

Thanks.

dimaro’s picture

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

Rerolled #18 again :)

I will work to fix 1, 2 in #30.

Status: Needs review » Needs work

The last submitted patch, 31: replace_deprecated-2502917-31.patch, failed testing.

dimaro’s picture

Status: Needs work » Needs review
FileSize
47.07 KB
35.99 KB

Patch to fix 1 and 2 in #30.

Status: Needs review » Needs work

The last submitted patch, 33: replace_deprecated-2502917-33.patch, failed testing.

dimaro’s picture

Status: Needs work » Needs review
FileSize
47.59 KB
849 bytes

I think that the error could be the "em" markup, this causes the function "getAutocompleteResult" does not return a result.

I'm right? Or I'm wrong?

leeotzu’s picture

Assigned: Unassigned » leeotzu
leeotzu’s picture

FileSize
7.58 KB

@dimaro, after applying patch #35, traces of entity_create can still be found. Running a quick grep -nr "entity_create(" . should give a quick references of it. Uploading the log file of traces.

leeotzu’s picture

Assigned: leeotzu » Unassigned
Status: Needs review » Needs work
rakesh.gectcr’s picture

Status: Needs work » Needs review
FileSize
69.35 KB
19.57 KB

In the attached patch I am not covered entity_create('config_test'

Status: Needs review » Needs work

The last submitted patch, 39: 2502917-39.patch, failed testing.

dimaro’s picture

Looking in a meta issue we can find the following issues:
#2490966: [Meta] Replace deprecated usage of entity_create with a direct call to the entity type class

Usages of entity_create('config_test'):
#2641518: Replace deprecated usage of entity_create('config_test') with a direct call to ConfigTest::create()

Usages of entity_create('entity_test'):
#2669400: Replace deprecated usages of entity_create('type') with a direct call to <EntityType>::create()

So I think that #37 and #39 could be outside the scope in this issue.
@DuaelFr @Mile23 What do you think about that?

DuaelFr’s picture

I agree.
#35 is RTBC for me in the scope of this issue.

$ rgrep -e "entity_create\\\([^\'\"]" * | wc -l
0

@dimaro can you repost the patch so it's clear for maintainers than yours is the good one?

rakesh.gectcr’s picture

Sorry It was mistaken,

Correct, According to the scope. #35 +1 RTBC

rakesh.gectcr’s picture

Please review #35

rakesh.gectcr’s picture

Status: Needs work » Needs review
dimaro’s picture

Repost the patch #35.
Thanks @DuaelFr for your review.

DuaelFr’s picture

Status: Needs review » Reviewed & tested by the community

Let's get commited! Thanks to everybody for your work.
@leeotzu and @rakesh.gectcr do not hesitate to work on one of the other issues of that meta #2490966: [Meta] Replace deprecated usage of entity_create with a direct call to the entity type class. Each of these issues cover one entity type so you're on the right way :)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 46: replace_deprecated-2502917-46.patch, failed testing.

The last submitted patch, 46: replace_deprecated-2502917-46.patch, failed testing.

dimaro’s picture

Status: Needs work » Needs review

RTBC again?

tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community

Once RTBC, always RTBC ;-) (at least if it's still the same patch...)

catch’s picture

Status: Reviewed & tested by the community » Fixed

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

  • catch committed 0e454b5 on 8.2.x
    Issue #2502917 by dimaro, rakesh.gectcr, DuaelFr, suhel.rangnekar:...

Status: Fixed » Closed (fixed)

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