4

For all textareas, I want to print the help text description above the field input.

By default, it looks like this:

  • Field Label
  • Field Input
  • Help Description

I want to arrange it like this:

  • Field Label
  • Help Description
  • Field Input

This was actually a hidden configuration option added to Drupal 8.0 So, the basic idea is that I have to set '#description_display' => 'before' for every textarea.

To do that, I tried to use template_preprocess_textarea().

I added the following code to mytheme/mytheme.theme:

function MYTHEME_preprocess_textarea(&$variables) {
  $variables['#description_display'] = 'before';
}

I checked to make sure this code is called with kint() but it doesn't change the positioning of the help descriptions on the forms. I checked my theme (Bootstrap's) templates and they have logic for handling before and after. So what is the right way to call this to adjust the position of the help text on all textareas?

1
  • 1
    Can't test it right now, but have you tried using HOOK_preprocess_form_element instead of preprocess_textarea?
    – Hudri
    Nov 20, 2017 at 17:01

2 Answers 2

5
+50

The description_display property is still working if supported by your active theme and I understand it is. Adding this to your mytheme.theme should do the trick :

function MYTHEME_preprocess_form_element(&$variables) {
  if ($variables['element']['#type'] == 'textarea') {
    $variables['description_display'] = 'before';
  }
}

Note that this might not work with formatted fields from the field API as the description field is in a parent template, see text-format-wrapper.html.twig. It would be a little more complicated to have this working for those fields.

3
  • If anyone else needs this simple approach, we created a module shell using the hook_preproccess_form_element approach. I set it up as a module instead of a theme hook, as we tend to use admin themes. Feel free to save yourself some time and use/modify/etc: bitbucket.org/idfivellc/idfive_move_description/src Dec 21, 2018 at 17:04
  • Hi. Will this work on ECK fields ? Mar 13, 2020 at 12:18
  • This works great in Drupal 9! However, it doesn't work for Select Other webform fields, as both input elements are in a fieldset with the description after them.
    – mbomb007
    Oct 24, 2022 at 18:26
1

There is a module for this, which supports Drupal 7,8,9,10+

https://www.drupal.org/project/top_description

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.