Adding text (just text!) to a form in Drupal 7

39,146

You can do this using #markup:

$form['some_text'] = array(
  '#markup' => '<p>Some text</p>'
);
Share:
39,146
Ivo Renkema
Author by

Ivo Renkema

Building WebApps and/or Android Apps...

Updated on May 30, 2020

Comments

  • Ivo Renkema
    Ivo Renkema almost 4 years

    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?

  • theunraveler
    theunraveler over 12 years
    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.
  • Clive
    Clive over 12 years
    @theunraveler: Yep you're right I just put it in there for demonstration
  • apaderno
    apaderno over 12 years
    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.
  • geerlingguy
    geerlingguy about 10 years
    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 :)
  • Cory Baumer
    Cory Baumer over 9 years
    I found people suggesting using prefix or suffix on an element before or after the desired markup, but this is clearly the best option
  • cdonner
    cdonner over 9 years
    This adds a <p> inside the submit button container widget of my views exposed form, which is not what I want.
  • cdonner
    cdonner over 9 years
    But this is: $form['#prefix'] = '<p>Some text</p>';
  • Matt Saunders
    Matt Saunders over 8 years
    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)