tags:

views:

52

answers:

5

I've created a form that has the following structure

function myfunction(){

      $form['myform']['row1']['field1'] = array(
        //type, title, etc.
      ); 
      $form['myform']['row1']['field2'] = array(
        //type, title, etc.
      ); 
      $form['myform']['row1']['field3'] = array(
        //type, title, etc.
      ); 


      $form['myform']['row2']['field1'] = array(
        //type, title, etc.
      ); 
      $form['myform']['row2']['field2'] = array(
        //type, title, etc.
      ); 
      $form['myform']['row2']['field3'] = array(
        //type, title, etc.
      ); 
}

but I'd like to render it as a table in the following format with a head row if possible.

                       Col 1              Col 2                Col 3
----------------------------------------------------------------------           
Row 1                  Field 1            Field 2              Field 3
Row 2                  Field 1            Field 2              Field 3
Row 3                  Field 1            Field 2              Field 3

What format should I follow to get Drupal to render the form as a table?

P.S. Col 1 = select, Col 2 = checkbox, Col 3 = texfield and the very first column is the row # Row1, Row2, Row3.

Edit: doesn't drupal have something that you could give it a form and it will automatically produce that colored row, white row look?

+2  A: 

This should work. I'm not sure what's in your actual arrays (the part that's commented out), but this should display it all as a table. I'm using implode because I don't know what is in those arrays, but this will display it with a space between each item in the cell array.

echo '<table>';

echo '<tr><th></th><th>Cell 1</th><th>Cell 2</th><th>Cell 3</th></tr>';

foreach( $form['myform'] as $row )
{
    echo '<tr>';
    foreach( $row as $cell )
    {
        echo '<td>' . implode(' ',$cell) . '</td>';
    }
    echo '</tr>
}

echo '</table>';
hookedonwinter
Also I realize this isn't exactly pretty. If any of it doesn't make sense, let me know and I'll esplain.
hookedonwinter
Also of note, I don't know anything about drupal. So some of the other answers might be better suited for your situation.
hookedonwinter
+1  A: 

assume row1, row2 are fieldset.

If you want to theme form and place fields into table:

Define your form id (you can look it in html source of your form: type="hidden" name="form_id" value="YOURFORMID", it's should be like "YOURMODULENAME_myform").

Add this function in template.php (or add array if already function there)

function YOURTHEME_theme($existing, $type, $theme, $path) {
  $items[] = array(
    'YOURMODULENAME_myform' => array(
      'arguments' => array('form' => NULL),
      'template' => 'YOURMODULENAME_myform' 
        // This is file name of your themer of form
    ),
   ...
   return $items;


Add file into your theme: YOURMODULENAME_myform.tpl.php Place code, like this:

<table><tbody>
  <tr>
    <td>
     <?php print drupal_render($form['row1']['field1'] );  ?>
    </td>
    <td>
     <?php print drupal_render($form['row1']['field2'] );  ?>
    </td>
    ...
  </tr>

</tbody></table>

<?php print drupal_render($form); // Required! ?>


Refresh cache in Perfomance page.

If you want to theme result of form, then just theme page output (like node-TYPE.tpl.php)

Nikit
+1  A: 

I would build arrays of drupal_render()-ed output from the form's element_children that can be themed using theme('table', $header, $rows, $attributes = array(), $caption = NULL).

scronide
+1  A: 

Something like that may help.


function yourmodule_yourform($form){

    $header = array(
        'field1', 'field2', 'field3',
    );

    $rows = array();

    $rows[] = array('data' => array(drupal_render($form['row1']['field1'], drupal_render($form['row1']['field2'], drupal_render($form['row1']['field3'])))));

    $rows[] = array('data' => array(drupal_render($form['row2']['field1'], drupal_render($form['row2']['field2'], drupal_render($form['row2']['field3'])))));

    $rows[] = array('data' => array(drupal_render($form['row3']['field1'], drupal_render($form['row3']['field2'], drupal_render($form['row3']['field3'])))));

    $output = theme('table', $header, $rows, array('id' => 'yourid'));
    $output .= drupal_render($form);

    return $output;

}

UPD.

Also you can play around with class attribute like this:



$header = array(
        'Title', array('data' => t('Checkbox one'), 'class' => 'checkbox'), array('data' => t('Checkbox two'), 'class' => 'checkbox'),
    );

$row[] = drupal_render($form['name']);

$row[] = array('data' => drupal_render($form['checkbox_one']), 'class' => 'checkbox');

$row[] = array('data' => drupal_render($form['checkbox_two'], 'class'=> 'checkbox');

$rows[] = $row;

$output = theme('table', $header, $rows, array('id' => 'yourid'));
$output .= drupal_render($form);

return $output;

I'm not sure whether you can use some other classes besides checkbox. Pretty sure you can use weight somehow, but not sure about the others.

angryobject
You should probably do this in a form's theme or preprocess function, right ?
mongolito404
You do it in the form's theme function, not preprocess.
angryobject
+1  A: 

You can individually override each form item's theme function by setting the #theme property.

$form = array();
...
$form['#theme'] = 'my_custom_form_theme_function';
...
$form['item'] = array(
  ...
  '#theme' => 'my_custom_item_theme_function',
);

You have to define theme_my_custom_form_theme_function and theme_my_custom_item_theme_function and register them with hook_theme.

Yorirou