Problem/Motivation
Now that plugins can be autowired thanks to #3452852: Add create() factory method with autowired parameters to PluginBase we can remove the create() method from many, if not all, plugins in core.
Steps to reproduce
Proposed resolution
Remove the create() method of all descendents of PluginBase. Flag the relevant constructor parameters with the necessary autowiring attributes.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3552110
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:
- 3552110-remove-plugin-create
changes, plain diff MR !13488
Comments
Comment #2
mstrelan commentedAdding related issue for controllers
Comment #3
mstrelan commentedNot sure the best way to find these, but this finds all files in
src/Plugindirs with the same signature:$ find core -type f -path "*/src/Plugin/*" -exec grep -rH 'public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition)' {} \;Note this doesn't include classes extending DeriverBase, ConstraintValidator or any Migration plugins with a ?MigrationInterface param. There are potentially others.
Ideally this is a job for rector as there are 172 exact matches.
EDIT: Sorry for multiple edits, I messed up the find command a couple times
Comment #5
dcam commentedNot sure what the best way is either, but PHPStorm located overrides of
PluginBase::create()for me and I'm going with that.Comment #7
dcam commented@mstrelan I'm still working on it, finding issues caused by removing too much.
Comment #8
berdirHad a quick attempt at profiling this, not spotting any major differences, variation is quite high, but I've had less than 5% time/memory differences.
Tested with disabled render caches on umami. 644 calls to ContainerFactory::createInstance(), in total 20ms/5%. \Drupal\Core\DependencyInjection\AutowiredInstanceTrait::createInstanceAutowired doesn't show up at all, I guess it's because it's a trait and split among many different classes, so doesn't registered as the same thing. ContainerFactory::createInstance() reports as 80% own cost, but I think that's just blackfire being unable to attribute it to anything else.
If anything, just spotted unrelated issues:
* ViewsBlock is by far the most expensive plugin to create, 10% of the total time with just 4 calls. This doesn't even use autowire yet due to the entity storage being injected and it does too much in in __construct().
* 100 calls to Drupal\Core\ImageToolkit\ImageToolkitManager::createInstance(). less than 1% of total time, but still, seems like a waste. It's actually all coming from 100 calls to isAvifSupported, I'm not sure we can statically cache the default instance as it has state (fibers..) but we can definitely statically cache if we support avif or not.
Comment #9
dcam commentedI responded to the feedback that was already given.
Comment #10
mondrakeAny reason why
Drupal\image\ImageEffectBaseis not converted?Comment #11
dcam commentedI checked for logger channels that are defined and not in use by the plugins, but missed that one. Thanks for catching it.
Comment #12
mondrakeTagging for release priority - having done #3452852: Add create() factory method with autowired parameters to PluginBase this is needed if we want to have contrib starting conversions. If core plugins still have the
create()method, contrib extending from those cannot benefit from the changes (unless they override the method, but that will mean touching the code twice)Comment #13
smustgrave commentedAppears to need a manual rebase.
Comment #14
dcam commentedI reverted the changes to
core/modules/filter/src/Plugin/migrate/process/FilterID.phpwhich was recently deprecated. That deprecation causes PHPStan issues incore/modules/filter/src/Plugin/migrate/process/FilterID.phpwhenFilterID::create()is removed. I couldn't stop the errors. If someone has an idea, then I'll try to fix it. But for now I decided to revert the changes since the class is deprecated anyway. So I'm not sure it matters.Comment #15
mondrakeReverted the revert (would that fit?), and just adjusted the baseline removing the entry that is no longer matched.
Comment #16
dcam commentedUgh, thank you. I could not figure out where that was coming from. I checked the output multiple times, but never saw any indication. IDK if I just missed it or didn't understand what it was saying.
Comment #17
mondrakeWhen PHPStan reports
Ignored error pattern […] was not matched in reported errors., it means that an ignored error, either in baseline or in the neon file, is no longer there. The way to solve is to remove the pattern entry from the baseline or the file. Or to regenerate the baseline.Comment #18
dcam commentedThank you for instructing me on that. I'm going to go back and look for it when I'm home later.
Comment #20
grimreaperHello,
For #3422958: Error: Call to a member function transformRelative() , I removed the create from core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php.
The problem is that it was an entity storage that was injected. So I did minimal change to only switch to EntityTypeManagerInterface.
Regarding https://mglaman.dev/blog/dependency-injection-anti-patterns-drupal, the storage should be obtained only at the moment of usage, not in the constructor, but I guess this should be a separated issue.
Comment #21
longwaveAdded some questions, also untagging as this isn't going to make 11.3.0.
Comment #22
dcam commentedI've made the changes and answered the questions.
Comment #23
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #24
dcam commentedRebased. There was a conflict in the workspaces_ui block.
Comment #25
longwaveThere's a couple of services that could be autowired better, but that's out of scope to solve here because it means changing more things, we can deal with that in followups.
Comment #26
catchOne question on the MR, leaving RTBC because I'm hoping it's a bad question and we can leave as-is.
Comment #27
catchCommitted/pushed to main and cherry-picked to 11.x, thanks!
Comment #30
catchNo unfortunately not - MR doesn't apply, committed it when it did, then another issue landed, then I couldn't rebase main to push.
Comment #32
dcam commentedRebased.
Comment #34
catchCommitted/pushed to main and 11.x, thanks!