Problem/Motivation
Core prevents service decoration of entity type manager, which is sometimes necessary to override some functions.
Particularly, clean override of handlers.
Right now service decorators need to extend \Drupal\Core\Entity\EntityTypeManager in order to get around this.
Steps to reproduce
Implement a service decorator to entity type manager.
Enable views.
Clear caches.
Proposed resolution
Change dependency injection to interface.
Whether you agree decorating this service is good or not, it should still be possible. And typehinting concrete implementations is seen as a big no-no.
Remaining tasks
\Drupal\views\FieldViewsDataProvider::__construct(EntityTypeManager entityTypeManager)
to
\Drupal\views\FieldViewsDataProvider::__construct(EntityTypeManagerInterface entityTypeManager)
There is also a \Drupal\Tests\workspaces\Kernel\EntityReferenceSupportedNewEntitiesConstraintValidatorTest::$entityTypeManager, but that is not runtime code. So that can be optional/bonus change.
User interface changes
None
Introduced terminology
None
API changes
None
Data model changes
None
Release notes snippet
None
Issue fork drupal-3564264
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:
- 3564264-fix-entitytypemanager-typehint
changes, plain diff MR !14152
Comments
Comment #4
jani commentedI've created a merge request that changes the constructor typehint from EntityTypeManager to EntityTypeManagerInterface in \Drupal\views\FieldViewsDataProvider.
The changes include:
1. Updated the use statement on line 7 to import EntityTypeManagerInterface instead of EntityTypeManager
2. Changed the constructor parameter typehint on line 23 from EntityTypeManager to EntityTypeManagerInterface
This allows for proper service decoration of the entity type manager as described in the issue.
Comment #6
longwaveComment #7
rollins commentedI strongly support this change. Relying on the concrete EntityTypeManager class in the constructor creates unnecessary tight coupling.
Switching to EntityTypeManagerInterface is the architecturally correct approach here.
RTBC.
Comment #8
catchCommitted/pushed to main and cherry-picked to 11.x, thanks!
Comment #14
catchBackported to 11.3.x via a request from @amateescu since this would unblock some changes in Trash module. We're not really changing what the constructor takes here except opening it up.
Comment #15
dpiThanks @catch, that'll help tidy up some things for some private projects that triggered the related change in #3564263: Change entity type manager class override to service decoration and also https://git.drupalcode.org/project/pinto_entity/-/blob/1.x/src/PintoEnti... without needing to wait for new core minor <3
Comment #16
quietone commentedChanging version because it was backported.