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 |
---|---|---|---|
#14 | 2641584-14.patch | 113.61 KB | heykarthikwithu |
#11 | field_config-2641584-11.patch | 101.36 KB | legovaer |
#7 | field_config-2641584-7.patch | 102.31 KB | Mac_Weber |
#4 | field_config-2641584-4.patch | 102.3 KB | Mac_Weber |
Comments
Comment #2
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedComment #4
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedFixed class aliasing
Comment #5
swentel CreditAttribution: swentel commentedDoesn't apply anymore (probably after #2604220: PHP notice for single value image field configured with a default image (no image present) and a hidden label), should be easy to fix.
Comment #6
swentel CreditAttribution: swentel commentedComment #7
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedComment #8
XanoTests must use
$this->container->get('entity_type.manager')->getStorage('field_config')->create()
. This type of dependency injection is not possible in procedural code, so that's why we useFieldConfig::create()
there.Comment #9
chx CreditAttribution: chx at Smartsheet commentedErm, nope. That's not what the @deprecated section of entity_create() says. If you disagree with that documentation and the decision behind it , bring it up with the entity maintainers but not in this issue or family of issues.
Comment #10
naveenvalechaThis patch needs reroll due to conflicts with #2641596: Replace deprecated usage of entity_create('node') with a direct call to Node::create()
Comment #11
legovaerComment #13
heykarthikwithuComment #14
heykarthikwithuAs per #10.
Comment #15
heykarthikwithuComment #16
legovaerPatch applies, successfully replaces entity_create('field_config').
RTBC.
Comment #17
catchCommitted/pushed to 8.1.x, thanks!