Exclude plugin types where the provider module is not installed - avoid "class not found" errors

Created on 21 April 2024, 9 months ago

Problem/Motivation

The PluginTypeManager collects plugin types declared in the *.plugin_type.yml files.
Modules can use these files to declare plugin types on behalf of other modules. The 'provider' key indicates which other module actually provides the plugin type.
Currently, it includes plugin types even where the provider module is not installed.

Some modules declare plugin classes for plugin types where the provider may or may not be enabled.
The idea is that these will simply be ignored if the respective module is not installed.
Such a plugin class might extend a base class or implement an interface that is only present if the provider module is installed.

E.g. we have a module with a `class MyCustomAddressFormatter extends AddressDefaultFormatter` that depends on 'address' module.

The 'plugin' module will attempt to include plugin files for all plugin types, even those where the provider does not exist.

Steps to reproduce

Install plugin module.
Create a custom module.
Define a custom plugin class `MyCustomAddressFormatter extends `\Drupal\address\Plugin\Field\FieldFormatter\AddressDefaultFormatter` in namespace `Drupal\MYMODULE\Plugin\Field\FieldFormatter`.
Create an entity bundle (I am trying this with media, but a node type will also work I suppose).
Add a field, using one of the plugin type fields, e.g. "field formatter".

Expected: All good, you can create the field.
Actual: "Error: Class "Drupal\address\Plugin\Field\FieldFormatter\AddressDefaultFormatter" not found in include() (line 23 of [...]/MYMODULE/src/Plugin/Field/FieldFormatter/MyCustomAddressFormatter.php)."

Proposed resolution

Filter out plugin types where the provider module is not installed, in PluginTypeManager.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Closed: works as designed

Version

2.0

Component

Code

Created by

🇩🇪Germany donquixote

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

Comments & Activities

  • Issue created by @donquixote
  • 🇩🇪Germany donquixote

    Actually.... I think I am wrong on this one.
    The plugin type is just "FieldFormatter", nothing to do with address module, so there is something else wrong here.

  • 🇩🇪Germany donquixote

    So....
    AnnotatedClassDiscovery actually uses StaticReflectionParser instead of loading the file directly.
    This way it can avoid loading a class where the base class might not be defined.
    It then adds the plugin definition, without ever loading the respective class.
    The plugin is never used only because the field type 'address' does never occur while 'address' module is not installed.

    The 'plugin' module does include the plugin classes for all formatters, because the plugin field does not (and cannot) filter them by field type.

    So, not sure if anything needs to be done here.

  • Status changed to Closed: works as designed 9 months ago
  • 🇩🇪Germany donquixote

    I think we don't need to do anything here, at least not for the scenario that I encountered.

Production build 0.71.5 2024