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

Mile23 created an issue. See original summary.

naveenvalecha’s picture

Issue tags: +Novice
felribeiro’s picture

Status: Active » Needs review
FileSize
11.87 KB
Mile23’s picture

Status: Needs review » Needs work

Thanks for working on this.

+++ b/core/modules/taxonomy/src/Tests/TaxonomyImageTest.php	2016-02-16 20:57:34.367308802 -0200
@@ -50,12 +51,12 @@
-    entity_create('field_config', array(
+    FieldConfig::create([

+++ b/core/modules/taxonomy/src/Tests/TermTranslationFieldViewTest.php	2016-02-16 21:12:08.643327386 -0200
@@ -74,16 +76,16 @@
-    $node = entity_create('node', array(
+    $node = Node::create([

We only want to work on entity type names starting with taxonomy. The other entity types have their own separate issues.

The last submitted patch, 3: 2669926-3.patch, failed testing.

felribeiro’s picture

Status: Needs work » Needs review
FileSize
8.33 KB

Status: Needs review » Needs work

The last submitted patch, 6: 2669926-5.patch, failed testing.

felribeiro’s picture

Status: Needs work » Needs review
FileSize
8.26 KB

Status: Needs review » Needs work

The last submitted patch, 8: 2669926-8.patch, failed testing.

naveenvalecha’s picture

Title: Replace deprecated usage of entity_create('taxonomy*') with a direct call to TaxonomyEntityType::create() » Replace deprecated usage of entity_create('taxonomy*') with a direct call to Vocabulary::create()
Issue tags: +Needs reroll

correcting title.

kostyashupenko’s picture

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

Rerolled #8

naveenvalecha’s picture

we have 1 left in core/scripts/generate-d7-content.sh but we can leave this for now ?
All looks good to me. RTBC +1

+++ b/core/modules/taxonomy/src/Tests/Views/TaxonomyTestBase.php
@@ -125,14 +127,7 @@ protected function mockStandardInstall() {
+   *   (Optional) An associative array of settings to pass to `Term::create()`.

Not sure about this documentation change.

dimaro’s picture

Maybe we should keep the comments in HEAD?
But I'm not sure...

Mile23’s picture

Status: Needs review » Needs work
+++ b/core/modules/taxonomy/src/Tests/Views/TaxonomyTestBase.php
@@ -125,14 +127,7 @@ protected function mockStandardInstall() {
    * Creates and returns a taxonomy term.
    *
    * @param array $settings
-   *   (optional) An array of values to override the following default
-   *   properties of the term:
-   *   - name: A random string.
-   *   - description: A random string.
-   *   - format: First available text format.
-   *   - vid: Vocabulary ID of self::$vocabulary object.
-   *   - langcode: LANGCODE_NOT_SPECIFIED.
-   *   Defaults to an empty array.
+   *   (Optional) An associative array of settings to pass to `Term::create()`.
    *
    * @return \Drupal\taxonomy\Entity\Term

Yah we definitely want to keep the documentation, unless it's wrong, in which case the solution is to make it correct. :-)

naveenvalecha’s picture

Issue tags: +Needs reroll

Thanks for confirming paul

dimaro’s picture

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

Rerolled #8 again and only keep the comments in HEAD.

naveenvalecha’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +@deprecated

Perfect Thanks! taken care #12
An interdiff is really helpful https://www.drupal.org/documentation/git/interdiff

naveenvalecha’s picture

if we really need documentation change, please file another issue.

  • catch committed 5d8ea5f on 8.1.x
    Issue #2669926 by felribeiro, dimaro, kostyashupenko: Replace deprecated...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.1.x, thanks!

AjitS’s picture

Issue tags: -Novice

Removing the 'novice' task.

naveenvalecha’s picture

Issue tags: +Novice

its not worth to remove that tags after issues gets committed.

naveenvalecha’s picture

Status: Fixed » Closed (fixed)

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