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);.
Proposed resolution
Replace the deprecated call to entity_create() by a proper call to <EntityType>::create().
Before:
entity_create('config_test', $params)->save();
After:
\Drupal::entityTypeManager()->getStorage($entity_type)->create($params)->save();
Remaining tasks
| 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.
Comments
Comment #2
anya_m commentedComment #3
andypostLooks everything covered
Comment #5
andypostBot failure
Comment #7
Anonymous (not verified) commentedRevert status after random fail.
Comment #8
larowlanIs there an issue to add the trigger_error to entity_create?
Then we know it is gone (for good)?
Comment #10
anya_m commentedI've added trigger_error for entity_create function
Comment #11
berdirI'm not sure what the rule now is. Obviously this will trigger a lot of deprecations in contrib, do we now no longer care about that since they are not reported anymore by testbot (for now)?
this could also use FieldStorageConfig::create() but either is fine.
Looks ok to me too.
Comment #12
larowlan- yep that's the point and it will power #2822727: [policy, no patch] Adopt a continuous API upgrade path for major version changes
This needs to reference the change record in which we deprecated it. See the 'how we deprecate' section in https://www.drupal.org/core/deprecation
Comment #14
rakesh.gectcr@larowlan,
Looks like there is no explicit change of record for the same, https://www.drupal.org/project/drupal/issues/2490966 , Do we need to add one ?
Comment #15
andypost@rakesh.gectcr yes, it needs new one like we dong for database.inc and others from #2999721: [META] Deprecate the legacy include files before Drupal 9
Comment #16
andypostWorking on related I think better to file new CR to notify contrib that now deprecation will throw
Comment #17
andypostThere's existing CR which I did update with new references https://www.drupal.org/node/2266845
This method is deprecated it in #2346261: Deprecate entity_create() in favor of a <EntityType>::create($values) or \Drupal::entityManager()->getStorage($entity_type)->create($values)
Meanwhile closed #2932317: Replace stray documentation references to entity_create() as there's only few leftovers in docs which added to patch
Here's reroll and clean-up of doc blocks, also added deprecation test
No interdiff because it's bigger then patch
this is d7 script which should not be changed
Comment #18
andypostfix missed coding standard
Comment #21
andypostThis was wrong, cos it returns ID instead of entity
Comment #23
berdirI think the second part here doesn't really make sense anymore. A whole lot about this seems very strange, the variable name is completely bogus.
Not sure if it is out of scope, but I'd suggest to just rename it to $values and just say "The values for the block_content entity"
Ah, I guess this settings stuff in block_content was copied from node, equally weird here.
should be "so that *the* created entity", but it's still weird, because the entity was then already created and isn't able to "set" something. Maybe just:
Defaults to NULL, which sets the weight to maximum + 1?
Comment #24
berdirComment #25
mikelutzComment #26
mikelutzAddressed Berdir's feedback and re-rolled. I went ahead and changed the variable names from $settings to $values in all three instances for clarity. Interdiff might be off slightly, it's between this patch and the initial reroll.
Comment #27
berdirThe new variables are so much better IMHO. Looks you missed on of the two cases here, though. Also, Defaults *to* NULL I think?
Comment #28
mikelutzYep, I missed that.
Comment #29
berdirThanks, looks good now I think.
Comment #30
larowlanShould this be 'Use the create method'?
should we reference the replacement method? not fussed
Comment #31
berdir2. I was thinking about that too, not sure. You can clearly see that the constructors of those 3-4 cases have been copied from each other, ContentLanguageSettings even still references FieldConfig::create(). The base class doesn't have anything like that, there also seem other things that are outdated, like mentions of a BC layer but also mismatches on field_name vs name vs id. Since it does reference the create method above (even if wrongly sometimes), I think removing that is fine. IMHO the only one that is a bit special is FieldConfig::__construct() because that allows different versions (field storage or field name + entity type) some aren't real properties, so there isn't really a good place to document that. For the others, I'd even consider just replacing the whole thing with an {@inheritdoc}.
Comment #32
berdirConflicted on EntityLegacyTest, reroll of that, just two methods being added in the same place. Also added the *the*.
Comment #33
berdirI only did a reroll and added a word to the deprecation message so I think it is OK if I set it back to RTBC. We'll need someone to decide on #30.2, I provided background in #31.
Comment #34
larowlanCommitted 9b1690c and pushed to 8.8.x. Thanks!