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

Issue fork drupal-3552110

Command icon 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:

Comments

longwave created an issue. See original summary.

mstrelan’s picture

Adding related issue for controllers

mstrelan’s picture

Issue summary: View changes

Not sure the best way to find these, but this finds all files in src/Plugin dirs 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

dcam made their first commit to this issue’s fork.

dcam’s picture

Not sure what the best way is either, but PHPStorm located overrides of PluginBase::create() for me and I'm going with that.

dcam’s picture

@mstrelan I'm still working on it, finding issues caused by removing too much.

berdir’s picture

Had 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.

dcam’s picture

Status: Active » Needs review

I responded to the feedback that was already given.

mondrake’s picture

Any reason why Drupal\image\ImageEffectBase is not converted?

dcam’s picture

Any reason why Drupal\image\ImageEffectBase is not converted?

I checked for logger channels that are defined and not in use by the plugins, but missed that one. Thanks for catching it.

mondrake’s picture

Tagging 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)

smustgrave’s picture

Status: Needs review » Needs work

Appears to need a manual rebase.

dcam’s picture

Status: Needs work » Needs review

I reverted the changes to core/modules/filter/src/Plugin/migrate/process/FilterID.php which was recently deprecated. That deprecation causes PHPStan issues in core/modules/filter/src/Plugin/migrate/process/FilterID.php when FilterID::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.

mondrake’s picture

Reverted the revert (would that fit?), and just adjusted the baseline removing the entry that is no longer matched.

dcam’s picture

Ugh, 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.

mondrake’s picture

When 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.

dcam’s picture

Thank you for instructing me on that. I'm going to go back and look for it when I'm home later.

grimreaper made their first commit to this issue’s fork.

grimreaper’s picture

Hello,

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.

longwave’s picture

Status: Needs review » Needs work
Issue tags: -11.3.0 release priority

Added some questions, also untagging as this isn't going to make 11.3.0.

dcam’s picture

Status: Needs work » Needs review

I've made the changes and answered the questions.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new91 bytes

The 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.

dcam’s picture

Status: Needs work » Needs review

Rebased. There was a conflict in the workspaces_ui block.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

There'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.

catch’s picture

One question on the MR, leaving RTBC because I'm hoping it's a bad question and we can leave as-is.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to main and cherry-picked to 11.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

catch’s picture

Status: Fixed » Needs work

No unfortunately not - MR doesn't apply, committed it when it did, then another issue landed, then I couldn't rebase main to push.

dcam’s picture

Status: Needs work » Reviewed & tested by the community

Rebased.

  • catch committed 563b0179 on main
    task: #3552110 Remove manual create() method from plugins
    
    By: longwave...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to main and 11.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • catch committed 8fe8e14b on 11.x
    task: #3552110 Remove manual create() method from plugins
    
    By: longwave...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.