Hi,

I'm creating a module and have run across a problem I would appreciate some help with.

I have a form page and I would like to display a table at the top (showing information to help the user make their choice in the form).

I have used the theme('table', $header, $rows) method to create a table but I have no idea how to place that into the form (created via the 'drupal_get_form' callback).

Can someone please help my idoicy?

Thx

Comments

afeijo’s picture

Thats very simple, use hooke_all or hook_block.

function test_all() {
$header = array('this', 'is', 'my', 'test');
$rows[] = array('wow', 'it', 'works', 'great!');

$content = theme('table', $header, $rows);
return $content;
}

huge hug
Feijó

panis’s picture

Adding a description tag to each form field may be another way you can provide help in filling up the forms. To use this just add a '#description'=>t('Help for the field') for every field that you would like to provide instructions with.

Of course the other alternative is to include it in the form like so:

$form['myformname']=array(
'#type'=>'item',
'#value'=>theme('table', $header, $rows...),
'#weight'=>-10 /* so it shows up before anything else or just make this the first definition. */
);
depace’s picture

note that this is a very extract form.. u may need to modify or add few stuffs to make it working...
better take it as an example...

	function modulename_form($node)
	{
		$arr_form['frm_field_1']	=	array(
											'#type'=>'textfield',
											'#title'=>t('Field One'),
											'#required'=>true,
											'#size'=>40
										);
		$arr_form['frm_field_2']	=	array(
											'#type'=>'textfield',
											'#title'=>t('Field Two'),
											'#required'=>true,
											'#size'=>40
										);
	}
	
	function theme_modulename_form($arr_form)
	{
		$str_output	=	"*** put ur html stuff here ***"; //i know its a very bad thing to do but hey .. its saves my skin... 
		
		$str_output	.=	drupal_render($arr_form);
	}

you can also put ur text fields, lets say "frm_field_2" inside a table cell (inside

) as well ...
$str_output .= "< td >".drupal_render($arr_form['frm_field_2'])."< /td >";

lemme know if its going above ur head... :-)

qasimchaudhry’s picture

Thanks, that works wonders

eotinfotech’s picture

I tried the same thing and it work perfect as the fields comes in the table.
but for each field, I am getting a error message

# warning: implode() [function.implode]: Bad arguments. in d:\Websites\htdocs\includes\form.inc on line 618.
# warning: implode() [function.implode]: Bad arguments. in d:\Websites\htdocs\includes\form.inc on line 618.
# warning: implode() [function.implode]: Bad arguments. in d:\Websites\htdocs\includes\form.inc on line 618.

few lines of the code is

<?php
function hms_information_horse_search () {

/*Create the form elements*/

$form['search_information_horse']['Horse_Name'] = array('#type' => 'textfield',
    '#default_value' => $edit['Horse_Name'],
    '#maxlength' => 20,
  );

  $form['search_information_horse']['Registration_Number'] = array('#type' => 'textfield',
    '#default_value' => $edit['Registration Number'],
    '#maxlength' => 20,
  );

$form['search_information_horse']['Submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit')
    );

/*Place the elements in the table */

$rows[] = array('Horse Name', 'Registration_Number');
$rows[] = array(
	drupal_render($form['search_information_horse']['Horse_Name']), 
	drupal_render($form['search_information_horse']['Registration_Number']), 
);
$rows[] = array(
	drupal_render($form['search_information_horse']['Submit'])
);

/*Generate the table*/
$table_content = theme('table', $header, $rows);

/*Send a output*/
return $table_content;
}
?>

I am unable to figure out why i am getting this error, also the
tag is not creating and submit button is not working.

Can any one help me in this ?

Regards, and Have a nice day.

Prachait Saxena
---------------------------
(M) :- +91 9953 200 299
(E) :- info (at) prachait (dot) com
(W) :- http://www.Prachait.Com/

panis’s picture

your form definition function and theming function need to be separated out.

put the first part of the function above: where you define the $form[..] = fields into its own function

function hms_information_horse_search() {
...
return $form;
}

The second part with the table put in a different function it has to be called:

function theme_hms_information_horse_search($fid,$form)
{
.. do your table formatting stuff.
..always in the end also have a call to and include in your output table - drupal_render_form($form) - this will make sure that all the fields that you may have missed out get included (hidden fields maybe etc).
return $table_content;
}
eotinfotech’s picture

hi panis, thanks for the help,
I did the changes as you said, but still the same error,

I am not able to figure out, the issues
the changes are

Called on some page from menu :

<?php

      $output .= theme_hms_information_horse_search('ll',hms_information_horse_search());


?>
</code?
<code>
<?php
function hms_information_horse_search () {


 $form['search_information_horse'] = array('#type' => 'fieldset',
    '#title' => t('Horse Information'),
  );
$form['search_information_horse']['#attributes'] = array('enctype' => "multipart/form-data");

...... usual form fields from the above 

?>

Function for creating the table theme

<?php]
function theme_hms_information_horse_search ($fid,$form) {

/*Place the elements in the table */

$rows[] = array('Horse Name', 'Registration_Number');
$rows[] = array(
    drupal_render($form['search_information_horse']['Horse_Name']),
    drupal_render($form['search_information_horse']['Registration_Number']),
);
$rows[] = array(
    drupal_render($form['search_information_horse']['Submit'])
);

$output .= drupal_render($form['search_information_horse']);
$output .= drupal_render($form['search_information_horse']['#attributes']);

/*Generate the table*/
$table_content = theme('table', $header, $rows);

/*Send a output*/

	$output .= $table_content;
	$output .= drupal_render_form($form,$fid);

  return $output;

?>

I check the HTML output, and got the followings

1. The
is not geting generated
2. the elements name="" is coming blank

	<table>
<tbody>
 <tr class="odd"><td>Horse Name</td><td>Registration_Number</td><td>Horse Type</td><td>Premium Status</td> </tr>
 <tr class="even"><td><div class="form-item">
 <input type="text" maxlength="20" name="" id=""  value="" class="form-text" />
</div>
</td><td><div class="form-item">
 <input type="text" maxlength="20" name="" id=""  value="" class="form-text" />
</div>
</td></tr>
<tr class="odd"><td><input type="submit" id="" value="Submit"  class="form-" />
</td> </tr>
</table>

Regards, and Have a nice day.

Prachait Saxena
---------------------------
(M) :- +91 9953 200 299
(E) :- info (at) prachait (dot) com
(W) :- http://www.Prachait.Com/

panis’s picture

in your menu function - do not call the theme function directly.

Call:

$output .= drupal_get_form('hms_information_horse_search');

This will automatically call the theme function and do the submit etc..

eotinfotech’s picture

Hi,

I made the said changes,

$output .= drupal_get_form('hms_information_horse_search');

1. The form submit is working fine
2. The elements name is coming
3. All the error are removed, but now a new error is coming

BUT:
1. New error :
warning: Missing argument 2 for theme_hms_information_horse_search() in d:\Websites\htdocs\modules\hms\hms_horse\hms_horse.module on line 1710.

i rectified the above error by changing

function theme_hms_information_horse_search ($fid, $form) {

to 

function theme_hms_information_horse_search ($form) {

2. The FORM is not coming in the TABLE now,
Now First the table appear then the form block with all the elements one below other.

Rectification :
Comment this line

//$output .= drupal_render($form['search_information_horse']);

Now it is working fine, the form is coming in the table and submit button is also working.

Thanks for the kind support.

Regards, and Have a nice day.

Prachait Saxena
---------------------------
(M) :- +91 9953 200 299
(E) :- info (at) prachait (dot) com
(W) :- http://www.Prachait.Com/