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 Yes Instructions

User interface changes

None.

API changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Issue summary: View changes
Issue tags: +Novice

The only uses of entity_create() in the core Search module are in tests:

src/Tests/SearchRankingTest.php:    $full_html_format = entity_create('filter_format', array(
src/Tests/SearchCommentTest.php:    $full_html_format = entity_create('filter_format', array(
src/Tests/SearchCommentTest.php:    $basic_html_format = entity_create('filter_format', array(

These are creating FilterFormat entities, so should all be changed to calls to
\Drupal\filter\Entity\FilterFormat::create(array(...))

Should be a good Novice project.

richardbporter’s picture

Assigned: Unassigned » richardbporter
richardbporter’s picture

I gave this a shot. Please let me know if anything needs to be changed. Thanks.

richardbporter’s picture

Status: Active » Needs review
jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Right on the first try, thanks! That is exactly what I believe needed to be done, and the two tests that were changed pass, so that is good. As mentioned in comment #1, I grepped and those 3 spots were the only calls to entity_create() in the search module and its subdirectories. So, this should be good to go.

DuaelFr’s picture

Priority: Major » Normal
xjm’s picture

Status: Reviewed & tested by the community » Postponed

Thanks 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).

jhodgdon’s picture

Hopefully the nice person who made the patch here will get credit on the other issue? (rbp). Thanks!!!

DuaelFr’s picture

Sure. My plan is to report all the names of the persons who worked on these to-be-duplicate issues in the new ones.

jhodgdon’s picture

Status: Postponed » Closed (duplicate)

I'm going to go ahead and close this as Duplicate, since the plan appears to be moving forward on the parent issue to consolidate the patches, and it keeps coming up in my "I'm the search maintainer" issue triage. OK?

DuaelFr’s picture