Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
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
User interface changes
None.
API changes
None.
Comment | File | Size | Author |
---|---|---|---|
#24 | fix_all_the_things_mage.jpeg | 45.62 KB | DuaelFr |
Comments
Comment #1
DuaelFrComment #2
DuaelFrComment #3
DuaelFrComment #4
DuaelFrComment #5
DuaelFrComment #6
DuaelFrComment #7
DuaelFrMajor 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.
Comment #8
DuaelFrComment #9
DuaelFrComment #10
DuaelFrComment #11
DuaelFrAll existing sub-issues has been updated.
Comment #12
BerdirNot 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:
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.
Comment #13
webchickSo 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.
Comment #14
DuaelFrSorry to be such a annoyance. I'll fix the status of all the sub-issues.
Comment #15
chananapeeyush CreditAttribution: chananapeeyush as a volunteer and commented@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 ?
Comment #16
xjmThanks @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.
Comment #17
webchickYou'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.
Comment #18
webchickOops. 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.
Comment #19
xjmWhat 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!
Comment #20
DuaelFrOk, 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?
Comment #21
DuaelFrComment #22
DuaelFrOpened 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?
Comment #23
chananapeeyush CreditAttribution: chananapeeyush as a volunteer and commentedAs 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. :)
Comment #24
DuaelFr#2346261: Deprecate entity_create() in favor of a <EntityType>::create($values) or \Drupal::entityManager()->getStorage($entity_type)->create($values) is now fixed!
Now let's fix all the things!
Comment #25
DuaelFrComment #26
DuaelFrComment #27
DuaelFrComment #28
DuaelFrComment #29
DuaelFrComment #30
DuaelFrComment #31
cilefen CreditAttribution: cilefen commentedWow, 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.
I changed its beta eval to:
Beta phase evaluation
Comment #32
DuaelFrAs @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.
Comment #33
DuaelFrAll 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.
Comment #34
webchickDang. Thanks so much for pushing on this, DuaelFr. Definitely looking forward to seeing you back working on this in 8.1.x. :D
Comment #35
xjmComment #36
xjmComment #37
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedReopening, as we are also working at branch 8.1.x now
Comment #38
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedMarking some items with no occurrences
Comment #39
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedComment #40
Wim LeersJust wanted to say: yay for consistency!
Comment #41
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commented@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
Comment #42
Mile23There 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.Comment #43
Mile23Updating with links to issues.
Just added #2669926: Replace deprecated usage of entity_create('taxonomy*') with a direct call to Vocabulary::create()
Comment #48
andypostThe 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
Comment #49
xjmYep, 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).Comment #50
anya_m CreditAttribution: anya_m as a volunteer and at Skilld commentedI've created new child issue for replacing all remain occurrences and edited summary.
Comment #51
anya_m CreditAttribution: anya_m as a volunteer and at Skilld commentedI've created new child issue for replacing stray documentation references to
entity_create()
Comment #54
andypostI think it's fixed because the last issue left #2928930: Properly deprecate entity_create() and remove all occurrences and I closed as duplicate #2932317: Replace stray documentation references to entity_create() of it