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

Comments

Mac_Weber created an issue. See original summary.

jhodgdon’s picture

Title: Decide how to create/load entities in procedural code and test classes » [policy] Decide how to create/load entities in procedural code and test classes
Component: documentation » entity system
Category: Task » Plan

This is not a docs issue, nor a task. Changing issue factors accordingly.

chx’s picture

Status: Active » Closed (works as designed)

This decision is made clear and loud already in the entity_load @deprecated documentation

Use The method overriding Entity::load() for the entity type, e.g. \Drupal\node\Entity\Node::load() if the entity type is known. If the entity type is variable, use the entity manager service to load the entity from the entity storage:

Same for entity_create

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:

dawehner raised a practical concern whether these work properly when someone uses setClass -- yes they do! That's why getEntityTypeFromClass calls getOriginalClass.

Theoretical concerns making everything thousand times more complicated like other parts of Drupal 8 are not relevant here.

Crell’s picture

Status: Closed (works as designed) » Active

Testability is very relevant. The use of static class calls harms testability. This is hardly a new statement, nor even a controversial one.

chx’s picture

Status: Active » Closed (works as designed)

Edit: deleted.

chx’s picture

Status: Closed (works as designed) » Active
chx’s picture

Issue summary: View changes
chx’s picture

Issue summary: View changes
dawehner’s picture

IMHO there is just no reason for procedural code. If you have a hook you can simply call out to an object (service even, if needed).

bojanz’s picture

1) If you must have procedural code, use the static methods. That was why entity_load & friends point to them in the first place, for procedural usage.
2) If you have a class, inject the entity type manager and get the storage.

Most/all code already does #1 and #2. The question is just about whether to take shortcuts in tests or not (#1 vs #2).
Entity API is not unit testable anyway, so tests can get away with #1, but #2 is a good habit to have in general.

Crell’s picture

If there were a +1 button, I would just push it on #9 and #10 several times and be done with it. :-)

mac_weber’s picture

@chx why not change the documentation adding instructions for test classes as pointed by #9 and #10?

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.

Even if it spreads everywhere, as you said, "there is barely any procedural code working with entities in core". Then, it would spread to contrib.

A simple way to avoid such spreading is to add a line of comment before the implementations in test files. Yet I think contrib developers not just copy the code, but also read the documentation - which would also have that note about the difference in test files and procedural code.

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

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should 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.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should 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.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should 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.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Active » Closed (works as designed)

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.

quietone’s picture

Version: 11.x-dev » 10.3.x-dev

Changing to latest version when this was closed.