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
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
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.
Comment | File | Size | Author |
---|---|---|---|
#46 | replace_deprecated-2502917-46.patch | 47.59 KB | dimaro |
#35 | interdiff-2502917-33-35.txt | 849 bytes | dimaro |
#35 | replace_deprecated-2502917-35.patch | 47.59 KB | dimaro |
#33 | interdiff-2502917-31-33.txt | 35.99 KB | dimaro |
#33 | replace_deprecated-2502917-33.patch | 47.07 KB | dimaro |
Comments
Comment #1
DuaelFrComment #4
DuaelFrComment #5
DuaelFrAs @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.
Comment #6
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedComment #7
suhel.rangnekar CreditAttribution: suhel.rangnekar as a volunteer and at Faichi Solutions Pvt Ltd commentedWorking
Comment #8
suhel.rangnekar CreditAttribution: suhel.rangnekar as a volunteer and at Faichi Solutions Pvt Ltd commentedUpdated the patch against the 8.1.x-dev version.
Please make a review on attached patch
Comment #9
naveenvalechathere 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 ?
Comment #11
Mile23Patch in #8 does not apply.
Comment #12
naveenvalechaComment #13
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commentedRerolled #8.
Hope it works.
Comment #15
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commentedConsole 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?
Comment #16
DuaelFrThere are two spaces before the
<?php
tag in core/modules/system/src/Tests/Entity/EntityAutocompleteTest.phpComment #17
DuaelFrKaboom!
Comment #18
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commentedUpdate the patch with this change.
Hope it works.
@DuaelFr Thanks .
Comment #20
Mile23Thanks for the reroll.
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
.Comment #21
naveenvalechaComment #22
Mile23The patch still applies, so no reroll is needed.
It just needs some work.
Comment #23
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commented@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?
Comment #24
naveenvalechaHeading up, yes exactly.
Comment #25
neerajsinghComment #26
neerajsinghpatch at #18 does not be applied.
Comment #27
naveenvalechaThe patch needs rework, please do so.
Comment #28
vaibhavsagar CreditAttribution: vaibhavsagar as a volunteer commentedComment #29
vaibhavsagar CreditAttribution: vaibhavsagar as a volunteer commentedI'm getting at least one test failure (EntityAutocompleteTest) from #18, unassigning.
Comment #30
Mile23Now 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 useentity_type.manager
instead.So:
In tests:
$this->container->get('entity_type.manager')->getStorage($entityType)....
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.
Comment #31
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commentedRerolled #18 again :)
I will work to fix 1, 2 in #30.
Comment #33
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commentedPatch to fix 1 and 2 in #30.
Comment #35
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commentedI 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?
Comment #36
leeotzu CreditAttribution: leeotzu as a volunteer and at Srijan | A Material+ Company commentedComment #37
leeotzu CreditAttribution: leeotzu as a volunteer and at Srijan | A Material+ Company commented@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.Comment #38
leeotzu CreditAttribution: leeotzu as a volunteer and at Srijan | A Material+ Company commentedComment #39
rakesh.gectcrIn the attached patch I am not covered
entity_create('config_test'
Comment #41
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commentedLooking 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?
Comment #42
DuaelFrI agree.
#35 is RTBC for me in the scope of this issue.
@dimaro can you repost the patch so it's clear for maintainers than yours is the good one?
Comment #43
rakesh.gectcrSorry It was mistaken,
Correct, According to the scope. #35 +1 RTBC
Comment #44
rakesh.gectcrPlease review #35
Comment #45
rakesh.gectcrComment #46
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commentedRepost the patch #35.
Thanks @DuaelFr for your review.
Comment #47
DuaelFrLet'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 :)
Comment #50
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commentedRTBC again?
Comment #51
tstoecklerOnce RTBC, always RTBC ;-) (at least if it's still the same patch...)
Comment #52
catchCommitted/pushed to 8.2.x and cherry-picked to 8.1.x. Thanks!