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();

entity_create($entity_type, $values)->save();

After:

use Drupal\field\Entity\FieldConfig;
FieldConfig::create($field_values)->save();

\Drupal::entityManager()->getStorage($entity_type)->create($values)->save();

Remaining tasks

Entity type Sub-issue
$variable #2502917: Replace deprecated usage of entity_create used with variable entity types with a direct call to EntityManager::getStorage()->create()
action #2494775: Replace deprecated usage of entity_create('action') with a direct call to Action::create()
aggregator_feed & aggregator_item #2503403: Replace deprecated usage of entity_create('aggregator_*') with a direct call to Feed/Item::create()
base_field_override none found
block & block_content & block_content_type #2503409: Replace deprecated usage of entity_create('block*') with a direct call to Block::create()
comment & comment_type #2503411: Replace deprecated usage of entity_create('comment*') with a direct call to Comment/CommentType::create()
config_query_test #2641506: Replace deprecated usage of entity_create('config_query_test') with a direct call to ConfigQueryTest::create()
config_test #2641518: Replace deprecated usage of entity_create('config_test') with a direct call to ConfigTest::create()
contact_form #2641520: Replace deprecated usage of entity_create('contact_form') with a direct call to ContactForm::create()
date_format #2641522: Replace deprecated usage of entity_create('date_format') with a direct call to DateFormat::create()
editor #2641528: Replace deprecated usage of entity_create('editor') with a direct call to Editor::create()
entity_form_display #2641532: Replace deprecated usage of entity_create('entity_form_display') with a direct call to EntityFormDisplay::create()
entity_test #2669400: Replace deprecated usages of entity_create('type') with a direct call to <EntityType>::create()
entity_test_default_access none found
entity_test_label #2641534: Replace deprecated usage of entity_create('entity_test_label') with a direct call to EntityTestLabel::create()
entity_test_mulrev #2641540: Replace deprecated usage of entity_create('entity_test_mulrev') with a direct call to EntityTestMulRev::create()
entity_test_mul_changed none found
entity_test_mulrev_changed none found
entity_test_rev #2641544: Replace deprecated usage of entity_create('entity_test_rev') with a direct call to EntityTestRev::create()
entity_test_string_id #2641548: Replace deprecated usage of entity_create('entity_test_string_id') with a direct call to EntityTestStringId::create()
entity_view_display #2641562: Replace deprecated usage of entity_create('entity_view_display') with a direct call to EntityViewDisplay::create()
field_config #2641584: Replace deprecated usage of entity_create('field_config') with a direct call to FieldConfig::create()
field_storage_config #2641586: Replace deprecated usage of entity_create('field_storage_config') with a direct call to FieldStorageConfig::create()
file #2641588: Replace deprecated usage of entity_create('file') with a direct call to File::create()
filter_format #2503379: Replace deprecated usage of entity_create('filter_format') with a direct call to FilterFormat::create()
image_style #2641590: Replace deprecated usage of entity_create('image_style') with a direct call to ImageStyle::create()
menu #2641592: Replace deprecated usage of entity_create('menu') with a direct call to Menu::create()
menu_link_content #2641696: Replace deprecated usage of entity_create('menu_link_content') with a direct call to MenuLinkContent::create()
node #2641596: Replace deprecated usage of entity_create('node') with a direct call to Node::create()
node_type #2641604: Replace deprecated usage of entity_create('node_type') with a direct call to NodeType::create()
rdf_mapping #2641608: Replace deprecated usage of entity_create('rdf_mapping') with a direct call to RdfMapping::create()
responsive_image_style #2641610: Replace deprecated usage of entity_create('responsive_image_style') with a direct call to ResponsiveImageStyle::create()
search_page #2641612: Replace deprecated usage of entity_create('search_page') with a direct call to SearchPage::create()
shortcut #2641616: Replace deprecated usage of entity_create('shortcut') with a direct call to Shortcut::create()
shortcut_set #2641614: Replace deprecated usage of entity_create('shortcut_set') with a direct call to EntityShortcutSet::create()
taxonomy_term #2669926: Replace deprecated usage of entity_create('taxonomy*') with a direct call to Vocabulary::create()
taxonomy_vocabulary #2669926: Replace deprecated usage of entity_create('taxonomy*') with a direct call to Vocabulary::create()
tour #2641620: Replace deprecated usage of entity_create('tour') with a direct call to Tour::create()
user #2641624: Replace deprecated usage of entity_create('user') with a direct call to User::create()
user_role #2641692: Replace deprecated usage of entity_create('user_role') with a direct call to UserRole::create()
view #2641626: Replace deprecated usage of entity_create('view') with a direct call to View::create()
other occurrences of entity_create #2928930: Properly deprecate entity_create() and remove all occurrences
documentation references to entity_create #2932317: Replace stray documentation references to entity_create()

User interface changes

None.

API changes

None.

CommentFileSizeAuthor
#24 fix_all_the_things_mage.jpeg45.62 KBDuaelFr
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

DuaelFr’s picture

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

Major change : enlarge the subject of this meta to all the entity_create that refers to a known entity type.
Todo : update all sub-issues and open new ones if needed.

DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes

All existing sub-issues has been updated.

Berdir’s picture

Major because we do not want deprecated code in Core as it could be used as example

Not convinced. This resulted in a *huge* amount of major issues. it would be more than enough to just have this meta as major and even that I think is wrong.

From the priority documentation:

replacing code that works with code that is the current best practice, or

That reads kinda weird IMHO, but I think this about replacing deprecated code ( a clarification there would be nice). This is listed under *normal* task.

It might not look like it, but many of us are really trying to not just get rid of the criticals, but also keep the majors at bay (I guess we gave up on the idea of actually getting them significantly down). Seeing a +30 on https://drupalreleasedate.com/ is demotivating for those, also for all the people that spent hours and hours triaging critical issues at the sprints in LA.

webchick’s picture

Priority: Major » Normal
Status: Active » Postponed

So first off, I'm generally in favour of getting rid of entity_create(). That's a very confusing anti-pattern to use everywhere when so much of Drupal is OO. However, I'm a bit worried about forging ahead on these sub-issues when it feels like the parent issue at #2346261: Deprecate entity_create() in favor of a <EntityType>::create($values) or \Drupal::entityManager()->getStorage($entity_type)->create($values) still hasn't reached a decision.

I would agree that code clean-up issues like this can never be "major." The code still works just fine, even if it's uglier to look at. As Berdir mentions, one of the things under definition of "normal task" is "replacing code that works with code that is the current best practice" and that's exactly what we're doing here. That ensures we reserve "major" priority for issues that match its definition, which includes things like "significant repercussions."

So my preference would be to postpone work (and demote to normal) this meta/child issues until the parent issue is settled. Per the core decision-making documentation, this much code refactoring to me qualifies as "significant impact" to the entity / fields subsystem, and so we'd want to make sure we have sign-off from the entity/field API maintainers before proceeding.

DuaelFr’s picture

Sorry to be such a annoyance. I'll fix the status of all the sub-issues.

chananapeeyush’s picture

@DuaelFr,
Are we going to postpone the sub issues as well as the meta has been postponed ?
Is there any logic behind that are not postponing the sub issues ?

xjm’s picture

Thanks @DuaelFr and @trwad for all your work on these issues so far. I agree that the invdividual sub-issues should be postponed for now.

Additionally, we should not make this kind of change with one patch per module. It's better to restrict changes to a specific scope instead. See, for example, the work in #2322195: Replace all instances of user_load(), user_load_multiple(), entity_load('user') and entity_load_multiple('user') with static method calls and also these notes on meta scoping. So when we unpostpone them (whenever that is), we should merge them into one patch that only replaces one thing with another thing, without other code changes.

webchick’s picture

You're *totally* not being an annoyance, don't worry! The priorities are not always straight-forward. And I just want to make sure all the various stakeholders are comfortable with it before going all out. :)

The advantage to postponing the sub-issues is people don't potentially waste effort and we don't accidentally commit one of them without the parent issue being resolved like we did with #2491009: Replace deprecated usage of entity_create with a direct call to the entity type class in Field UI module (which, FWIW, looked absolutely fine to me). But I also acknowledge it's a *ton* of busywork, which would be nice to avoid. An email just went out to the committers tonight alerting them of the need to resolve the parent issue prior to committing further patches, so we should be fine as far as that goes.

webchick’s picture

Oops. Cross-post! However, 1 patch for the whole whack of them sounds fine to me if it's truly the same pattern repeated over and over across the whole system. Much easier to review/commit that way.

xjm’s picture

What might make the most sense would be to do one patch per entity type, as was done for the load method. We can probably put together starter patches from the by module issues that were already filed. So about 6-7 large patches rather than dozens of 5K ones. So the existing issues could be marked as duplicates of those larger patches (and applied together in many cases to create them). However, let's wait for #2346261: Deprecate entity_create() in favor of a <EntityType>::create($values) or \Drupal::entityManager()->getStorage($entity_type)->create($values) before starting that.

Also note that, in general, removing usage of code that's deprecated for 9.0 isn't a prioritized change for the beta phase -- just code that was deprecated for 8.0.0 before the beta. However, in the case of this it's horribly inconsistent with the rest of the entity API, so I am in favor of completing the code removals if #2346261: Deprecate entity_create() in favor of a <EntityType>::create($values) or \Drupal::entityManager()->getStorage($entity_type)->create($values) goes in.

I'm postponing the previously RTBC issues, at least. It'd be appreciated if the others could be postponed as well so that Novice contributors don't invest effort in them when they won't be accepted currently. Thanks!

DuaelFr’s picture

Ok, thank you for these clarifications. I'll postpone all the remaining open sub-issues.

In fact, the discussion in #2346261: Deprecate entity_create() in favor of a <EntityType>::create($values) or \Drupal::entityManager()->getStorage($entity_type)->create($values) only concerns the usage of entity_create with a varaible entity type what is a small case of this meta.
So, can we start working on patches by entity type as this way of solving the problem has a consensus?

DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes

Opened one sub-issue as a sample: #2494775: Replace deprecated usage of entity_create('action') with a direct call to Action::create()
@webchick @xjm does it seem OK for you to proceed like this?

chananapeeyush’s picture

As per the above #22, AFAIU,we also need to open another follow up issue that will replace deprecated usage of entity_create('comment',..... with Comment Entity Class create static function i.e. Comment::create() Are we going to proceed with it like this ? Are we approaching the right way ?
For any guidance, thanks in advance. :)

DuaelFr’s picture

DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes
DuaelFr’s picture

Issue summary: View changes
cilefen’s picture

Wow, I should have read this first before my comment in #2503379-19: Replace deprecated usage of entity_create('filter_format') with a direct call to FilterFormat::create(). Actually, that's a good lesson for me.

@DuaelFr Nice work on getting rid of these deprecated usages. I'd like to triage this issue and consider whether it is suitable for the beta phase:

  • It is not an "unfrozen" change.
  • Is it a prioritized change? I think it is not. The function is marked as deprecated and it will be removed before Drupal 9, not Drupal 8.0. Usages in core can be removed later.
  • Is it disruptive? It "will require changes across many subsystems and patches in order to make core consistent". With this patch applied there are more than 300 files using entity_create() in core. Many other issues may have conflicts with this one.

I updated the beta evaluation accordingly. Ultimately, the scale of this change in terms of the amount of files affected suggests to me that it should be postponed to 8.1.x. If it were only a handful of files in an isolated system, things would be different. The beta phase rules are not only intended to increase stability, they are also for focusing the attention of contributors and committers on issues that put Drupal 8 in a releaseable state.

I changed its beta eval to:

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task, because Drupal will continue working during and after the beta without this change.
Issue priority Normal because it's just about code cleanup and good practices
Prioritized changes This is not prioritized because it does not "improve Drupal 8's stability or move Drupal 8 closer to a releasable state".
Disruption This change could be disruptive to many pending patches because there are hundreds of files in core that call entity_create(). In addition, some usages may require injection, which again is more disruptive than a simple find and replace. Simply it "will require changes across many subsystems and patches in order to make core consistent."
DuaelFr’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Active » Postponed

As @alexpott said in #2494775-8: Replace deprecated usage of entity_create('action') with a direct call to Action::create(), this meta and all it's sub issues are now postponed to Drupal 8.1.

DuaelFr’s picture

All the by-module sub-issues has been closed as duplicate and all the by-entity-type sub-issues has been postponed to 8.1.
See you in a few months.

webchick’s picture

Dang. Thanks so much for pushing on this, DuaelFr. Definitely looking forward to seeing you back working on this in 8.1.x. :D

xjm’s picture

Issue summary: View changes
xjm’s picture

Issue summary: View changes
Mac_Weber’s picture

Issue summary: View changes
Status: Postponed » Active

Reopening, as we are also working at branch 8.1.x now

Mac_Weber’s picture

Issue summary: View changes

Marking some items with no occurrences

Mac_Weber’s picture

Issue summary: View changes
Wim Leers’s picture

Just wanted to say: yay for consistency!

Mac_Weber’s picture

@Wim Leers regarding consistency, we had some discussion about how to create the entities for this meta, but we could not reach a consensus. Some core developers are in favor of using Dependency Injector in tests, others prefer to not use it in favor of DX. See #2644752: [policy] Decide how to create/load entities in procedural code and test classes

Mile23’s picture

There doesn't seem to be a child issue for entity_create('entity_test'). Also, maintainers want to consolidate the remaining entity_test issues: #2641544-9: Replace deprecated usage of entity_create('entity_test_rev') with a direct call to EntityTestRev::create()

So I combined those two things here: #2669400: Replace deprecated usages of entity_create('type') with a direct call to <EntityType>::create() which serves as a catch-all for the remaining usages of entity_create() with a string constant.

Mile23’s picture

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

The last child issue is ready #2641518: Replace deprecated usage of entity_create('config_test') with a direct call to ConfigTest::create()

but there's still 12-15 mentions

$ git grep " entity_create("
core/includes/entity.inc:308:function entity_create($entity_type, array $values = []) {
core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php:90:   * @see entity_create()
core/modules/block_content/src/Tests/Views/BlockContentTestBase.php:61:   *   used in entity_create().
core/modules/block_content/tests/src/Functional/Views/BlockContentTestBase.php:56:   *   used in entity_create().
core/modules/content_translation/tests/src/Functional/ContentTranslationSyncImageTest.php:128:    $entity = entity_create($this->entityTypeId, $values);
core/modules/field/src/Entity/FieldConfig.php:87:   * @see entity_create()
core/modules/field/src/Entity/FieldStorageConfig.php:241:   * @see entity_create()
core/modules/field/src/Tests/reEnableModuleFieldTest.php:98:    $field_storage2 = entity_create('field_storage_config', [
core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldTranslatedReferenceViewTest.php:287:    $node = entity_create($this->testEntityTypeName, [
core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldTranslatedReferenceViewTest.php:311:    $node = entity_create($this->testEntityTypeName, [
core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldTranslatedReferenceViewTest.php:330:    $node = entity_create($this->testEntityTypeName, [
core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php:248:    $referencing_entity_1 = entity_create($this->entityType, ['name' => $this->randomMachineName()]);
core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php:271:    $referencing_entity_2 = entity_create($this->entityType, ['name' => $this->randomMachineName()]);
core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php:250:    $entity = entity_create($entity_type, ['id' => 1, 'revision_id' => 1, 'type' => $this->fieldTestData->field->getTargetBundle()]);
core/modules/field/tests/src/Kernel/FieldValidationTest.php:37:    $this->entity = entity_create($this->entityType, [
core/modules/field/tests/src/Kernel/TranslationTest.php:181:      $entity = entity_create($entity_type_id, $values);
core/modules/hal/src/Normalizer/ContentEntityNormalizer.php:100:   *   Unused, entity_create() is used to instantiate entity objects.
core/modules/language/src/Entity/ContentLanguageSettings.php:76:   * @see entity_create()
core/modules/node/tests/src/Traits/NodeCreationTrait.php:44:   *   entity_create(). Override the defaults by specifying the key and value
core/modules/user/tests/src/Traits/UserCreationTrait.php:92:   *   (optional) The weight for the role. Defaults NULL so that entity_create()
core/modules/user/tests/src/Traits/UserCreationTrait.php:119:   *   (optional) The weight for the role. Defaults NULL so that entity_create()
core/scripts/generate-d7-content.sh:143:    $term = entity_create('taxonomy_term', array(
core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php:110:    $entity = entity_create($entity_type, ['name' => 'test', 'user_id' => $user1->id()]);
core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php:113:    $entity = entity_create($entity_type, ['name' => 'test2', 'user_id' => $user1->id()]);
core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php:111:    $referenced_entity = entity_create($this->referencedEntityType, ['type' => 'non_referenceable']);
core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php:170:      $entity = entity_create($entity_type, [
xjm’s picture

Yep, I think we need two more child issues, one that replaces stray documentation references to entity_create(), and another that replaces calls that use a generic entity type (which can easily be converted to use the entity type manager).

anya_m’s picture

Issue summary: View changes

I've created new child issue for replacing all remain occurrences and edited summary.

anya_m’s picture

Issue summary: View changes

I've created new child issue for replacing stray documentation references to entity_create()

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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