Add an Autowire trait for plugins

Created on 6 June 2024, 18 days ago
Updated 11 June 2024, 12 days ago

Problem/Motivation

We already have a \Drupal\Core\DependencyInjection\AutowireTrait class, suitable for wiring dependencies into classes extending \Drupal\Core\DependencyInjection\ContainerInjectionInterface, implemented in πŸ“Œ Allow controller service wiring via constructor parameter attributes Fixed

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 πŸ“Œ Allow plugin service wiring via constructor parameter attributes Needs work , 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

Release notes snippet

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 1 hour ago

Created by

πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024