Problem/Motivation
To wire services to a form, developers must implement a custom create() method that pulls services from the container and calls the constructor.
Using PHP 8 attributes, we can tag constructor parameters directly and allow the base class to discover and inject the required services, dropping the create() method entirely.
In #3394870: Allow controller service wiring via constructor parameter attributes we added an AutowireTrait and applied it to ControllerBase for controllers. We can apply the same trait to FormBase for all forms to use.
Steps to reproduce
Proposed resolution
Add AutowireTrait to FormBase.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Issue fork drupal-3398534
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:
- 3398534-11.x
changes, plain diff MR !15046
- 3398534-allow-form-service
changes, plain diff MR !14568
Comments
Comment #3
sidharth_soman commentedAdded a commit adding the trait and removing the create method.
Comment #6
longwaveComment #7
dcam commentedThis looks like a simple change for some big wins in DX and the size of the PHPStan baseline. I only made notes on the MR so it's clearer why those changes were made. I had to go figure those things out, so I wanted to make certain no one else had to. LGTM.
Comment #8
dcam commentedSorry for walking that back, I thought maybe this should have a change record. The pending issue for controllers does. Maybe they could be combined?
Comment #9
longwaveThanks for the review. Yep, let's write a change record, might as well be a new one.
#3558306: Support automatic setter injection using the #[Required] attribute in AutowireTrait/AutowiredInstanceTrait will be useful here too, we can autowire the config factory with it, but that's for a followup.
Comment #10
longwaveAdded a CR: https://www.drupal.org/node/3575184
Comment #11
longwaveComment #12
dcam commentedI reviewed the change record and change to
ConfigFormBase. I don't have any additional feedback. This looks good to me.Comment #13
godotislate1 comment on the MR. Also needs merge conflict resolution.
Comment #14
longwaveRebased, removed the constructor docblock.
Comment #15
dcam commentedThe requested change was made. Back to RTBC.
Comment #18
godotislateCommitted to main. Needs a new MR for 11.x, probably for the baseline. Thanks everyone!
Comment #19
longwaveRebased on to 11.x.
Comment #21
dcam commentedLooks like there are some problems with PHPStan relating to modules that were removed from main.
Comment #22
longwaveWhoops, let's try again.
Comment #23
dcam commentedI verified these are the same changes that went into main, rebased for 11.x with baseline changes added. The test failure seems random, but I can't re-run it. LGTM.
Comment #26
godotislateCommitted ef55a8f and pushed to 11.x. Thanks!