Problem/Motivation
Sending some patches to this issue
#2490966: [Meta] Replace deprecated usage of entity_create with a direct call to the entity type class →
some core developers argued that in test classes we should use Dependency Injection instead of calling static methods. This helps decoupling and makes tests easier.
Some developers do not agree with it, saying that the entity maintainers already made the decision and codified it in the @deprecated section of the doxygen of entity_create and entity_load. Moreover, maybe we should not decouple everything. It makes code more difficult to read and to debug.
In addition, the methods EntityClass::create() are not bullet proof and they do not work for 100% of the cases. See the error got by the testbot while using them:
https://www.drupal.org/pift-ci-job/124330 →
This error was fixed by using the long chaining way in this patch:
https://www.drupal.org/node/2641518#comment-10715660 →
. This is hardly relevant as it's just a bug but it's an excellent excuse to make Drupal 8 even harder to understand, develop and debug.
We had some discussion on IRC #drupal-contribute, but we could not reach a consensus. Another concern was whether overriding an entity class with setClass would break Node::load(). It would not. Entity API, while sometimes perhaps clunky and certainly a bit performance challenged is a reasonably well working and not too fragile unlike other parts of D8.
For consistency, we should pick a way we will work in core.
Proposed resolution
Choose a standard way to create and load entities.
Decide if the same way will be used both in procedural code and also in test classes. This is also a straw man, there is barely any procedural code working with entities in core and test classes are the place everyone copies the code out from. So if we were to override the already made decision solely for tests it would spread to everywhere.
Decide how to proceed with that meta issue and others, while we do not make the final decision for this standard here.
Remaining tasks
Get some people to get off the theory high horse. The decision was made long ago and it's the simple good one:
https://api.drupal.org/api/drupal/core!includes!entity.inc/function/enti...
Use The method overriding Entity::create() for the entity type, e.g. \Drupal\node\Entity\Node::create() if the entity type is known. If the entity type is variable, use the entity storage's create() method to construct a new entity:
User interface changes
API changes
Data model changes