[development] Form fields in a table

John Fiala jcfiala at gmail.com
Fri Sep 3 15:30:53 UTC 2010


Well, you can do a fieldset if you like.  But it seems that you don't
need to have a fieldset to theme a group of fields, you just need to
group them in the array.  So...

<?php
... form stuff...

$form['table'] = array(
  '#theme' => 'my_project_form_as_table',
);

$form['table']['field_1'] = array(
  '#type' => 'textfield',
....
);

$form['table']['field_2'] = array(...);
$form['table']['field_3'] = array(...);
...

$form['submit'] = array('#type' => 'submit', '#value' => 'submit');
return $form
}

....

theme_my_project_form_as_table(&$elements) {
$header = ('a', 'b');

$rows = array(
  array(t('Row 1'), drupal_render($elements['field_1'])),
  array(t('Row 2'), drupal_render($elements['field_2'])),
  array(t('Row 3'), drupal_render($elements['field_3'])),
...
);

return theme('table', $header, $rows) . drupal_render($elements);
}
?>

So, the two points you _really_ don't want to forget:
1) The $elements argument in the theme function declaration must start
with the &, so that when fields are marked as rendered by
drupal_render the rest of the system knows it.
2) Always call 'drupal_render($elements)' and add the output before you return.


On Fri, Sep 3, 2010 at 9:03 AM, Jeff Greenberg <jeff at ayendesigns.com> wrote:
> It's confused the heck out of me :-)  in terms of which element to #theme...
>  Some of the form fields are in a table, rather than a table containing the
> form. So at first blush, it would seem that theming individual fields
> wouldn't work, nor would theming the entire form... create a fieldset around
> the table and theme it?
>
> On 09/03/2010 10:38 AM, John Fiala wrote:
>>
>> Generally what you want to do to get a table as part of a form is to
>> declare a theme function that you then assign as '#theme' =>
>> 'function_name', where the theme function is theme_function_name.
>>
>> This used to confuse the heck out of me, and then I went in and read
>> through the code on the admin/content/node page, which is a pretty
>> good example of how to display a form in a table, I think.
>>
>>
>>
>



-- 
John Fiala
www.jcfiala.net


More information about the development mailing list