Problem/Motivation
We already have a \Drupal\Core\DependencyInjection\AutowireTrait
class, suitable for wiring dependencies into classes extending \Drupal\Core\DependencyInjection\ContainerInjectionInterface
, implemented in #3394870: Allow controller service wiring via constructor parameter attributes
I think we should have a similar thing for \Drupal\Core\Plugin\ContainerFactoryPluginInterface
, so we can easily skip adding a create()
method for plugins.
I acknowledge we also have something similar #3294266: Allow plugin service wiring via constructor parameter attributes, but this is useful in the short term for improving DX, before removing the requirement for ContainerFactoryPluginInterface
. I guess the proposed new autowire trait would be removed from drupal alongside ContainerFactoryPluginInterface, if ever that happens. So I dont see this as redundant code.
Proposed resolution
Add a AutowireTrait for plugins, so they dont need to implement create().
Remaining tasks
- Tests.
- Should we switch any non-tests core usages now? My suggestion is not, because I imagine there will be a huge LOC change here (many LOC removals, for the better!). But create methods could also be doing other things, or not all dependencies autowirable yet.
User interface changes
Nil.
API changes
New trait
Data model changes
Nil