Problem/Motivation
While create a new enhancer, if I use attributes to define it i got an error on both backend panel and json response. Defining it with annotations give no problems.
Steps to reproduce
Create a new enhancer
#[ResourceFieldEnhancer(
id: 'custom_enhancer',
label: new TranslatableMarkup('Custom enhancer'),
description: new TranslatableMarkup('Dummy for test only.'),
)]
class CustomEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {}then go to /admin/config/services/jsonapi/resource_types/node--page/edit
got this error: Uncaught PHP Exception TypeError: "Cannot access offset of type array in isset or empty" at /var/www/html/web/core/lib/Drupal/Core/Extension/ModuleHandler.php line 267
If defining like
/**
* @ResourceFieldEnhancer(
* id = "custom_enhancer",
* label = @Translation("Custom enhancer"),
* description = @Translation("Dummy for test only."),
* )
*/
class CustomEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {}
then no error happen
Proposed resolution
Change Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager.php
adding a check in the alterDefinitions() foreach
foreach ($definition_dependencies as $dependency) {
if (!is_string($dependency)) {
continue;
}
// If dependency is not enabled removed from list of definitions.
if (!$this->moduleHandler->moduleExists($dependency)) {
unset($definitions[$definition_key]);
continue;
}
}| Comment | File | Size | Author |
|---|---|---|---|
| #3 | jsonapi_extras-3584099-3.patch | 1.4 KB | bala_28 |
Issue fork jsonapi_extras-3584099
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 #3
bala_28 commentedRoot cause
In ResourceFieldEnhancerManager::alterDefinitions(), the code iterates over $definition_info['dependencies'] and passes each value directly to $this->moduleHandler->moduleExists(). However, plugin definitions using the #[ResourceFieldEnhancer] attribute declare dependencies as a flat array (e.g., dependencies: ['datetime']), but during plugin discovery the dependencies value can be structured as a keyed array (e.g., ['module' => ['datetime']]). When the value is a nested array, it gets passed as-is to moduleExists(), which calls isset($this->moduleList[$module]) — triggering a TypeError in PHP 8.1+ because an array cannot be used as an array offset in isset().
Fix
Flatten the dependencies array before iterating, so both flat (['datetime']) and nested (['module' => ['datetime']]) formats are handled correctly.
PS: The existing partial fix (the is_string check) was incorrect — it silently skipped array dependencies without validating them, so plugins with unmet nested dependencies would still be loaded.
Comment #4
nvandijk commentedI have tested https://www.drupal.org/files/issues/2026-04-22/jsonapi_extras-3584099-3.... against one of my projects. The routes no longer give a 500 error, but instead give the desired output.
Comment #5
bbralaI think i need to check if this change will break ealier versions.
Comment #6
spfaffly commentedRan into this issue this morning - loaded up the patch from #3 and the page loads as intended.
Thanks!
Comment #7
timfletcher commentedI tried Patch #3 and the error still appears. I verified the patch definitely applied and the changes appeared. I'm running jsonapi_extras 8.x-3.28.
I'm trying to access the 'Override' page for several entities, but found the error also fires when attempting to edit existing overrides. I'm also not defining any enhancers or plugins.
The full error is:
Comment #8
bbralaDid some testing and this fix will drop dependencies, which is not nice. The fix will be to actually move things around. I will keep a BC path for the enhancers but will emit a deprecation.
Comment #9
bbralaFixed, thanks all