[DX] Remove direct use of new Node()
and other direct instantiations of entity classes from the codebase
#6075
Labels
new Node()
and other direct instantiations of entity classes from the codebase
#6075
When Backdrop partially incorporated D7 Entity API into core, and switched to using named classes for its core entities (that is,
Node
andUser
etc instead ofstdClass
), this allowed developers to extend those entity class (and their entity controllers) to incorporate additional functionalities and properties. This can be done very simply and efficiently, by (1) implementing ahook_entity_info_alter()
to change the entity class and the entity controller, and (2) extending the entity class and/or the entity controller class with the new functionality or properties.Unfortunately, at the same time, a few places in Backdrop codebase instantiate those core entity classes by making direct calls, as in
$node = new Node();
. This effectively curtails the new possibilities created by the use of named classes as described above. So, if you are a developer who wants to transform/expand nodes in new ways, you are limited by those few direct instantiations (so, in those places, the resulting entity of calling$node = new Node();
will use the core class, instead of the class you created to extend nodes).The solution here is to replace those few
new Node()
and similar calls, with the intermediary API functionentity_create('node');
. This call will take care of instantiating the entity class as defined in the entity_info_alter hook.There are very few direct instantiations of core entity classes in the codebase:
Many of those are in tests, which we can simply leave alone, as we want to use the core classes in tests.
The text was updated successfully, but these errors were encountered: