I have a form where I need to loop through an array and display multiple form elements in a specific area.


$form_layout = 'Some html';
$form_layout .= '<div>';

foreach($something as $key => $value) {
    $form_layout .= 'some html';
    $form_layout .= 'some html';

    // I need this to be displayed in between the above and below code
    // in the layout
    $form['awesome_element'] => array(
        '#type' => 'textfield',
        '#size' => 5,
        '#value' => 0

    $form_layout .= 'some html';
    $form_layout .= 'some html';

$form_layout .= '</div>';
$form_layout .= 'Some html';

$form['some-other-awesomeness'] = array(
    // ...
    '#prefix' => $form_layout,

Doing it like the example puts the textfields after all the other page elements. I have tried using #prefix and #suffix but that breaks the layout even worse.

Any ideas on what I should do?

Edit: For clarity...

If I had layout elements such as a div tag I need my form elements, such as textfields, to be inserted in between those div tags.

So I need it to output something like this:


Instead it is outputting something like this:


Hopefully that is more clear, sorry if the above is confusing. Looking at code for the last 20 hours.

Edit2: added some visual stuff to make it easier to understand

  • 1
    I'm not sure if it's just me..but this doesn't seem to make much sense?!?
    – Chapabu
    Commented Apr 3, 2012 at 12:36
  • How would you like me to clarify? There is a layout, there are many form elements that are populated via an array which I extract with a foreach loop. I need those elements in between the layout elements.
    – rf43
    Commented Apr 3, 2012 at 12:39
  • No they are not the same... the div tag is ABOVE the form elements. I need the form elements IN BETWEEN the tags.
    – rf43
    Commented Apr 3, 2012 at 12:48

1 Answer 1


Could you do something like the following:

$form['openingdiv'] = array(
  '#markup' => '<div>'

//Do all your foreach stuff here

$form['closingdiv'] = array(
  '#markup' => '</div>'

(Sorry it's a bit simple..I'm tired :-p)

  • Probably needs '#type' => 'markup' as well.
    – Beebee
    Commented Apr 3, 2012 at 12:50
  • What is this 'markup'? I am going to go look this up now... I will let you know if that looks promising. Thank you! :)
    – rf43
    Commented Apr 3, 2012 at 12:52
  • '#type' => 'markup' is the default type in D7, so no need.
    – Chapabu
    Commented Apr 3, 2012 at 12:52
  • 1
    @Baysaa It doesn't, if no #type is provided Drupal defaults to using markup
    – Clive
    Commented Apr 3, 2012 at 12:53
  • 2
    Ok, this is totally my fault! Your answer is exactly what I would need for the question I asked. Unfortunately though, I have not asked the question properly. Instead of editing more and possibly becoming more confusing I will just accept this and re-ask the question after some sleep lol Thank you for the quick answer!!!
    – rf43
    Commented Apr 3, 2012 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.