The "Enable Description field" setting for a file field instance (as defined in Drupal\file\Plugin\Field\FieldType\FileItem::fieldSettingsForm()) cannot be set via the UI. If this option is checked (set TRUE) it will not be preserved.
To reproduce:
- Install site instance and enable "node", "field_ui" and "file"
- Create a new node content type and add a field of type "file" (e.g., field_file) to it.
- When configuring the file field's instance settings, check the "Enable Description field" option and save.
The expected behavior is that a "description" (sub)field will appear while uploading a file to the field_file field (when creating a new node), which can be populated with a string. This option does not appear. In addition, when navigating back to the field instance settings form for field_file, the "Enable Description field" is not checked. Re-checking it and re-saving always leads to this value being unset.
Solution:
It looks like the description_field form element that's defined in Drupal\file\Plugin\Field\FieldType\FileItem::fieldSettingsForm() contains a "#parents" value, which seems to have been carried over from this form element's definition in D7. I believe that this "#parents" value simply needs to be removed for the setting to property get saved.
Note that overall behavior of this description field seems to have test coverage (in \Drupal\file\Tests\FileFieldDisplayTest::testNodeDisplay()), but in that test the description_field value is set with entity_create(), as opposed to the field creation forms, so the behavior of the form itself is not tested.
| Comment | File | Size | Author |
|---|---|---|---|
| #24 | file_field-enable_description-2350683-24-TEST_ONLY.patch | 2.28 KB | rjacobs |
| #24 | file_field-enable_description-2350683-24.patch | 3.04 KB | rjacobs |
Comments
Comment #1
rjacobs commentedHere's a simple patch to remove the "#parents" value as noted in solution part of the OP.
Should I also add a test for this? The problem is isolated to the form definition so I'm not sure if that justifies a dedicated test method. If as test is needed, would it be bundled for inclusion in the permanent test cases, or just used temporarily to help rectify this specific issue?
Comment #4
rjacobs commented#2364701: Description field for file field can't be enabled is marked as a dup. The same patch was proposed in that issue as well.
Comment #6
rjacobs commentedOk, I went ahead and whipped-up a test to demonstrate this programmatically. I'm attaching a test only version along with a version that includes the test and the patch. By no means do I think the test should be part of a commit (the patch-only file from #1 is still the candidate solution), this is just to hopefully streamline a review process so as to get this RTBC.
Comment #14
rjacobs commentedJust my luck, the testbot was malfunctioning when I submitted my tests. Anyway, it seems to be alright now and after a test re-submission the results are as expected.
For ongoing reference regarding the core issues queues I would still be interested to know if these kinds of simple UI issues actually warrant a test. I'd like to make things as easy to review as possible, but it seems to me that a 1-liner FAPI oversight like this may not actually justify a supporting test?
Comment #15
benjy commentedType, "field field"
I think what you want here is $this->drupalGetNodeByTitle()
Un-needed white space.
EDIT: Sorry, I should have answered your question, we do usually add tests for regressions such as this.
Comment #16
rjacobs commentedOk, I guess that makes sense. I'll clean-up the test a bit based on your comments. Thanks.
Comment #17
rjacobs commentedOk, here are some re-rolls that factor in comments from #15. Thanks for those notes.
I'm still of the mindset that committing the test could be unnecessary, but either way I wanted to get it updated accordingly.
Comment #19
arla commentedLooks good. But I would guess that the cache-clears in the test are not needed.
Comment #20
berdirYes, it's not. Because there are no such caches.
Config entities are only cached as config objects and automatically invalidated already. config entities have no static or persistent cache unless they explicitly opt-in, and those two don't.
Comment #21
rjacobs commentedRight-o. Here's a version with the cache clears removed. I also needed to modify the name attribute used to target the description field in the test (
field[settings][description_field]tosettings[description_field]) as it looks like some form element nesting changed in core since the last patch.Comment #23
berdirSorry, didn't see this before.
We usually don't shorten variable names, I'm also not sure why we even need the inst prefix?
Why not just $node_type = 'test' and $field_type and $field_name?
Comment #24
rjacobs commentedYour absolutely right. The "inst" prefix was just something I was in the habit of using on contrib tests. Anyway, when I whipped-up this test I really didn't think it would be considered for inclusion along with the fix (I was just trying to validate the issue programmatically). It didn't become clear until #15 that these kinds of very simple UX regressions/oversights would also be candidates for a dedicated core test method. Long story short, my initial test may have been a bit sloppy.
I've made some adjustments to (hopefully) get things more in-line with other core test naming conventions. I'm now using the same names found elsewhere in FileFieldDisplayTest.php.
Comment #26
arla commentedA trick I recently learned: Upload test-only patch before real patch to avoid this kind of "Needs work" status change.
Comment #27
rjacobs commented@arla, thanks for that tip (and for resetting the correct status). I'm actually surprised that quirk of d.o. has not tripped me up more in the past.
Comment #28
berdirThanks, looks good now I think.
This is a plain bug fix that has no impact on an other code so I don't think we need a beta evaluation for this?
Comment #29
alexpottThis issue is a normal bug fix, and doesn't include any disruptive changes, so it is allowed per https://www.drupal.org/core/beta-changes. Committed e1c2401 and pushed to 8.0.x. Thanks!