When GET:ting data for nodes like /jsonapi/node/article the referenced images are correctly appearing in response so we get:
"field_image": {
"data": {
"type": "file--file",
"id": "1c563a12-6bb7-4aa0-be83-d2ba62c420cc",
"meta": {
"alt": "test",
"title": "",
"width": 320,
"height": 240
}
}But if the entity type has a default image the file reference data is:
"field_image": {
"data": null,
...
Reproduce:
1. Create an article with an image. => Request works.
2. Set a default image to article image field.
3. Create a new article using the default image. => Request gives null value to field_image.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 3005528-4-do-not-test.patch | 4.32 KB | wim leers |
Issue fork drupal-3005528
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
wim leersFascinating! Working to reproduce this.
Comment #3
wim leersThis is a core bug. Great find!
Found the root cause: the default image is only used by the field formatter, whereas it should be computed by the field itself.
\Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase::getEntitiesToView()does this:But we need
\Drupal\image\Plugin\Field\FieldType\ImageItem'slist_classto compute this.Comment #4
wim leersI think something like this is what we want. Untested patch.
Comment #5
tipit commentedWell, that was fast!
"3005528-4-do-not-test.patch" OK, I won't. :)
Comment #6
wim leersYou can test it, but IDK if it will work, I didn't actually run the code. The
-do-not-testsuffix is only there to ensure DrupalCI doesn't run automated tests :)Comment #7
effulgentsia commentedDoes this bug exist for the default values of other core field types? Or only images?
Comment #8
wim leersGood question. I quickly checked, and yes, the same problem exists there too.
In the general case, the UI text for this is: — in
\Drupal\field_ui\Form\FieldConfigEditForm::form().So at least that somewhat implies it only applies at content creation time, i.e. that it's not dynamically computed. Still, that either ought to apply to entities created through the REST API also, or the UI text should be updated to clarify it only sets form defaults.
Apparently this setting is disconnected from the
setDefaultValue()andsetDefaultValueCallback()operations on field definitions.Generalizing. Good catch, @effulgentsia!
Comment #9
tipit commentedThe patch did not quite work because I get:
when navigating to /node/add/article. I don't think the patch is good enough
from the performance perspective because I also needed to pump up the max
execution time to over 60s.
What I also noticed depending on which node type I try to create e.g.
/node/add/habit the error line.
E.g. habit gives in
web/core/lib/Drupal/Core/TypedData/ComputedItemListTrait.php on line 34Comment #10
wim leersThanks for trying the patch! (I also unpublished your comment at #2958554: Allow creation of file entities from binary data via JSON API requests about this, to keep that issue focused).
This is very much a PoC patch, so I understand that it doesn't work.
Comment #11
tipit commentedYou did everything right - again. So thank you - again.
Comment #13
maseyuk commentedI've just tried the patch out and it looked like the isEmpty() check in core/modules/image/src/Plugin/Field/FieldType/ImageItemList.php was causing the loop. I just removed that and everything seems to be working as hoped. (Even without the isEmpty check if a genuine image has been uploaded its not overwritten by the default which I thought might happen without the isEmpty check)
I've not tested with the original posters jsonapi issue I was testing the related issue to do with image field tokens and the metatags module not showing the default image value when no image has been uploaded
Comment #18
hudriIMHO the default value should not be computed, but stored. The problem with computed default values - no matter if on field widget level or if in FieldItemList - is that a user can't nullify a computed default value. Currently you can override a default image, but it is not possible to remove a default image on a single instance of a content entity. Please see the referencing issue #3250680
Comment #23
ro-no-lo commentedIs there any plan to fix this for Drupal 9.4? Or could be work around with a jsonapi_default_image module?
Comment #24
ro-no-lo commented@wim-leers: is it also possible to fix it via a module which hacks into the json:api request, thus I can _easily_ provide default images? I don't want to patch every Drupal core all the time.