[development] Form fields in a table

John Fiala jcfiala at gmail.com
Fri Sep 3 17:31:11 UTC 2010


Just in case, first thing is to remind you to register your theme
function in a hook_theme implementation - I forgot to list that as a
'need to do' earlier.

Second thing is that *anything* you are displaying from $elements
should be processed with drupal_render.  That tells the form rendering
system that this element has been rendered and you don't need to mess
with it.

Third thing to point out is that if you're doing that way of building
fields - [table][$record->id]['field1'] (which is a good thing to do)
- Don't forget to either include '#tree' => TRUE or explicitly set the
'#parents' on each item.  By default fields are kept track of in the
fapi by the 'right-most' of the fields - 'field1' - whereas setting
'#TREE' => TRUE means that suddenly it's remembered as table-#-field1.

Yes, it's a little confusing the first time!  But the results are cool.

If you're still having trouble after this, I suggest cutting/pasting
some code into a pastebin website and providing the link here so we
can see it more clearly.


On Fri, Sep 3, 2010 at 11:18 AM, Jeff Greenberg <jeff at ayendesigns.com> wrote:
> Arrgh. Ok, I've made all those changes, and here's what it looks like
> (below). What I'm getting is the non-table form stuff, a vertical stacking
> of all the form fields that should be in the table, and then the table
> header, so the fields are being rendered (and the default value is
> showing--hoorah!), but outside of and previous to the table.
>
> form stuff
> .
> .
>            $form['table'] = array(
>                '#theme' => 'my_form_as_table',
>            );
>            while (loop through recordset)    {
>                $form['table'][$record->id]['field1'] = array (
>                    field stuff
>                );
>                $form['table'][$record->id]['field2'] = array (
>                    field stuff
>                );
>                .
>                .
>                .
>            }
>            $form[] = array (
>                '#type' => 'submit',
>                '#value' => 'Save Changes',
>            );
>        }
> function theme_my_form_as_table(&$elements) {
> $header = array(t('a'),t('b')...
>
> foreach ($elements['table'] as $k => $f) {
>    $rows[] = array($f['field1'],$f['field2'], drupal_render($f['field3'])...
>                     );
> }
> return theme('table', $header, $rows) . drupal_render($elements);
> }
>
>
> On 09/03/2010 11:30 AM, John Fiala wrote:
>>
>> 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...
>>
>>
>



-- 
John Fiala
www.jcfiala.net


More information about the development mailing list