Problem/Motivation

Since Drupal 11.3 and https://www.drupal.org/node/3542837 the Drupal\Core\Plugin\PluginBase class has been extended with a generic version of the create() method suitable for most plugins with dependency injection.

The ExtraFieldDisplayBase and ExtraFieldFormBase classes could extend Drupal\Core\Plugin\PluginBase instead of Drupal\Component\Plugin\PluginBase and allow for the autowiring of services to the plugins which extend it (provided they implement \Drupal\Core\Plugin\ContainerFactoryPluginInterface).

Steps to reproduce

N/a.

Proposed resolution

Make the code change which should be as simple as switching between the two classes.

Remaining tasks

Create MR, review, merge.

User interface changes

None.

API changes

None.

Data model changes

None.

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

arousseau created an issue. See original summary.

arousseau’s picture

Issue summary: View changes
Status: Active » Needs review
pcambra’s picture

Status: Needs review » Needs work

Ah good to know, thanks @arousseau, but 3.x needs to support both 10.x and 11.x, how do you suggest we can add this while keeping BC?

arousseau’s picture

Hello pcambra. It should be safe to use in 10.x since the Drupal\Core\Plugin\PluginBase already exists in that version, and be useful for people using Extra Field with 11.3.x

See https://git.drupalcode.org/project/drupal/-/blob/10.0.x/core/lib/Drupal/... and https://git.drupalcode.org/project/drupal/-/blob/11.3.x/core/lib/Drupal/...

idebr’s picture

Drupal\Core\Plugin\PluginBase use a number of Traits that are also used in Extra Field plugins:

abstract class PluginBase extends ComponentPluginBase {
  use StringTranslationTrait;
  use DependencySerializationTrait;
  use MessengerTrait;
}

https://git.drupalcode.org/project/drupal/-/blob/10.0.x/core/lib/Drupal/...

Let's remove those traits from Extra Field plugins to remove duplicates

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

hitchshock’s picture

Status: Needs work » Needs review

Agree that we need to use Drupal\Core\Plugin\PluginBase
This is important for D11+, and it is supported by D10

Switched base class for both types of plugins: ExtraFieldDisplayBase.php and ExtraFieldFormBase.php and did a small code clean up

arousseau’s picture

Title: Provide autowiring through ExtraFieldDisplayBase » Provide autowiring through ExtraFieldDisplayBase and ExtraFieldFormBase
Issue summary: View changes
arousseau’s picture

Thank you hitchshock, I updated the issue summary and title to reflect the changes.

pcambra’s picture

Are we sure this is the correct change?

hitchshock’s picture

@pcambra why not?
Most of Drupal plugins extend Drupal\Core\Plugin\PluginBase because of autowiring, e.g. Block plugins and others. It allows to use dependency injections easier. So I don't see any reason to ignore this

pcambra’s picture

Apologies, that's not what I was trying to say, the changes I see in https://git.drupalcode.org/project/extra_field/-/merge_requests/34/diffs are mostly removing stringtranslationtrait?

idebr’s picture

The removed traits are redundant because Drupal\Core\Plugin\PluginBase already uses them, see #6

pcambra’s picture

Yeah but those files have no other changes, does PluginBase already use them in Drupal 10? I'd like not to introduce breaking changes if we can avoid them

hitchshock’s picture

@pcambra everything is ok, even Drupal 10.0.x has traits I cleaned up
https://git.drupalcode.org/project/drupal/-/blob/10.0.x/core/lib/Drupal/...

pcambra’s picture

Status: Needs review » Fixed

Thanks all for the clarifications :)

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.

Status: Fixed » Closed (fixed)

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