Problem/Motivation
The use of the #[Symfony\Component\DependencyInjection\Attribute\Autowire] attribute in ContainerInjectionInterface and ContainerFactoryPluginInterface class constructors is supported since #3394870: Allow controller service wiring via constructor parameter attributes and #3452852: Add create() factory method with autowired parameters to PluginBase, but so far, only services can be autowired this way.
Since service class constructors can use the attribute to autowire container parameters as well, it would be useful to extend this functionality to AutowireTrait /AutowiredInstanceTrait. For an example, see
Drupal\rest\Plugin\views\display\RestExport
, where the create() method inejcts the authentication_providers and serializer.format_providers container parameters.
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3558292
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:
- 3558292-support-passing-container
changes, plain diff MR !14562
Comments
Comment #2
godotislateComment #3
godotislateComment #4
dimitriskr commentedI'm going to throw #3552110: Remove manual create() method from plugins as related.
Comment #8
longwaveImplemented this, with test coverage for the entire trait.
Comment #9
claudiu.cristeaI'm expecting that we can even autowire a plain value. If we can pass a simple constant/value in *.services.yml, why wouldn't that be possible by autowiring? Looking to \Symfony\Component\DependencyInjection\Attribute\Autowire::__construct(), I see that it's possible to do by passing the $value param, e.g.,
#[Autowire(value: 'foo')]Comment #10
longwave@claudiu.cristea that is what #3533263: AutowireTrait has wrong behavior for literal string values is for, let's solve that separately?
Comment #11
godotislateThis looks good to me. I was surprised there wasn't already a dedicated AutowiredInstanceTrait test, so that was good to see.
This will conflict hard with #3558306: Support automatic setter injection using the #[Required] attribute in AutowireTrait/AutowiredInstanceTrait, though, but I guess we'll just see which gets in first.
Comment #12
claudiu.cristeaSure, I've missed that issue.
BTW, I've been working for the same in Drush https://github.com/drush-ops/drush/pull/6061. I hit quite a lot of complexity because Drush handles two containers: Drupal's and its own (which is League)
Comment #13
claudiu.cristeaThis is a blocker for #3226806: Move filter implementations from filter.module to plugin classes
Comment #14
longwaveNeeds reroll following #3558306: Support automatic setter injection using the #[Required] attribute in AutowireTrait/AutowiredInstanceTrait
Comment #15
longwaveRebased and combined with #3558306: Support automatic setter injection using the #[Required] attribute in AutowireTrait/AutowiredInstanceTrait so you can also use setter injection for container parameters if you need to.
Comment #16
godotislate1 question on the MR.
Comment #17
longwaveAdded test for nullable services, also merged the setter test into the main test which reduces some duplication and proves that we can use all features simultaneously.
Comment #18
godotislateNice work! lgtm.
Comment #19
godotislateAdded a CR: https://www.drupal.org/node/3575335
Comment #20
longwaveI realised this doesn't support default values properly, e.g.
This will fail if
some.parameteris not set - but this feels like an edge case that we can handle in a followup if we need the feature.Comment #21
godotislateI agree that it can be handled in a follow up.
A workaround in the interim would be to make the parameter nullable and then assign it to a default value in the constructor if NULL.
Comment #22
godotislateComment #23
alexpottI considered asking for similar optimisations that Symfony uses ir. to check if the first character is a % before doing the regex but I decided that felt like premature optimisation.
Committed and pushed 73c6e0ac22d to main and 0a46fe24b9e to 11.x. Thanks!