I have form section that needs 9 text fields next to one label. Is this possible?
beckyjohnson - November 10, 2009 - 06:10
| Project: | Webform |
| Version: | 6.x-2.9 |
| Component: | Miscellaneous |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | fixed |
Description
I have to make a form and this is what I need is to have the label List all MAC Addresses and have 9 text fields next to it.
Is that possible? Or should I just use one text area? Having 9 separate fields seems better from a user point of view but I'm not sure how to execute it.
Any ideas?
Becky

#1
You can create 9 textfields (they all need labels initially), then theme your Webform to put them side-by-side or in a table and remove the labels. See the THEMING.txt file that comes with Webform.
#2
Ok cool. Becky
#3
I have been working on themeing lately. I tried to theme the node-webform.tpl.php and was using this post as an example: http://drupal.org/node/358926
However, while I have gotten the body to print properly using
<?php print $node->content['body']['#value']; ?>I have not been able to print out the fields and I'm not sure why.
I have tried this:
<?php print drupal_render($form['submitted']['first_name']); ?>this:
<?php print $node->content['webform']['#components'] ['#first_name']; ?>(trying to walk down the arrays, so to speak)and this:
<?php print $node->content['webform']['first_name']; ?> or alternately ['#first_name']Nothing works. What I get is a node with the body printed and no forms....
Could I please have an example of the right syntax? This doesn't seem to be like printing out cck fields.
Becky
#4
Hm, well if you want to theme the *form* then you have to use the webform-[nid].tpl.php file (as described in THEMING.txt). Using node-webform.tpl.php, you'll be able to theme the things around the form but not the form itself.
#5
Oh..ok. I'll give it another shot then. Thanks for putting up with this.
Becky
#6
OK. So I found out that this code works with the webform-[nid].tpl.php file:
print drupal_render($form['submitted']['first_name']);
print drupal_render($form['submitted']['last_name']);
But, how would I separate the labels out using this from this code? Could I have an example?
Thanks,
Becky
#7
Print out the title, then unset() it from the item before rendering the actual field:
<?php
print $form['submitted']['first_name']['#title'];
unset($form['submitted']['first_name']['#title']);
print drupal_render($form['submitted']['first_name']);
?>
#8
Sweet. Thanks! That makes sense. I have one more question. Is it necessary to print out page breaks? I printed out my page break like above:
print drupal_render($form['submitted']['page_break_one']);it didn't show up inside my table like I programmed it to. I'm not actually sure it made any difference. However, I don't mind NOT printing them out on the page, if they still show up and still function.(I would paste all my code here but it is too long to do so at this point)
#9
You shouldn't need to print out the page breaks, but you should make sure you render out the remaining portions of the form at the bottom of your .tpl.php file.
<?phpprint drupal_render($form);
?>
This will print out all the fields that you have not individually rendered already, and it will include things like the hidden fields that Drupal uses for security and form processing.
#10
Ok. I have one more question.
On my form I have a fieldset. I skipped printing out the field set because I assumed based on your last post that
<?phpprint drupal_render($form);?>
<?phpprint "<tr>";
print "<td>";
print drupal_render($form['submitted']['mac_address_1']);
print "</td>";
print "<td>";
print drupal_render($form['submitted']['is_this_a_range']);
print "</td>";
print "</tr>";
?>
These tr/td tags are wrapped in a table tag of course, that has other fields printed out in other table cells.
Anyway, these fields are rendering, but not next to each other like I would expect them to based on the code...
What else am I missing?
#11
OK. I got my fields to print out next to each other using this:
print "<table>";print "<tr>";
print "<td>";
print drupal_render($form['submitted']['list_mac_addresses'] ['mac_address_1']);
print "</td>";
print "<td>";
print drupal_render($form['submitted']['list_mac_addresses'] ['is_this_a_range']);
print "</td>";
print "</tr>";
print "</table>";
But when I did that, it took the fields right out of their field set, which I don't want. I have been searching everywhere to try and fix this but nothing I try works. What I end up is the resulting screenshot.
The heading List Mac Addresses and it's description should be above the two fields are are side by side, not below it.
#12
You can put the rendered HTML back into a fieldset by using the mysterious and not often documented #children property.
$fieldset_output .= drupal_render(...);
$fieldset_output .= drupal_render(...);
$fieldset_output .= drupal_render(...);
$form['submitted']['fieldset_name']['#children'] = $fieldset_output;
print drupal_render($form);
#13
Awesome. Thanks for that info.
This is what I did below:
<?phpprint "<table>";
print "<tr>";
print "<td>";
print $fieldset_output .= drupal_render($form['submitted']['list_mac_addresses'] ['mac_address_1']);
$forum['submitted'] ['list_mac_addresses'] ['#children'] = $fieldset_output;
print "</td>";
print "<td>";
print $fieldset_output .= drupal_render($form['submitted']['list_mac_addresses'] ['is_this_a_range']);
$forum['submitted'] ['list_mac_addresses'] ['#children'] = $fieldset_output;
print "</td>";
print "</tr>";
print "</table>";
?>
And what I get, is that my mac_address_1 field prints out twice and doesn't go back into the fieldset. I have an attached screenshot.
#14
I re did it with this code
<?php
// Print out the main part of the form.
// Feel free to break this up and move the pieces within the array.
print "<table>";
print "<tr>";
print "<td>";
print $fieldset_output .= drupal_render($form['submitted']['list_mac_addresses'] ['mac_address_1']);
print "</td>";
print "<td>";
print $fieldset_output .= drupal_render($form['submitted']['list_mac_addresses'] ['is_this_a_range']);
print "</td>";
print "</tr>";
print "</table>";
// Always print out the entire $form. This renders the remaining pieces of the
// form that haven't yet been rendered above.
$forum['submitted'] ['list_mac_addresses'] ['#children'] = $fieldset_output;
print drupal_render($form);
?>
and got the same problem..
#15
You need to not print out the contents of the fieldset since otherwise they will appear immediately when you tell them to print. Instead build up a variable, then put it contents into the fieldset's #children property.
<?php
// Print out the main part of the form.
// Feel free to break this up and move the pieces within the array.
$fieldset_output .= "<table>";
$fieldset_output .= "<tr>";
$fieldset_output .= "<td>";
$fieldset_output .= drupal_render($form['submitted']['list_mac_addresses'] ['mac_address_1']);
$fieldset_output .= "</td>";
$fieldset_output .= "<td>";
$fieldset_output .= drupal_render($form['submitted']['list_mac_addresses'] ['is_this_a_range']);
$fieldset_output .= "</td>";
$fieldset_output .= "</tr>";
$fieldset_output .= "</table>";
// Always print out the entire $form. This renders the remaining pieces of the
// form that haven't yet been rendered above.
$forum['submitted']['list_mac_addresses']['#children'] = $fieldset_output;
print drupal_render($form);
?>
#16
Something didn't quite work...
Sorry about this but the above code didn't work out, which is weird because I understand what is going on in it and I can't see where there would be an error.
This is my code now:
$fieldset_output .= "<table>";
$fieldset_output .= "<tr>";
$fieldset_output .= "<td>";
$fieldset_output .= drupal_render($form['submitted']['list_mac_addresses'] ['mac_address_1']);
$fieldset_output .= "</td>";
$fieldset_output .= "<td>";
$fieldset_output .= drupal_render($form['submitted']['list_mac_addresses'] ['is_this_a_range']);
$fieldset_output .= "</td>";
$fieldset_output .= "</tr>";
$fieldset_output .= "</table>";
// Always print out the entire $form. This renders the remaining pieces of the
// form that haven't yet been rendered above.
$forum['submitted']['list_mac_addresses']['#children'] = $fieldset_output;
print drupal_render($form);
And the attached screenshot is the result. It made the Mac address 1 and is this a a range? field vanish entirely.
#17
Hey, sorry I didn't mean to change the issue name. I just forgot I wasn't in the regular forum area.
Becky
#18
$forum['submitted']['list_mac_addresses']['#children'] = $fieldset_output;
should be
$form['submitted']['list_mac_addresses']['#children'] = $fieldset_output;
#19
Oh my god! what a stupid typo! Sorry I took up your time with this.
Becky
#20
Awesome! Everything works and looks fine now. Thank's for your help. I actually learned a lot from you. Also, incidentally, using the above code, I had to print out every field in the fieldset. Left out fields would not render on the screen. Not a big deal though.
Becky
#21
One last note, you can render out all the fields within the fieldset all at once by calling drupal_render() on the entire fieldset, then adding that to your $fieldset_output variable.
So the final code:
$fieldset_output .= "<table>";
$fieldset_output .= "<tr>";
$fieldset_output .= "<td>";
$fieldset_output .= drupal_render($form['submitted']['list_mac_addresses']['mac_address_1']);
$fieldset_output .= "</td>";
$fieldset_output .= "<td>";
$fieldset_output .= drupal_render($form['submitted']['list_mac_addresses']['is_this_a_range']);
$fieldset_output .= "</td>";
$fieldset_output .= "</tr>";
$fieldset_output .= "</table>";
$fieldset_output .= drupal_render($form['submitted']['list_mac_addresses']); // <-- New line.
// Always print out the entire $form. This renders the remaining pieces of the
// form that haven't yet been rendered above.
$form['submitted']['list_mac_addresses']['#children'] = $fieldset_output;
print drupal_render($form);
Glad that we were finally able to get it.
#22
That's great! Thanks so much for your help on this.