2

I have a form with a text field which looks like this:

$form['myTextField'] = array(
    '#title' => 'Write here',
    '#type' => 'text_format',
    '#format' => 'filtered_html',
    '#description' => "Description goes here")
);

Now I would like the description to appear bewteen the title and the field, so I tried the technique described here. It works for all the other fields, but not for this one. I can make it work by changing the above code to this:

$form['myTextField'] = array(
    '#title' => 'Write here',
    '#type' => 'textarea',
    '#description' => "Description goes here")
);

The downside of this approach is that I end up with a plain old textarea instead of a text_format, which is what I need.

Any ideas as to why the description appears below the field rather than above it when I use '#type' => 'text_format' as opposed to '#type' => 'textarea'?

1 Answer 1

3

text_format is not a single form element that is rendered like textarea, textfield checkbox etc. Instead, it is several form elements (you get two form elements, one to select the filter, and a textarea to enter the actual text).

What you need to do is use the #after_build property to define a function that will enable you to alter the element after it has been built (the two form elements has been created). You can define custom theme function here and move data around in order to output the description and other data exactly how you want to.

Look at the FAPI documentation for more info.

Update

The #after_build on text_formats works a bit differently than expected, as it gets called twice. What you could is:

$form['text_format'] = array(
  '#type' => 'text_format',
  '#title' => t('Body'),
  '#my_description' => t('The text you enter here appears on the page.'),
  '#after_build' => array('after_build'),
);

And then

function after_build($element) {
  if ($element['#type'] == 'textarea') {
    $element['#description'] = $element['#my_description'];
  }
  return $element;
}

The idea is that you create a value for the fake description and convert it to the real description on the text area. What will happen with the normal description value is that is will be added to the filter form element only which is why your theme overrides doesn't work.

2
  • I understand that #after_build allows me to call a function once the form element is built, and that the function I call will be sent the form element as an argument. What I still don't understand is how to have that function place the description where I want it. Would you have any literature on this subject?
    – Shawn
    Oct 26, 2012 at 19:17
  • @Shawn Updated answer with greater detail.
    – googletorp
    Oct 27, 2012 at 9:20

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.