0

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

Example:

$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:

<div>
FORM ELEMENT
FORM ELEMENT
FORM ELEMENT
</div>

Instead it is outputting something like this:

<div></div>
FORM ELEMENT
FORM ELEMENT
FORM ELEMENT

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 Apr 3 '12 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 Apr 3 '12 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 Apr 3 '12 at 12:48
2

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)

| improve this answer | |
  • Probably needs '#type' => 'markup' as well. – Beebee Apr 3 '12 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 Apr 3 '12 at 12:52
  • '#type' => 'markup' is the default type in D7, so no need. – Chapabu Apr 3 '12 at 12:52
  • 1
    @Baysaa It doesn't, if no #type is provided Drupal defaults to using markup – Clive Apr 3 '12 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 Apr 3 '12 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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