53

OK, I finally figured out how to add (or edit) form controls through the form API in Drupal.

But now, I would like to add text to a form. Not a <textarea>, and not a <textfield>; both of which can be edited by the user. I would like to add <p>static text</p>, written by me, to be read by the user.

How would one do that?

Jarek
  • 25
  • 4
Ivo Renkema
  • 2,188
  • 1
  • 29
  • 40

1 Answers1

99

You can do this using #markup:

$form['some_text'] = array(
  '#markup' => '<p>Some text</p>'
);
Clive
  • 36,918
  • 8
  • 87
  • 113
  • 1
    I'm pretty sure the `#type` line isn't necessary if you are using render arrays. But it doesn't hurt to keep it in there. – theunraveler Dec 06 '11 at 20:57
  • @theunraveler: Yep you're right I just put it in there for demonstration – Clive Dec 06 '11 at 21:40
  • The default type in Drupal 7 is "markup", not "item" as used in this answer. If you want to use "item", you need to explicitly set `#type`. – apaderno Dec 07 '11 at 18:29
  • 8
    Also, for really short strings, or passing in a variable with markup in it, you can get by with `$form['some_text']['#markup'] = 'HTML';`, saving two lines of code :) – geerlingguy Apr 09 '14 at 01:32
  • I found people suggesting using prefix or suffix on an element before or after the desired markup, but this is clearly the best option – Cory Baumer Sep 07 '14 at 19:25
  • This adds a

    inside the submit button container widget of my views exposed form, which is not what I want.

    – cdonner Dec 17 '14 at 01:31
  • But this is: $form['#prefix'] = '

    Some text

    ';
    – cdonner Dec 17 '14 at 01:41
  • Is this supposed to go in template.php? It doesn't seem to do anything. I'm using correct form ID (replacing - with _ but still nothing at all) – Matt Saunders Jan 22 '16 at 15:25