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 in favor of the create() method of the entity type class if known, or the EntityManager::create() helper.
That deprecated functions is used in a lot of different contextes.
Beta phase evaluation
Issue category | Task |
---|---|
Issue priority | Major because we do not want deprecated code in Core as it could be used as example |
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 performances) |
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()
or EntityManager::create()
, according to the case, everywhere its needed.
Before:
entity_create('field_config', $field_values)->save();
$type = 'node';
entity_create($type, $node_values)->save();
After:
use Drupal\field\Entity\FieldConfig;
FieldConfig::create($field_values)->save();
use Drupal\Core\Entity\EntityManager;
$type = 'node';
EntityManager::create($type, $node_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 |
---|---|---|---|
#3 | user-test.png | 16.74 KB | chananapeeyush |
#2 | replace_deprecated_entity_create-2491045-2.patch | 8.33 KB | DuaelFr |
Comments
Comment #1
DuaelFrUpdate summary and title according to the change in the meta issue.
Comment #2
DuaelFrComment #3
chananapeeyush CreditAttribution: chananapeeyush as a volunteer and commentedTested patch locally. Screenshot attached.Setting to RTBC :)
Comment #4
DuaelFrAs seen in #2490966-13: [Meta] Replace deprecated usage of entity_create with a direct call to the entity type class, demoting this issue to Normal.
Comment #5
xjmThanks for your work on this! See #2490966-13: [Meta] Replace deprecated usage of entity_create with a direct call to the entity type class comments 13 through 19. We should postpone these changes on #2346261: Deprecate entity_create() in favor of a <EntityType>::create($values) or \Drupal::entityManager()->getStorage($entity_type)->create($values) and also ideally make one patch per logical scope of change rather than one per module (so eventually marking this as a duplicate and using the patch as part of conceptually scoped ones).
Comment #6
xjmComment #7
DuaelFrPartially merged in #2494775: Replace deprecated usage of entity_create('action') with a direct call to Action::create()
Comment #8
DuaelFrPartly merged in #2503411: Replace deprecated usage of entity_create('comment*') with a direct call to Comment/CommentType::create()