I'm not seeing my computed field show up in JSON:API output with computed_field 4.x.
I was previously using computed_field 2.x and the field was appearing as expected in JSON:API output.
I updated to computed_field 4.x and created a plugin following the TestString.php example in test_computed_field_plugins very closely. (My computed field outputs a string as well.) I created the field via the "Add computed field" process in the UI and attached my plugin to it.
The computed field output shows up properly when the node is rendered in the site theme, but not when viewing the JSON:API output for the node. The field is not listed there at all.
I also tried modeling my plugin after the version of TestString.php that's in test_computed_field_output, (though I'm not sure I got the attach exactly right), but still nothing in JSON:API output.
Should the field just work? Is there something more I need to do to get it to show up?
Thanks in advance.
| Comment | File | Size | Author |
|---|---|---|---|
| #24 | 3353839-24.patch | 4.54 KB | meeni_dhobale |
| #23 | 3353839-23.patch | 3.77 KB | genellann |
| #22 | 3353839-22.patch | 3.21 KB | rishvi |
| #12 | 3353839-12.patch | 2.5 KB | spurlos |
| #10 | 3353839-10.patch | 2.53 KB | spurlos |
Issue fork computed_field-3353839
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
jeffschulerI tried enabling the
test_computed_field_outputmodule and creating a computed field with each of its plugins. Same problem: they show up in the theme but not in JSON:API output.Comment #3
joachim commentedThat's weird, it should just work -- see #3252278: Complete JSON:API's handling of field (including computed & empty fields) cache metadata, https://gorannikolovski.com/blog/add-computed-field-jsonapi-response
Can you debug?
Comment #4
jeffschulerThanks @joachim. It does just work in 2.x & 3.x. Both those links you shared would be pre 4.x, no?
I downgraded back to 3.x to keep moving, but will revisit and dig further when time allows.
I could see the field listed in JSON:API Extras' Resource overrides pane for my entity (/admin/config/services/jsonapi/resource_types)... so it's being registered in some way.
Comment #5
joachim commented> Thanks @joachim. It does just work in 2.x & 3.x. Both those links you shared would be pre 4.x, no?
They are for computed fields in general, independent of this module.
Comment #6
daniel korteI’m experiencing the same issue. The field shows up everywhere except the JSON output.
Comment #7
daniel korteOkay, it worked for me if I defined my computed field in code only (i.e. not adding the computed field to an entity bundle via the Field UI):
Comment #8
joachim commentedIn that case, it it looks like this is a base field / bundle field problem.
Comment #9
lambic commentedI'm seeing the field in my jsonapi output, but it's only showing the first value. My computeValue method returns [1,2] but in the jsonapi I just see "1"
Comment #10
spurlos commentedThe root cause here comes from how JSON:API filters out internal properties.
By default,
\Drupal\jsonapi\JsonApiResource\ResourceObject::extractContentEntityFields()calls\Drupal\Core\TypedData\TypedDataInternalPropertiesHelper::getNonInternalProperties(), which relies on\Drupal\Core\Field\FieldConfigBase::isInternal()to determine whether a field should be exposed.That method checks the field definition for the internal key. If it’s missing, it falls back to
isComputed(). This means any computed field without an explicit definition is treated as internal and therefore excluded from the JSON:API output.Since
\Drupal\computed_field\Entity\ComputedFielddoes not provide a definition, it always ends up flagged as internal purely because it’s computed.To work around this, I patched
ComputedFieldso that it returns a definition from::getFieldStorageDefinition(), and I propagate theinternalflag from the plugin definition.internal = FALSE, because most field properties are intentionally public.This approach ensures that computed fields are no longer discarded automatically, while still respecting Drupal’s intent around the
internalflag.Comment #11
joachim commentedGood find!!
So do all computed fields from this module show in JSONAPI with this patch?
What's the magic getting needed for?
And also, could you make this a MR please?
Comment #12
spurlos commentedSmall adjustment to previous patch. Removed parent::_get() in ComutedField.php as it does not exists in FieldConfigBase
I'll create a MR when i'll have time.
Comment #15
redndahead commentedThis is my first MR so let me know if I need to do something different.
Comment #16
joachim commentedYeah, you've put your commits and the MR on the wrong branch.
Leave 4.0.x as is.
Put your commits on the feature branch which you've created -- computed_field-3353839/3353839-show-computed-field
Comment #17
joachim commentedAlso, a kernel test would be nice!
Comment #19
redndahead commentedHopefully I've done the MR correctly this time.
I don't think I'm the right one to write the test. I don't use this feature and I provided the MR as a low hanging fruit for me to learn how to create MR's and I believe if I post an issue I should at least try to help out in another issue.
Comment #20
rishvi commentedI tried to address the review feedback on MR !22 locally.
I’ll push the update to the issue fork if access is available, or open a follow-up MR if needed.
Comment #21
joachim commentedThis needs a test before it goes anywhere -- I'm frankly a bit confused by the interplay between plugin / config entity / field definitions.
Comment #22
rishvi commentedComment #23
genellann commentedI tried #22 and my computed fields still didn't show up in JSON:API.
The problem is that JSON:API asks the field definition whether the field is internal. The field definition never asks the plugin, it just uses the core default, and the core default hides all computed fields. So the new flag from #22 never gets checked.
I rerolled #22 and added an isInternal method to the field definition so it asks the plugin. Now computed fields created in the UI show up in JSON:API.
Tested on Drupal 11.3 with 4.0.0.
Comment #24
meeni_dhobale commentedTested the patch from #23 (
3353839-23.patch) on Drupal 11.x, PHP 8.3. Without it,isInternal()always returnsTRUEbecause the field definition never asks the plugin — so JSON:API strips every computed field no matter what the plugin actually wants. With it applied,isInternal()follows the plugin'sinternalattribute like it should, and the field shows up in JSON:API.@joachim asked for tests above, so here's one:
3353839-24.patchadds aComputedFieldInternalKernelTestcovering both cases — a plugin that doesn't mark itself internal, and one that does. Checked it fails without #23's patch and passes with it.One more thing: MR !22's branch has actually regressed since this was last tested. The "Remove fallback value" commit deleted
ComputedField::isInternal()entirely instead of just trimming it, so it's back to the same bug. It still needs at leastreturn $this->getFieldValuePlugin()->isInternal();.