From simon@dirtbike.ws Sat Nov 12 00:35:15 2005 From: Simon Lindsay To: development@drupal.org Subject: [development] Forms API Question - Node with table like poll.module Date: Sat, 12 Nov 2005 11:04:10 +1030 Message-ID: <43753882.2010706@dirtbike.ws> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============8992049763451229542==" --===============8992049763451229542== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Hello All, I'm developing some modules to manage my computer business, and to do invoice, purchase orders, timesheets etc. I was using a table in the form in 4.6.3, but things are different in 4.7. The code below looks the closert so far, is there a better way to do this anybody can recommend? I've looked at the modules that use arrays/rows, like poll and system, but they aren't really doing what i'm trying to. Or do I just need to use css in the theme it to get the correct display? I'm open to suggestions. TIA Simon 4.6.3 ------- $header = array(t('Time'), t('Customer'), t('Comments')); $rows = array(); $count = count($node->timesheet_time); if ($count < 5) { $count = 5; } if ($_POST['edit']['timesheet_links_more'] == 1) { $count += 5; } for ($i = 0; $i < $count; $i++) { $row = array(); $row[] = form_textfield('', 'timesheet_time][' . $i, $node->timesheet_time[$i], 6, 6); $row[] = form_autocomplete('', 'timesheet_customer][' . $i, $node->timesheet_customer[$i], 30, 128, 'contacts/autocomplete_customer', $type); $row[] = form_textfield('', 'timesheet_comments][' . $i, $node->timesheet_comments[$i], 30, 255); //$row[] = form_textarea('', 'timesheet_comments][' . $i, $node->timesheet_comments[$i], 20, 2); $rows[$i] = $row; } $output .= form_item(t('Timesheet Entries'), theme('table', $header, $rows)); 4.7 ------ // If the user pressed the button asking for more rows, add more if ($_POST['edit']['timesheet_links_more'] == 1) { $count += 5; } $form['entries'] = array( '#type' => 'fieldset', '#title' => t('Timesheet Entries'), '#tree' => TRUE, ); // Now actually display the rows for ($i = 0; $i < $count; $i++) { $form['entries'][$i] = array( '#type' => 'fieldset', '#tree' => TRUE, ); $form['entries'][$i]['timesheet_time'] = array( '#type' => 'textfield', '#size' => 6, '#maxlength' => 6, '#default_value' => $node->timesheet_time[$i], '#prefix' => '', ); $form['entries'][$i]['timesheet_customer'] = array( '#type' => 'textfield', '#size' => 30, '#maxlength' => 128, '#autocomplete_path' => 'customer/autocomplete', '#default_value' => $node->timesheet_customer[$i], '#prefix' => '', ); $form['entries'][$i]['timesheet_comments'] = array( '#type' => 'textfield', '#size' => 30, '#maxlength' => 255, '#default_value' => $node->timesheet_comments[$i], '#prefix' => '
', '#suffix' => '', '#suffix' => '', '#suffix' => '
', ); } --===============8992049763451229542==-- From drewish@katherinehouse.com Sat Nov 12 01:15:03 2005 From: andrew morton To: development@drupal.org Subject: Re: [development] Forms API Question - Node with table like poll.module Date: Fri, 11 Nov 2005 17:15:02 -0800 Message-ID: In-Reply-To: <43753882.2010706@dirtbike.ws> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============3238944565956496001==" --===============3238944565956496001== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit I'm doing a little of the same kind of updates to some modules and I've found that the system.module settings forms are an excellent example of how to do form tables. The basic idea is to build the form, then put all the table rendering into a theme function. As you build the table you call form_render() on the contents of each cell. Finally, you call: $output .= theme('table',...) $output .= form_render($form); return $output; The cool part is that anything you haven't rendered in the table (like submission buttons) gets rendered below it. If that doesn't make sense or something's incorrect let me know, as I said I've just kind of figured it out. andrew On 11/11/05, Simon Lindsay wrote: > > Hello All, > > I'm developing some modules to manage my computer business, and to do > invoice, purchase orders, timesheets etc. I was using a table in the > form in 4.6.3, but things are different in 4.7. > > The code below looks the closert so far, is there a better way to do > this anybody can recommend? I've looked at the modules that use > arrays/rows, like poll and system, but they aren't really doing what i'm > trying to. > > Or do I just need to use css in the theme it to get the correct display? > > I'm open to suggestions. > > TIA > > Simon > > 4.6.3 > ------- > $header = array(t('Time'), t('Customer'), t('Comments')); > $rows = array(); > $count = count($node->timesheet_time); > if ($count < 5) { > $count = 5; > } > if ($_POST['edit']['timesheet_links_more'] == 1) { > $count += 5; > } > for ($i = 0; $i < $count; $i++) { > $row = array(); > $row[] = form_textfield('', 'timesheet_time][' . $i, > $node->timesheet_time[$i], 6, 6); > $row[] = form_autocomplete('', 'timesheet_customer][' . $i, > $node->timesheet_customer[$i], 30, 128, > 'contacts/autocomplete_customer', $type); > $row[] = form_textfield('', 'timesheet_comments][' . $i, > $node->timesheet_comments[$i], 30, 255); > //$row[] = form_textarea('', 'timesheet_comments][' . $i, > $node->timesheet_comments[$i], 20, 2); > > $rows[$i] = $row; > } > > $output .= form_item(t('Timesheet Entries'), theme('table', $header, > $rows)); > > 4.7 > ------ > > // If the user pressed the button asking for more rows, add more > if ($_POST['edit']['timesheet_links_more'] == 1) { > $count += 5; > } > > $form['entries'] = > array( > '#type' => 'fieldset', > '#title' => t('Timesheet Entries'), > '#tree' => TRUE, > ); > > // Now actually display the rows > for ($i = 0; $i < $count; $i++) { > $form['entries'][$i] = > array( > '#type' => 'fieldset', > '#tree' => TRUE, > ); > $form['entries'][$i]['timesheet_time'] = > array( > '#type' => 'textfield', > '#size' => 6, > '#maxlength' => 6, > '#default_value' => $node->timesheet_time[$i], > '#prefix' => '', > ); > > $form['entries'][$i]['timesheet_customer'] = > array( > '#type' => 'textfield', > '#size' => 30, > '#maxlength' => 128, > '#autocomplete_path' => 'customer/autocomplete', > '#default_value' => $node->timesheet_customer[$i], > '#prefix' => '', > ); > > $form['entries'][$i]['timesheet_comments'] = > array( > '#type' => 'textfield', > '#size' => 30, > '#maxlength' => 255, > '#default_value' => $node->timesheet_comments[$i], > '#prefix' => '
', > '#suffix' => '', > '#suffix' => '', > '#suffix' => '
', > ); > > } > > --===============3238944565956496001==-- From karoly@negyesi.net Sat Nov 12 01:50:37 2005 From: Karoly Negyesi To: development@drupal.org Subject: Re: [development] Forms API Question - Node with table like poll.module Date: Sat, 12 Nov 2005 02:52:55 +0100 Message-ID: In-Reply-To: <43753882.2010706@dirtbike.ws> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============7761528708057072725==" --===============7761528708057072725== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit > // Now actually display the rows > for ($i = 0; $i < $count; $i++) { > $form['entries'][$i] = Please look at the flowcharts http://drupal.org/node/37194 At this step you display nothing, you are building the form. Later on, you'll have a chance to theme your form (that it's only a part of a bigger form, that does not matter). so $form['entries']['#theme'] = 'theme_function_here'; If you need more advice (there are a lot of custom theme'd forms now in core) do not hesitate to write devel again. Regards NK --===============7761528708057072725==-- From karoly@negyesi.net Sat Nov 12 01:55:46 2005 From: Karoly Negyesi To: development@drupal.org Subject: Re: [development] Forms API Question - Node with table like poll.module Date: Sat, 12 Nov 2005 02:58:04 +0100 Message-ID: In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0848999486188948581==" --===============0848999486188948581== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit > $form['entries']['#theme'] = 'theme_function_here'; Further clarification: form['entries']['#theme'] = 'mymodule_foo'; function theme_mymodule_foo($form) { } For examples, I'd grep on element_children (though there are quite a few forms that got updated before element_children was born). Regards NK --===============0848999486188948581==-- From simon@dirtbike.ws Sat Nov 12 11:43:51 2005 From: Simon Lindsay To: development@drupal.org Subject: Re: [development] Forms API Question - Node with table like poll.module Date: Sat, 12 Nov 2005 22:12:46 +1030 Message-ID: <4375D536.6040002@dirtbike.ws> In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============7721365134831838774==" --===============7721365134831838774== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Karoly Negyesi wrote: > form['entries']['#theme'] = 'mymodule_foo'; > > function theme_mymodule_foo($form) { > } > > For examples, I'd grep on element_children (though there are quite a > few forms that got updated before element_children was born). OK, but the timesheet module is an extension of node.module, and you're supposed to "return $form;" at the end of the "function timesheet_form(&$node)", not "return drupal_get_form('timesheet_entries', $form);". However, doing the "return $form" means that the theme doesn't get called, and doing the "drupal_get_form" gives array errors. I can't find a module that extends node that uses the themeing for node submission (despite quite a bit of grepping), although several use it for administration. If anyone can point one out to me, or where I'm going wrong, that would be great. Simon --===============7721365134831838774==-- From karoly@negyesi.net Sat Nov 12 12:55:11 2005 From: Karoly Negyesi To: development@drupal.org Subject: Re: [development] Forms API Question - Node with table like poll.module Date: Sat, 12 Nov 2005 13:57:36 +0100 Message-ID: In-Reply-To: <4375D536.6040002@dirtbike.ws> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============4424561179199172443==" --===============4424561179199172443== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit On Sat, 12 Nov 2005 12:42:46 +0100, Simon Lindsay wrote: > Karoly Negyesi wrote: >> form['entries']['#theme'] = 'mymodule_foo'; >> function theme_mymodule_foo($form) { >> } >> For examples, I'd grep on element_children (though there are quite a >> few forms that got updated before element_children was born). > > OK, but the timesheet module is an extension of node.module, and you're > supposed to "return $form;" at the end of the "function > timesheet_form(&$node)", not "return > drupal_get_form('timesheet_entries', $form);". Yes. Return $form and it'll call theme_mymodule_foo with the array in $form['entries'] Regards NK --===============4424561179199172443==-- From chad@apartmentlines.com Sat Nov 12 14:54:49 2005 From: Apartment Lines To: development@drupal.org Subject: RE: [development] Forms API Question - Node with tablelike poll.module Date: Sat, 12 Nov 2005 07:50:22 -0700 Message-ID: In-Reply-To: <4375D536.6040002@dirtbike.ws> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============4281965305861096945==" --===============4281965305861096945== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable simon, please visit the core examples section of the handbook: http://drupal.org/no= de/36052 i believe you'll find that the system_user example and the system_themes exam= ple have what you're looking for. also, if you haven't yet, please visit the general forms API documentation at= : http://drupal.org/node/33338 at this point, i believe the doc team has covered most of the issues necessar= y to convert a module to the API. chad > -----Original Message----- > From: Simon Lindsay [mailto:simon@dirtbike.ws] > Sent: Saturday, November 12, 2005 04:43 > To: development@drupal.org > Subject: Re: [development] Forms API Question - Node with tablelike > poll.module >=20 >=20 > Karoly Negyesi wrote: > > form['entries']['#theme'] =3D 'mymodule_foo'; > >=20 > > function theme_mymodule_foo($form) { > > } > >=20 > > For examples, I'd grep on element_children (though there are quite a=20 > > few forms that got updated before element_children was born). >=20 > OK, but the timesheet module is an extension of node.module, and you're=20 > supposed to "return $form;" at the end of the "function=20 > timesheet_form(&$node)", not "return=20 > drupal_get_form('timesheet_entries', $form);". >=20 > However, doing the "return $form" means that the theme doesn't get=20 > called, and doing the "drupal_get_form" gives array errors. >=20 > I can't find a module that extends node that uses the themeing for node=20 > submission (despite quite a bit of grepping), although several use it=20 > for administration. If anyone can point one out to me, or where I'm=20 > going wrong, that would be great. >=20 > Simon >=20 >=20 --===============4281965305861096945==--