Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Standardize a create and wrap new entities. This would allow for easy implementation of default values.
Proposed resolution
Have a createEntity()
(maybe wrapNew()
) function in the TypedRepositoryInterface
.
An added bonus is the TypedRepositoryInterface
already knows the bundle and this can be autofilled.
Issue fork typed_entity-3213360
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
Birk CreditAttribution: Birk at B14 commentedI've added a proposed code solution in the issue fork. Only consisting of logic and no tests.
This is my first tumble with the forking approach, so let me know if I'm missing something.
Comment #3
e0ipsoI do not see a merge request in GitLab. I could access the code changes, in this link https://git.drupalcode.org/issue/typed_entity-3213360/-/compare/4.x...32... but I cannot merge them.
---
Feedback:
This is a fantastic idea. A couple of thoughts:
WrappedEntityStorageTrait
andWrappedEntityStorageInterface
would help achieve the same goal? That way we can provide a solution to a common use case, yet keep the base repository a bit cleaner. Maybe we can end up moving some stuff out from the base repository in the future.WrappedEntityInterface|null
. When do you think you will getnull
in this case?Comment #4
e0ipsoComment #5
Birk CreditAttribution: Birk at B14 commentedI thought you only opened the merge request once the code was complete, but I'll open it up straight away.
---
Not sure a trait would work, the new
createEntity()
function depends on multiple properties of the TypedRepository class (bundle, entityTypeManager and entityType).I haven't been able to find anything official on traits in Drupal, but with a few random samples from core it seems all traits are self contained, and make no assumption of the classes using them (which makes sense).
This doesn't rule out splitting the repository class into traits, but it seems like a refactoring issue worthy of it's own ticket.
Good catch with the
null
, that's an oversight, I also switch betweengetEntityType
andentityType
, I'll fix it.Comment #7
e0ipsoThis looks good. Despite the interface being updates, since there is a base class for it we can merge this withoug BC breaks.
Comment #9
e0ipso