Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By blond_and_stupid on
A simple example:
function test_form() {
$form['serial'] = array(
'#type' => 'textfield',
'#title' => t('serial number'),
);
$form['submit'] = array (
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['#redirect'] = FALSE;
$output = drupal_get_form('test_form', $form);
return $output;
}
$output = test_form();
echo "$output";
will display the submit button below the textfield.
Now I want to position the submit button on the same line, after the textfield, using a table.
How do I accomplish that?
Comments
2 ways
Lasy to search for examples, just can point to check '#prefix' and '#suffix' options.
Or, better to produce table with theme('table', )
Examples?!
I tried using #prefix and #suffix without success.
Where can one find easy-to-understand examples?
-
Theming forms
theme_table (use:
theme('table', $header, $rows, $attributes, caption);
)still puzzled...
thanks for your help, but I still can't figure out how to do this.
What I want is roughly the following simple php code...
to get the textfield and submit button on the same line.
Writing forms in tables
Writing forms in tables
Finally!
OK, I got it to work using #prefix and #suffix.
For those interested, I'll post my original test form modified to display the textfield and submit button on the same line instead of on separate lines.
It was a simple solution, yet it took me some hours to figure out.
Thanks Budrick for your help, especially the last link "Writing forms in tables" was exactly what I needed.
Thanks for posting this code
Hi,
Thank you for posting this code.
I was searching for some way to display my form elements in a table.
Thanks again.
where to write this ?
Hello,
That seems to be exactly what I am looking for. But I can't figure out where to write all this :(
Any link ? Any help ?
Thanks in advance ;)
Thanks a lot...
Thank u guys..i was searching for this piece of code for long time..hope it will be useful for many users like me... :)
Proper way of doing it
Hi Guys
Found the proper solution. It might look scary at first, but once you get it is all good.
To begin with you should read http://drupal.org/node/751826, pay spacial attention to "Theming Forms".
Once you've read that you can take a look at http://api.drupal.org/api/function/theme_node_admin_nodes/6
That is part of the implementation for the well known 'admin/content/node/overview' page in every Drupal install.
Long story short (read above links anyway) you render each form element that is to go on a table independently and at the end you render whatever is left of the form that you do not want on a table, Drupal is smart enough not to render something twice (just brilliant).
Example:
You need to create your own theme_WHATEVER function that will render you form, and make your form use your theme function:
Last, register your theme to Drupal using hook_theme()
http://www.jaypan.com/blog/th
http://www.jaypan.com/blog/themeing-drupal-6-forms-tables-checkboxes
Jay: Great tutorial! Here is
Jay: Great tutorial! Here is the link to the D7 tutorial of the same subject. It is much easier than D6!
http://www.jaypan.com/blog/themeing-drupal-7-forms-tables-checkboxes-or-...
Simple Drupal 7 FAPI form fields in html table rows
Avoid spreading your code across a bunch of locations with something like the following FAPI snippet:
to get:
sample fapi table row output
You'd want to put in id and class attributes appropriate to your application, but the stub functionality is here.
--
http://drupaltees.com
80s themed Drupal T-Shirts
That's great if you want a
That's great if you want a tableselect, but that doesn't really help if you want inputs in the table cells.
You can use this tutorial for
You can use this tutorial for that: http://www.jaypan.com/tutorial/themeing-drupal-7-forms-including-css-and-js
The difference will be that in the theme function (called theme_form_theme_form()), you will build and return your table.
You might want to change
You might want to change
$output .= drupal_render($form);
into$output .= drupal_render_children($form);
.Group into fields, set fiel group to Table
This might help you to avoid writing code:
https://drupal.org/project/field_group_table
How to get textfield value from custom table using custom module
How to create and get textfield value from custom table using custom module