Problem/Motivation
If I 'wrap' my element into text_format element, the original description_display has no effect anymore. I think this is because text-format-wrapper.html.twig (and maybe the preprocess function) does not take it into account.
See #314385: Make position of #description configurable via the API
Steps to reproduce
- Install Drupal core (e.g. version 10.3.x), standard profile.
- Configure the 'body' field on 'basic_page' node type to define "Help text" (aka the description) at
/admin/structure/types/manage/page/fields/node.page.body - Use
hook_preprocess_form_element()to set$variables['description_display'] = 'before';. - Clear all caches.
- Visit node/add/page
- Behold that the description is still below the text area, not between the title and the text area as expected
Proposed resolution
Remove broken logic in text-format-wrapper.html.twig that tries to handle the description and forces it to happen at the end. Allow form-element.html.twig itself to handle description (which it already does correctly) when the child elements are being rendered.
Remaining tasks
- Decide the scope of the fixes we want (see comment #30)
- Decide what test coverage is needed (if any)
- Reviews / refinements
- RTBC
- Commit
User interface changes
Setting #description_display in a render array of #type 'processed_text' and setting description_display as a theme variable for formatted text fields now works.
API changes
None.
Data model changes
None.
Release notes snippet
Using the 'description_display' theme variable on formatted text area form elements now works. Previously, the description was always rendered last, regardless of this setting.
| Comment | File | Size | Author |
|---|
Issue fork drupal-2421445
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 #7
bgilhome commentedIn the meantime, some template hackery can get this achieved, editing text-format-wrapper.html.twig to:
If used in a module, you'll need hook_theme_registry_alter() to set the path to it.
Comment #9
christian_m commentedBgilhome's hack is a nice trick.
But as I have multiple tags among the {{ children }}, I had to tweak it a bit to target only the first one:
Otherwise, it does exactly what I need: it allows me to insert the description right after the first label.
Thanks a lot!
Comment #11
cegri commentedBgilhome's hack, and christian's tweak, helped me make my node edit forms collapsible with the details tag.
I remove the first
<label>and use it for the summary tag. (I should also check if the label exists.)BUT, I'm sure there should be an easier, drupal friendly, way to do this. I notice, the image field type already uses a details tag with details.html.twig. (Maybe that's because it's the only field in my site with multiple values.) Any suggestions?
Comment #12
gantal commentedTagging for DrupalCamp Colorado's upcoming contrib day.
Comment #13
dwwRan into this. It's sort of a mess. Even if the text-format-wrapper.html.twig wanted to deal with this properly itself, it couldn't. By the time we hit that wrapper, the children elements have been rendered and glued together.
I believe the safest way to handle #description_display = 'before' is to *move* those attributes into the 'value' sub element. This seems to work well in local testing.
Before we can commit this, it needs tests. Also curious about the BC implications of this, and if this approach will fly. But uploading something to get us started. ;)
Thoughts?
Thanks!
-Derek
Comment #14
sagannotcarl commented@dww any chance you could share what other steps were required to make this patch work? Did you have to override any preprocess functions?
I can't get this patch to work and I feel like I'm missing some important step. Thanks!
Comment #15
dww@sagannotcarl - Shouldn't need much. Apply patch, clear caches. However, the initial patch still doesn't support the 'invisible' case. It's only fixing 'before'. If you're trying to use 'invisible', it'll still be appearing. To fix that, we'd also need to start changing the core Twig templates, which is more of a mess. ;)
Comment #16
sagannotcarl commented@dww Awesome got it working. It is the 'before' I'm after so this was perfect.
So now between this patch and #2396145: Option #description_display for form element fieldset is not changing anything I can pull my hair out a little less trying to get descriptions in the right place.
Comment #18
tjh commentedCreated this patch with a slightly different approach than #13. Removing description from the excluded keys list lets it be handled by the child form-element template where description_display logic lives. This was tested with 'before' and 'invisible' values and seemed to work okay.
Comment #19
tjh commentedForgot to attach the patch with last comment, sorry
Comment #20
seanbThe approach in #19 seems to work for me. Nice!
Should we update all the
text-format-wrapper.html.twigtemplates to the new approach?Comment #23
smustgrave commentedMoving back to needs work based on missing tests and some open questions.
Comment #25
nicolas bouteille commented+1 it is very hard to be able to display the description between the label and textarea for formatted text fields when description is removed in TextFormat.php processFormat() $keys_not_to_copy
This new approach seems better
Comment #26
steven jones commentedSorry to do this, but I've fixed a warning/notice coming from #13.
The other approach in #19 is probably better, but I don't have time to progress that solution, sorry!
Comment #27
solideogloria commentedIssues need to target 11.x. Also, I'm pretty sure 11.x requires merge requests. So creating new patches won't get us anywhere.
Comment #29
dwwRan into this again on another project. 😅 I agree #19 is better, all it does is remove code! Local testing and it's working fine.
We probably still want tests, so leaving NW and tagged, but I converted #19 into MR !7218 to move this along.
Also, this summary needs the real template and to be filled in.
Comment #30
dww#20 makes sense. I started removing description and related variables from most of these:
I ran into comments with @see pointing to #3016343: Fix inconsistencies of TextareaWidget and TextareaWithSummaryWidget form elements, so adding that as related.
I'm not sure what scope of a fix is appropriate. Currently, the MR only fixes Claro. I know we never touch stable9.
We can fix Olivero, starterkit_theme and demo_umami. But I'm worried about losing functionality if we completely remove all these lines:
form-element.html.twigitself doesn't add a separateis-disabledclass to the description classes, it addsform-item--disabledto the whole wrapper.is-disabledclass ondescription_classestoform-element.html.twig?modules/filter/templates/text-format-wrapper.html.twigitself?aria_descriptionfrom these templates, since we're not using that anymore?* - attributes: HTML attributes for the containing element.yet they all do something like this this for the containing element:In practice,
attributesis only used for the description:That seems like a separate bug. Sound familiar? Needs follow-up?
For now, I pushed separate commits for:
aria_descriptionfrom all of themWe can easily revert anything if needed. Curious to see what the bot thinks of all this. Probably some classy test is going to fail about template file hashes changing. 😅
Comment #31
dwwRe: #30.4: Reading more closely, that's a big part of what #3016343: Fix inconsistencies of TextareaWidget and TextareaWithSummaryWidget form elements proposes to fix. So that doesn't need another issue. Hopefully this helps reviewers understand why we're leaving the templates alone in this regard. Perhaps we should preserve the @see comments pointing to #3016343?
Meanwhile, gave this a real summary.
Comment #32
dwwA few more edits to make the summary more accurate
Comment #33
dwwFor tests, we could mostly copy
modules/system/tests/src/Kernel/Form/ElementsFieldsetTest.phpintoElementsProcessedTextTest.phpor something (perhaps broaden it to also includetextarea?). It feels a little gross to copy/pasta, but for now we're still at N=2 and haven't necessarily hit the Rule of 3. At least not that I'm seeing trying to grep the core code. 😅Comment #35
smustgrave commentedCame here while triaging my queue on #3016343: Fix inconsistencies of TextareaWidget and TextareaWithSummaryWidget form elements
Readying the summary and something isn't clicking why we need to remove all the description? Maybe #18 can be captured in summary?