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

  1. Install Drupal core (e.g. version 10.3.x), standard profile.
  2. 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
  3. Use hook_preprocess_form_element() to set $variables['description_display'] = 'before';.
  4. Clear all caches.
  5. Visit node/add/page
  6. 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

  1. Decide the scope of the fixes we want (see comment #30)
  2. Decide what test coverage is needed (if any)
  3. Reviews / refinements
  4. RTBC
  5. 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.

Issue fork drupal-2421445

Command icon 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

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

bgilhome’s picture

In the meantime, some template hackery can get this achieved, editing text-format-wrapper.html.twig to:

<div class="js-text-format-wrapper js-form-item form-item">

    {% set parts = children |split('</label>') %}

    {{ parts.0 |raw }}</label>

    {% if description %}
        <div{{ attributes.addClass('form-item__description') }}>{{ description }}</div>
    {% endif %}

    {{ parts.1 |raw }}
</div>

If used in a module, you'll need hook_theme_registry_alter() to set the path to it.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

christian_m’s picture

Bgilhome'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:

<div class="js-text-format-wrapper js-form-item form-item">

    {% set parts = children |split('</label>', 2) %}

    {{ parts.0 |raw }}</label>

    {% if description %}
        <div{{ attributes.addClass('form-item__description') }}>{{ description }}</div>
    {% endif %}

    {{ parts.1 |raw }}
</div>

Otherwise, it does exactly what I need: it allows me to insert the description right after the first label.
Thanks a lot!

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

cegri’s picture

Bgilhome'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?

<details class="js-text-format-wrapper text-format-wrapper js-form-item form-item">
  {% set parts1 = children |split('<label', 2) %}
  {% set parts2 = parts1.1 |split('>', 2) %}
  {% set parts3 = parts2.1 |split('</label>', 2) %}

  <summary>{{ parts3.0|raw }}</summary>
  {{ parts1.0|raw }}
  {{ parts3.1|raw }}
</details>
gantal’s picture

Issue tags: +DCCO2020

Tagging for DrupalCamp Colorado's upcoming contrib day.

dww’s picture

Status: Active » Needs review
Issue tags: +Bug Smash Initiative, +Needs tests
StatusFileSize
new1.42 KB

Ran 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

sagannotcarl’s picture

@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!

dww’s picture

@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. ;)

sagannotcarl’s picture

@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.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

tjh’s picture

Created 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.

tjh’s picture

StatusFileSize
new1.24 KB

Forgot to attach the patch with last comment, sorry

seanb’s picture

The approach in #19 seems to work for me. Nice!
Should we update all the text-format-wrapper.html.twig templates to the new approach?

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work

Moving back to needs work based on missing tests and some open questions.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

nicolas bouteille’s picture

+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

steven jones’s picture

StatusFileSize
new1.47 KB
new795 bytes

Sorry 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!

solideogloria’s picture

Version: 9.5.x-dev » 11.x-dev

Issues need to target 11.x. Also, I'm pretty sure 11.x requires merge requests. So creating new patches won't get us anywhere.

dww’s picture

Title: Text format wrapper does not take description_display into account » Text format wrapper does not honor description_display
Issue tags: +Needs issue summary update

Ran 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.

dww’s picture

Parent issue: » #314385: Make position of #description configurable via the API
Related issues: +#3016343: Fix inconsistencies of TextareaWidget and TextareaWithSummaryWidget form elements

#20 makes sense. I started removing description and related variables from most of these:

% find . -name text-format-wrapper.html.twig
./core/profiles/demo_umami/themes/umami/templates/classy/content-edit/text-format-wrapper.html.twig
./core/modules/filter/templates/text-format-wrapper.html.twig
./core/themes/stable9/templates/content-edit/text-format-wrapper.html.twig
./core/themes/claro/templates/text-format-wrapper.html.twig
./core/themes/starterkit_theme/templates/content-edit/text-format-wrapper.html.twig
./core/themes/olivero/templates/text-format-wrapper.html.twig

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:

-  {% if description %}
-    {%
-      set description_classes = [
-        aria_description ? 'form-item__description',
-        disabled ? 'is-disabled',
-      ]
-    %}
-    <div{{ description_attributes.addClass(description_classes) }}>{{ description }}</div>
-  {% endif %}

form-element.html.twig itself doesn't add a separate is-disabled class to the description classes, it adds form-item--disabled to the whole wrapper.

  1. Do we care? Can/should we move this is-disabled class on description_classes to form-element.html.twig?
  2. Can we fix modules/filter/templates/text-format-wrapper.html.twig itself?
  3. Should we be removing all mention of aria_description from these templates, since we're not using that anymore?
  4. While we're at it, the module template and most of the themes have a comment about * - attributes: HTML attributes for the containing element. yet they all do something like this this for the containing element:
    <div class="js-text-format-wrapper text-format-wrapper js-form-item form-item">
    

    In practice, attributes is only used for the description:

      {% if description %}
        <div{{ attributes }}>{{ description }}</div>
      {% endif %}
    

    That seems like a separate bug. Sound familiar? Needs follow-up?

For now, I pushed separate commits for:

  • Olivero
  • starterkit_theme
  • demo_umami
  • modules/filter/templates/text-format-wrapper.html.twig
  • Removing aria_description from all of them

We 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. 😅

dww’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

Re: #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.

dww’s picture

Issue summary: View changes

A few more edits to make the summary more accurate

dww’s picture

For tests, we could mostly copy modules/system/tests/src/Kernel/Form/ElementsFieldsetTest.php into ElementsProcessedTextTest.php or something (perhaps broaden it to also include textarea?). 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. 😅

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

smustgrave’s picture

Came 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?