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
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 |
---|---|---|---|
#19 | replace_deprecated-2672600-19.patch | 19.64 KB | borisson_ |
Comments
Comment #2
naveenvalechaComment #3
arunkumarkComment #4
arunkumarkHi,
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
Comment #5
arunkumarkComment #6
arunkumarkComment #9
arunkumarkComment #11
Mile23Comment #12
Mile23Thanks, @arunkumark
For multiline arrays, we always leave a trailing comma. That way future patches will affect fewer lines.
Why are these loops being removed? It seems like the test would expect those nodes.
Unnecessary whitespace.
Comment #13
borisson_#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.Comment #15
borisson_Looks like I was behind on 8.1.x, rebased patch.
Comment #17
borisson_More imports of code.
Comment #18
Mile23Well, 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.
Comment #19
borisson_ok.
Comment #20
Mile23Patch in #19 applies. My IDE says no more occurrences of entity_create('node') exist. The patch only changes entity_create('node') to Node::create.
Comment #22
catchCommitted/pushed to 8.2.x and cherry-picked to 8.1.x. Thanks!
Comment #25
catch