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

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

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Create a patch Instructions

User interface changes

None.

API changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DuaelFr’s picture

Title: Replace deprecated usage of entity_create for field_config and field_storage_config in File module » Replace deprecated usage of entity_create with a direct call to the entity type class in File module
Issue summary: View changes

Update summary and title according to the change in the meta issue.

chananapeeyush’s picture

Assigned: Unassigned » chananapeeyush

Assigning myself for next one.

chananapeeyush’s picture

Status: Active » Needs review
FileSize
13.79 KB

Replaced 27 occurances.lets see what testbot thinks

DuaelFr’s picture

Status: Needs review » Reviewed & tested by the community

Ok for me

anavarre’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/file/src/Tests/FileFieldTestBase.php
    @@ -48,7 +49,7 @@ function getTestFile($type_name, $size = NULL) {
    +    return File::create((array) $file);
    

    We don't need array(), do we?

  2. +++ b/core/modules/file/src/Tests/SaveUploadTest.php
    @@ -50,7 +51,7 @@ protected function setUp() {
    +    $this->image = File::create((array) current($image_files));
    

    Ditto

  3. +++ b/core/modules/file/src/Tests/SpaceUsedTest.php
    @@ -6,6 +6,7 @@
    +use Drupal\file\Entity\File;
    

    Needs a blank line after the namespace.

  4. +++ b/core/modules/file/src/Tests/ValidatorTest.php
    @@ -6,6 +6,7 @@
    +use Drupal\file\Entity\File;
    

    Ditto

chananapeeyush’s picture

Status: Needs work » Needs review
FileSize
666 bytes
13.79 KB

Addressed #5.3 and #5.4
Regarding #5.1 and #5.2, The File::create function expects array as its argument here so if we are pretty sure the above $file variable will be array then we can remove that and AFAIK about drupal 7 the file functions returned the file object but not sure about drupal 8 so typecasting should be needed here.Rerolled the patch with updated changes.

anavarre’s picture

Status: Needs review » Reviewed & tested by the community

Ah, fair point. We likely did some typecasting for a reason. Nothing to complain about, then. Thank you.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 6: 2491011-6.patch, failed testing.

DuaelFr’s picture

Priority: Major » Normal
DuaelFr’s picture

Assigned: chananapeeyush » Unassigned
Status: Needs work » Postponed

As seen in the meta issue, all the sub-issues has to be postponed and are going to be merged to split the work by entity type and not by module. Thank you for your work, we are going to do our best to preserve your attribution.

DuaelFr’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Postponed » Closed (duplicate)

The meta has been postponed to Drupal 8.1 and this issue is going to be merged on other issues organized by entity type and not by module.