Problem/Motivation
The module has several areas that need modernization to align with current Drupal 10.3+ / 11 best practices:
- Hook implementations live as procedural code in
.moduleinstead of using the OOP hook pattern. - The test plugin (
DummyType) uses the deprecated@Annotationdiscovery instead of PHP attributes. - The formatter outputs plugin labels with
#markup, which does not escape user-controlled text, posing a potential XSS risk. - PHPStan was running at level 1; several loose types (
@return mixed, loose equality==,static::calls on instance methods) would be caught at higher levels. - The
storageSettingsSummary()displays the raw machine name of the plugin type instead of its human-readable label. - The widget's
supportsConfiguration()does not guard against an empty definitions array, causing a potential error. - Attribute strings use double quotes instead of single quotes (minor style inconsistency).
- The module requires
^10but realistically depends on APIs available from 10.3+.
Proposed resolution
1. OOP hook migration
- Add
plugin_configuration_field.services.ymlwith thePluginConfigurationFieldHooksservice. - Move
hook_field_widget_info_alter()andhook_field_formatter_info_alter()logic into that class. - Keep thin procedural wrappers in
.modulefor backward compatibility with Drupal versions without#[Hook]auto-discovery.
2. Annotation to PHP attribute
- Replace the
@DummyTypeannotation class with aDummyTypePHP attribute in the test module. - Update
BasicDummyTypeto use the#[DummyType]attribute. - Delete the now-unused
Annotation/DummyType.php.
3. Stricter types and code quality (PHPStan level 5)
- Raise PHPStan level from 1 to 5.
- Fix loose equality (
==to===) inisEmpty(). - Change
getPluginManager()fromstaticto instance method. - Add proper
@returntype to the deriver'sgetEvent(). - Add
staticreturn type tocreate(). - Replace
get_class($this)withstatic::class. - Normalize attribute strings to single quotes.
- Add type annotations (
@var) in the widget for field items.
4. UX: human-readable label in storage settings summary
storageSettingsSummary()now dispatches the plugin types event to resolve the label instead of showing the machine name.
5. Widget empty-definitions guard
supportsConfiguration()returnsFALSEearly when definitions array is empty.
6. Core version requirement bump
core_version_requirementchanged from^10to^10.3 || ^11.
7. API documentation
- Add
plugin_configuration_field.api.phpdocumenting the types event hook.
8. Expanded test coverage
- New kernel tests:
PluginConfigurationFieldElementTest,PluginConfigurationFieldFormatterTest. - New functional test:
PluginConfigurationFieldWidgetTest. - New unit test:
PluginConfigurationFieldTypesEventTest. - Additional assertions in existing kernel test:
testIsEmpty(),testStorageSettingsSummaryShowsLabel(),testSetValueDefaultsConfiguration().
9. README rewrite
- Complete rewrite with architecture overview, registration guide, usage examples, and submodule documentation.
User interface changes
Storage settings summary now shows the human-readable plugin type label (e.g., "Reference type: Basic dummy type") instead of the machine name.
API changes
getPluginManager()changed fromprotected statictoprotected(instance method). Subclasses callingstatic::getPluginManager()must update to$this->getPluginManager().- New
plugin_configuration_field.services.ymlregisters the OOP hooks service. - New
plugin_configuration_field.api.phpdocuments the types event hook.
Data model changes
None. The field schema (two columns: target_plugin_id and target_plugin_configuration) is unchanged.
Issue fork plugin_configuration_field-3579691
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
Comment #4
facine commented