ExternalDataSource widgets break Dependency Injection

Created on 20 January 2022, almost 3 years ago
Updated 22 August 2023, over 1 year ago

Problem/Motivation

Current implementation of ExternalDataSourceSelectWidget and ExternalDataSourceCheckboxesWidget break dependency injection when writing own plugin and using this design pattern.

Steps to reproduce

* Write your own plugin (below called "LubriOrganizations") extending ExternalDataSourceBase, implementing ContainerFactoryPluginInterface (like any other plugin).
* Try to inject your own services
* Following error will be generated when adding External Data Source field:

[20-Jan-2022 21:45:51 Europe/Amsterdam] ArgumentCountError: Too few arguments to function Drupal\lubri_organizations_field\Plugin\ExternalDataSource\LubriOrganizations::__construct(), 0 passed in /Users/vinlaurens/Sites/advisor/web/modules/contrib/external_data_source/src/Plugin/Field/FieldWidget/ExternalDataSourceSelectWidget.php on line 126 and exactly 1 expected in ...

The error is caused by how ExternalDataSourceSelectWidget instantiates the plugin class. It currently does that as follows:

$pluginInstance = new $plugin_definitions[$settingPlugin]['class']();

While this would be desired to solve this issue:

$pluginInstance = $this->plugin_manager->createInstance($settingPlugin);

Proposed resolution

Inject the appropriate plugin manager.

Remaining tasks

Please apply and review the patch I will attach.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Needs review

Version

3.1

Component

Code

Created by

🇳🇱Netherlands vinlaurens Utrecht, The Netherlands

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024