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