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

Reference: https://www.drupal.org/core/beta-changes
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

Contributor tasks needed
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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DuaelFr’s picture

Assigned: DuaelFr » Unassigned
Issue summary: View changes
Status: Active » Needs review
FileSize
12.52 KB
DuaelFr’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Needs review » Postponed
Issue tags: -Avoid commit conflicts

As @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.

Mile23’s picture

Title: Replace deprecated usage of entity_create('block*') with a direct call to Block/BlockContent/BlockContentType::create() » Replace deprecated usage of entity_create('block*') with a direct call to BlockEntityType::create()
Status: Postponed » Needs work

Patch in #1 does not apply.

naveenvalecha’s picture

Issue tags: +Novice, +Needs reroll

Adding few appropriate tags

naveenvalecha’s picture

Title: Replace deprecated usage of entity_create('block*') with a direct call to BlockEntityType::create() » Replace deprecated usage of entity_create('block*') with a direct call to Block::create()

correcting title.

lluvigne’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
10.88 KB

Rerolled #1. Hope that everything's ok.

dimaro’s picture

Remove unused namespace.
I checked with my IDE and its fine, there's no occurance left.

RTBC?

DuaelFr’s picture

Status: Needs review » Needs work

Nice work here!

There is no occurence of entity_create('block* remaining.

$ rgrep -e entity_create\([\'\"]block . | wc -l
0

One last little nitpick, though.

+++ b/core/modules/views/src/Tests/Plugin/BlockDependenciesTest.php
@@ -8,6 +8,8 @@
+use Drupal\views\Tests\ViewUnitTestBase;

This is not related to this issue and it seems not used anywhere in this file.

dimaro’s picture

Status: Needs work » Needs review
FileSize
10.77 KB
505 bytes

Update the patch to fix this little nitpick.
Thanks @DuaelFr.

DuaelFr’s picture

If testbot is happy, that's RTBC for me :)

Mile23’s picture

Status: Needs review » Reviewed & tested by the community

Applies, fixes problem, yay.

naveenvalecha’s picture

Issue tags: +@deprecated

RTBC +1, My IDE is happy.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.1.x, thanks!

  • catch committed 252fee2 on 8.1.x
    Issue #2503409 by dimaro, DuaelFr, lluvigne: Replace deprecated usage of...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.