Form API is magnificent. I love the idea of having to write as little markup as possible and having the framework render the most of the gritty HTML. This ensures that developers come and go but the high standards the Drupal community has set for it’s code, are upheld.
Moving along, today’s problem is as irritating as it is simple. I had to insert some default text into a textarea field in Drupal 7. And obviously I was using Form API. However, the tricky part was that I had to insert line breaks in the default text.
Typically, this is very simple to do in pure HTML/PHP. You simply incorporate the line breaks into your markup itself:
In Drupal though, you have to make do with the #default_value
parameter. Now you’d think that a \n
or \n\r
would suffice. But those two just refuse to do anything—Drupal’s Form API spits them out as is. What you need is
. And while we are at it, a 	
would produce a tab.
Here’s what my code looked like:
And it produced results exactly the way I wanted it to: