- Issue created by @dpi
- Issue was unassigned.
- š¦šŗAustralia dpi Perth, Australia
Drive by code drop. Not working on this for now.
- š©š°Denmark arnested
Nice. I was looking for exactly this today!
I have tested the code together with Drupal 10.3 and can confirm it works.
Since I wanted to have the benefit of this right away, I took the liberty and made it a small module: https://www.drupal.org/project/autowire_plugin_trait ā
- š®š¹Italy mondrake š®š¹
Also see āØ Add a trait for autowiring properties in tests Needs work .
There Iām suggesting to add
symfony/expression-language
as a dependency.That would nicely allow using expressions and enable autowiring of constructs like
#[AutowireProperty(expression: "service('entity_type.manager').getStorage('node')")]
- Status changed to Needs review
3 months ago 12:57pm 22 August 2024 - š¬š§United Kingdom longwave UK
Can we just extend the existing AutowireTrait to work here?
public static function create(ContainerInterface $container) { $args = [];
becomes
public static function create(ContainerInterface $container, ...$args) {
ie. additional arguments passed to
create()
become the initial set of arguments? - š¬š§United Kingdom longwave UK
No, #8 doesn't work because
ContainerFactoryPluginInterface::create()
doesn't match. - š¬š§United Kingdom longwave UK
Added AutowirePluginTrait to PluginBase to enable autowiring by default (simply omit the
create()
method) and autowired some Views plugins to test that it all works. - š¬š§United Kingdom longwave UK
Does this even need to be a trait? Should it just be included directly in PluginBase?
Not sure we need explicit tests, given we are exercising it in Views tests now, so removing the tag.
- š¬š§United Kingdom longwave UK
Adding the trait to \Drupal\Core\Plugin\PluginBase (not Component) seems to work, I autowired the BlockContentBlock plugin to prove it.
- šŗšøUnited States smustgrave
Shouldn't there be a test coverage for the new exception being thrown?
throw new AutowiringFailedException($service, sprintf('Cannot autowire service "%s": argument "$%s" of method "%s::_construct()", you should configure its value explicitly.', $service, $parameter->getName(), static::class));
- š¬š§United Kingdom longwave UK
Added coverage for success and failure of autowiring a block plugin.
- š¬š§United Kingdom longwave UK
In fact let's do the refactor in #14 now to avoid copy-pasting the code here.
- š¦šŗAustralia mstrelan
Regarding the phpstan baseline, it turns out that phpstan will accept
@return
annotations, and we already have that inContainerFactoryPluginInterface
, butPluginBase
does not yet implement that interface. Have pushed a commit to implement that interface and update the baseline. - š¬š§United Kingdom longwave UK
Unfortunately implementing ContainerFactoryPluginInterface in PluginBase breaks a bunch of tests, I don't think Migrate plugins can cope with this as they have a slightly different constructor.