Is it possible to create a single checkbox form element in a Webform form?..

vip82 - October 20, 2007 - 11:23

I know we have 'select' component in a Webform form, which allows creation of listboxes, radios and checkboxes, but I need just one checkbox element... If I create it using select component it is then rendered as it was an item from the list: with list title and such. And in mail it is rendered as it was an option from many elements in that list...
What I need is a single checkbox. In mail it should say only the title of the checkbox and "yes" or "no" as its value.

I already managed to remove the list title on the form (via CSS). And I know I can theme the output of letters (need to override about 2 functions for that: 'theme_webform_mail_select' to get rid of a "dot" prefix for the checkbox and put my 'yes' or 'no' values, and theme_webform_mail_fields to get rid of the list title which I don't need for this single checkbox item) but this looks a bit overcomplicated, I have to put a lot of IFs in overridden functions with a fair amount of hardcode... this is not elegant at all.

I just though if there is a better way to do this?
What I need is a single checkbox item named "Subscribe to newsletter?". If user checks this, subscription routine is initiated using the email from the form... but this is another story, which is already working for me. I just need to polish the mail texts...

Please help!

My current solution

vip82 - October 20, 2007 - 12:24

Here is what I did, see php below. I think I'm going to use it in production version of my website, I went with just one function override:

<?php
function mytheme_webform_mail_fields($key, $value, $node, $indent = "") {
  ...
 
// Else use a generic output for arrays.
 
else {
   
$message .= $indent . $node->webformcomponents[$key]['name'] .":\n";
    foreach (
$value as $k => $v) {
      foreach(
$node->webformcomponents as $local_key => $local_value) {
        if (
$local_value['form_key'] == $k) {
         
$form_key = $local_key;
          break;
        }
      }
      if (
$k == 'the_list_with_only_one_checkbox') // if it is our single item list, process it the way we need
       
$message .= '  Subscribe to newsletter: ' . ($v['subscribe'] ? 'Yes' : 'No') . "\n";
      else
       
$message .= theme('webform_mail_fields', $form_key, $v, $node, $indent ."  ");
    }
  }
  ...
}
?>

This way we got rid of list title being inserted into mail text, and we put only values we need of the checkbox for that list. This is still hardcoded to elements' names (if you change names on form building page, you have to update this code, hardcoding is nasty), and we still need to remove list's title from webpage via CSS... but it worked for me.
If anyone has a better solution, please post.

Offer one option, but check Multiple

chellman - February 14, 2008 - 01:14

The way I did this was to only offer one selectable option, but check the Multiple checkbox.

I did the same as chellman

robdinardo - September 5, 2008 - 19:59

I did the same as chellman too! I make it mandatory and use it as a "I agree to the terms..."

 
 

Drupal is a registered trademark of Dries Buytaert.