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 |
---|---|---|---|
#29 | 2641520-26.patch | 1.94 KB | Mile23 |
Comments
Comment #2
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedComment #3
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedComment #5
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedFixed class aliasing
Comment #6
jibranComment #9
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedI think I can reroll it myself by tomorrow
Comment #10
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedRerolled
Comment #11
XanoThis should use
$this->container->get('entity_type.manager')->getStorage('contact_form')->create()
. Using the static::create()
method on entity classes is for procedural code only, because such code does not support this kind of dependency injection.Same here.
Comment #12
darkdimComment #13
darkdimdone
Comment #14
darkdimComment #16
andypostuseless use...
Comment #17
rakesh.gectcrComment #18
tim.plunkettWhich is it? Why not just use
ContactForm::create
?Comment #19
tim.plunkettIgnore #11, that's wrong. We didn't do that in any of the other ::create conversion issues.
Comment #20
rakesh.gectcr@tim.plunkett, Thanks for the help in IRC.
I have re rolled the patch, Please review it.
Comment #21
rakesh.gectcrWrong Patch attched
Comment #23
rakesh.gectcrComment #25
tim.plunkettPlease restore the space after 'id'
Also the patch fail says it does not apply.
Are you rolling it against 8.1.x?
Comment #26
rakesh.gectcrSorry, The previous one i did against 8.0.x,
The latest patch I rolled against 8.1.x, Please review it
Comment #27
Mile23The patch in #26 applies, and does the job of replacing all occurrences of
entity_load('contact_form
withContactForm::create()
.Setting RTBC.
Comment #28
catchSeeing if I can persuade the bot to test #26.
Comment #29
Mile23Hmm. Don't see it going.
Here's a re-upload of the file. All credit to @rakesh.gectcr #26.
RTBC if it passes. :-)
Comment #30
catchComment #32
catchCommitted/pushed to 8.1.x, thanks!