Adding text (just text!) to a form in Drupal 7 [closed]
Asked Answered
S

1

54

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?

Scallion answered 6/12, 2011 at 19:8 Comment(1)
Belongs on drupal.stackexchange.comBloodcurdling
H
99

You can do this using #markup:

$form['some_text'] = array(
  '#markup' => '<p>Some text</p>'
);
Hydrometallurgy answered 6/12, 2011 at 19:37 Comment(8)
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.Waal
@theunraveler: Yep you're right I just put it in there for demonstrationHydrometallurgy
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.Marcosmarcotte
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 :)Testee
I found people suggesting using prefix or suffix on an element before or after the desired markup, but this is clearly the best optionMedina
This adds a <p> inside the submit button container widget of my views exposed form, which is not what I want.Secure
But this is: $form['#prefix'] = '<p>Some text</p>';Secure
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)Foreandaft

© 2022 - 2024 — McMap. All rights reserved.