Using FAPI for a form (D6), and in the form is a markup field, which is a table created with theme(). Some of the cells in the table need to be populated with form fields. The contents of the table is being sent to theme() as $rows, and I've tried having the cells in question defined as $form[myfield] that has been created earlier, but I end up with an empty cell, and the form field appearing where it was first defined, prior to the table... so... $form[myfield] = array(... $rows[this_row] = array(cellval, cell2val, $form[myfield] ... isn't working. I need something like Row 1 text text selectbox text textfield text
Hi Jeff You need to call drupal_render on the form field. Have a look at theme_user_admin_account for a good example, this generates the list of users with the checkbox at /admin/user/user Lee On Thu, 2010-09-02 at 20:20 -0400, Jeff Greenberg wrote:
Using FAPI for a form (D6), and in the form is a markup field, which is a table created with theme(). Some of the cells in the table need to be populated with form fields. The contents of the table is being sent to theme() as $rows, and I've tried having the cells in question defined as $form[myfield] that has been created earlier, but I end up with an empty cell, and the form field appearing where it was first defined, prior to the table... so...
$form[myfield] = array(...
$rows[this_row] = array(cellval, cell2val, $form[myfield] ...
isn't working.
I need something like
Row 1 text text selectbox text textfield text
Thanks Lee. On 09/02/2010 08:49 PM, Lee Rowlands wrote:
Hi Jeff You need to call drupal_render on the form field. Have a look at theme_user_admin_account for a good example, this generates the list of users with the checkbox at /admin/user/user
So almost there. Before, the form fields were being rendered when declared ,prior to the table, and they (selects) were showing the default value. I left everything as is, and when I refer to the form field in the array being formatted as a table, I encased it in drupal_render. I'm now getting the populated form field in the table and not outside it, but they no longer display the default value they were declared with. On 09/02/2010 08:49 PM, Lee Rowlands wrote:
Hi Jeff You need to call drupal_render on the form field. Have a look at theme_user_admin_account for a good example, this generates the list of users with the checkbox at /admin/user/user
Lee On Thu, 2010-09-02 at 20:20 -0400, Jeff Greenberg wrote:
Using FAPI for a form (D6), and in the form is a markup field, which is a table created with theme(). Some of the cells in the table need to be populated with form fields. The contents of the table is being sent to theme() as $rows, and I've tried having the cells in question defined as $form[myfield] that has been created earlier, but I end up with an empty cell, and the form field appearing where it was first defined, prior to the table... so...
$form[myfield] = array(...
$rows[this_row] = array(cellval, cell2val, $form[myfield] ...
isn't working.
I need something like
Row 1 text text selectbox text textfield text
$form[myfield] is an array. Assuming you only want the value then use $form[myfield]['#value'] or $form[myfield]['#default_value']. Nancy ________________________________ From: Jeff Greenberg <jeff@ayendesigns.com> To: development@drupal.org Sent: Thu, September 2, 2010 8:20:20 PM Subject: [development] Form fields in a table Using FAPI for a form (D6), and in the form is a markup field, which is a table created with theme(). Some of the cells in the table need to be populated with form fields. The contents of the table is being sent to theme() as $rows, and I've tried having the cells in question defined as $form[myfield] that has been created earlier, but I end up with an empty cell, and the form field appearing where it was first defined, prior to the table... so... $form[myfield] = array(... $rows[this_row] = array(cellval, cell2val, $form[myfield] ... isn't working. I need something like Row 1 text text selectbox text textfield text
I want the cell in the table to be the form field ($form[myfield]), which was declared previously as $form['myfield'][$id] = array ( '#type'=> 'select', '#options' => $value_array, '#default_value' => $value[$id], ); So now, using drupal_render($form[myfield]) in the table, I get the select box, and it is populated with $value_array values, but its initial presentation in every row is no value...the default value seems to be ignored. On 09/02/2010 10:59 PM, nan wich wrote:
$form[myfield] is an array. Assuming you only want the value then use $form[myfield]['#value'] or $form[myfield]['#default_value'].
/*Nancy*/
------------------------------------------------------------------------ *From:* Jeff Greenberg <jeff@ayendesigns.com> *To:* development@drupal.org *Sent:* Thu, September 2, 2010 8:20:20 PM *Subject:* [development] Form fields in a table
Using FAPI for a form (D6), and in the form is a markup field, which is a table created with theme(). Some of the cells in the table need to be populated with form fields. The contents of the table is being sent to theme() as $rows, and I've tried having the cells in question defined as $form[myfield] that has been created earlier, but I end up with an empty cell, and the form field appearing where it was first defined, prior to the table... so...
$form[myfield] = array(...
$rows[this_row] = array(cellval, cell2val, $form[myfield] ...
isn't working.
I need something like
Row 1 text text selectbox text textfield text
So there seems to be two problems with the drupal_rendered form field. One is that in looking at the page source, the option value in each of these select fields that matches the #default_value are not marked as selected. The other problem is that the data is not passed back on any of these fields, and the page source shows each of them having a class of form_field like the non drupal_render fields, but id="" and name=""
Jeff Are you calling drupal_render in the theme function or the form builder? You need to call it in the theme function. See theme_user_admin_account and user_admin_account, the form is built and returned in user_admin_account (as a flat form) and returned as an array, which is then rendered in theme_user_admin_account as a table. Lee On Fri, 2010-09-03 at 00:49 -0400, Jeff Greenberg wrote:
So there seems to be two problems with the drupal_rendered form field. One is that in looking at the page source, the option value in each of these select fields that matches the #default_value are not marked as selected.
The other problem is that the data is not passed back on any of these fields, and the page source shows each of them having a class of form_field like the non drupal_render fields, but id="" and name=""
Hi Lee, Well, I think this is a slightly different scenario, so I might need to do what you're saying in a different way. It's a multi-page form that starts as a a few search fields, and then adds on the results, some of which is editable. So, the flow is like this: hook_form() { turn off #redirect build sort form fields does form_values have id? Y update form submit was clicked? Y update tables N query based on contents of sort fields loop through result set and create additional form fields create table containing mixture of text and form fields add submit field } Jeff On 09/03/2010 05:14 AM, Lee Rowlands wrote:
Jeff Are you calling drupal_render in the theme function or the form builder? You need to call it in the theme function. See theme_user_admin_account and user_admin_account, the form is built and returned in user_admin_account (as a flat form) and returned as an array, which is then rendered in theme_user_admin_account as a table.
Lee On Fri, 2010-09-03 at 00:49 -0400, Jeff Greenberg wrote:
So there seems to be two problems with the drupal_rendered form field. One is that in looking at the page source, the option value in each of these select fields that matches the #default_value are not marked as selected.
The other problem is that the data is not passed back on any of these fields, and the page source shows each of them having a class of form_field like the non drupal_render fields, but id="" and name=""
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
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.
If you are doing this for your own site or for a client and not for something that will be used my many sites, I have found just using #prefix #suffix in the form fields work out great. It's not the most "drupaly" way of doing things, but it does work and gets the job done rather fast. Jamie Holly http://www.intoxination.net http://www.hollyit.net On 9/3/2010 11:03 AM, Jeff Greenberg 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.
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@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
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...
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@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
I -had- forgotten #tree, and have added hook_theme and (I think) had drupal_render on all those fields, but that hasn't put the elements in the table. http://pastebin.com/rR9N5LfG On 09/03/2010 01:31 PM, John Fiala wrote:
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.
Hm. I'm wondering if $elements['table'] actually exists. Try http://pastebin.com/xU4F7C0m - pay attention to lines 51 through 58, that's where I made changes. Basically, I think the problem is that the $elements item _is_ the 'table' level, and that's why you're not finding your rows. And it's important to use $elements[$k] instead of $f, because drupal_render's changes need to go back to the calling function. On Fri, Sep 3, 2010 at 1:31 PM, Jeff Greenberg <jeff@ayendesigns.com> wrote:
I -had- forgotten #tree, and have added hook_theme and (I think) had drupal_render on all those fields, but that hasn't put the elements in the table. http://pastebin.com/rR9N5LfG
On 09/03/2010 01:31 PM, John Fiala wrote:
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.
-- John Fiala www.jcfiala.net
Ah, yes, defintey ['table'] was an issue. I've made those changes, including the & before $f, but the output is the same. One thing that of note that might be a clue is that not even the table heading is showing since I moved the theme('table'... call to a theme function. I just get the upper standard part of the form, all the fields rendered as if the theming and drupal_render were being ignore altogether (stacked vertically), and then the submit button. On 09/03/2010 03:59 PM, John Fiala wrote:
Hm. I'm wondering if $elements['table'] actually exists. Try http://pastebin.com/xU4F7C0m - pay attention to lines 51 through 58, that's where I made changes.
Basically, I think the problem is that the $elements item _is_ the 'table' level, and that's why you're not finding your rows. And it's important to use $elements[$k] instead of $f, because drupal_render's changes need to go back to the calling function.
participants (5)
-
Jamie Holly -
Jeff Greenberg -
John Fiala -
Lee Rowlands -
nan wich