[policy] Decide how to create/load entities in procedural code and test classes

Created on 5 January 2016, almost 9 years ago
Updated 6 March 2024, 10 months ago

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

🌱 Plan
Status

Closed: works as designed

Version

11.0 🔥

Component
Entity 

Last updated about 17 hours ago

Created by

🇧🇷Brazil Mac_Weber

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇿New Zealand quietone

    There has been no discussion here for 8 years. There is a general sense that this is a working as designed. Therefore, I am restoring that status. If that is wrong, reopen the issue, by setting the status to 'Active', and add a comment.

Production build 0.71.5 2024