Problem/Motivation
Passing NULL as plugin ID to the ::createInstance() of a plugin manager with fallback plugin worked until now and the fallback plugin was instantiated. With a fallback plugin manager, passing NULL as plugin ID should not be so uncommon. Designing a fallback plugin manager is often made for such cases. Just imagine the plugin ID is stored in nullable config property. If it was not configured you want to get the fallback plugin.
But with PHP 8.5 a deprecation error is emitted:
Deprecated function: Using null as an array offset is deprecated, use an empty string instead in Drupal\Core\Plugin\DefaultPluginManager->doGetDefinition() (line 45 of core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php)
Steps to reproduce
Call ::createInstance(NULL) on a plugin manager with fallback.
Proposed resolution
Fix.
Remaining tasks
None.
User interface changes
None.
Introduced terminology
None.
API changes
None.
Data model changes
None.
Issue fork drupal-3581628
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 #2
claudiu.cristeaComment #5
andypostAdded quick-fix and test
Comment #6
andypostComment #7
claudiu.cristeaI still think this is a bug, as without a plugin ID we should get the fallback plugin. At least, for me, this was the obvious usage of the fallback interface.
The fix is simple and tested, RTBC
Comment #8
alexpottI'm not sure. There's no code in core calling with a NULL and for me the fallback is about handling the case when you have have configured plugin that disappears for some reason so plugin ID would not be NULL in this case. Do we have a concrete example of this happening in contrib? Otherwise we're just adding code that goes against the documentation... ie.
Our docs say $plugin_id is a string - so don't pass a NULL to it.
Comment #9
claudiu.cristea@alexpott, a plugin that disappears is a use case. My understanding was that a plugin not configured somewhere in a config is other case. Why only disappearing plugins are covered? My first thought when a saw the fallback interface was that it addresses this scenario: "if you don't have a plugin we're covered, we give you the fallback".
But I'm OK to solve the problem in the caller, not a big deal, and for now on to be careful with the fallback mechanism. Maybe FallbackPluginManagerInterface should implement a method ::createInstanceOrFallback() to cover the nullable case?
Comment #10
smustgrave commented@alexpott what do you think of the response above?