Hello friends,
I searched the forums and found several question about setting a checkbox field to be checked in default on registration form but without answers:
http://drupal.org/node/328514
http://drupal.org/node/314266

So I'm asking how to do that?
I added some fields to the registration form by using the Profile core module and there is no option to setting it to be checked as a default.

Any idea?

Thanks,
Miki

Comments

tbartels’s picture

Normally I would suggest reading up on some theming tutorials such as:
http://11heavens.com/theming-the-register-form-in-Drupal-6
or
http://drupal.org/node/350634

But AFAIK the #default_value can't be changed on the theming layer w/o using some ugly HTML or JS hack after the fields have been rendered. I have had no success changing default values of form items via form templates registered with hook_theme.

The easiest way I have found is to use hook_form_FORM_ID_alter or hook_form_alter.

You will need to create a simple module to house it but the following should work as a general template:

mytheme.info

name = My Theme
description = User Registration form alter for default On checkbox.
core = 6.x

mytheme.module:

mytheme_form_user_register_alter(&$form, $form_state) {
  // uncomment the line below to see the structure of the form
  //drupal_set_message('<pre>'. print_r($form, 1) .'</pre>');
  $form['<profile_category>']['<field_name>']['#default_value'] = TRUE;
}

You'll have to replace <profile_category> and <field_name> with the necessary values depending on your field.

There may be other ways and I would love to know them.

You might also be able to achieve this using the Content Profile module since CCK allows you to control the default value of fields.

projectnashville’s picture

Thanks for this helpful mini-module. I found I had to change line 2 to this and it worked great.
function mytheme_form_user_register_alter (&$form, $form_state) {

chadhester’s picture

I'm confused... shouldn't this be called "My Module"? I mean, thank you very much for the code. It is very helpful for my site for this exact purpose, but I want to make sure we're all on the same page here. This is not a theme, but a module, so it should be named as such to avoid confusion.

Also the .module file should not have a closing php tag "?>". I know the post wouldn't render properly without it, but still worth noting... ;)

** END RANT **

:)

__________
Regards,
Chad Hester

colemanw’s picture

Hi folks, I was glad to find this thread because I have the same problem. But after several frustrating hours trying every permutation of this code I could think of, my little custom module still does exactly nothing to affect the default value of my checkbox (although it does make all the variables show up in the "pre" text on the register page, which is interesting but hasn't helped me solve the problem)
Here's the exact code I'm using (in D6.14)

<?php
function customreg_form_user_register_alter (&$form, $form_state) { 
  drupal_set_message('<pre>'. print_r($form, 1) .'</pre>');
$form['<Subscriptions>']['<profile_iwitness_subscriber>']['#default_value'] = 'TRUE';
}

So my little module's name is "customreg" and the "iwitness subscriber" checkbox is in the "Subscriptions" category on my user profile page. Specifically:

[Subscriptions] => Array
(
[#type] => fieldset
[#title] => Subscriptions
[#weight] => 2
[profile_iwitness_subscriber] => Array
(
[#type] => checkbox
[#title] => Free Subscription to the iWitness, our monthly email newsletter
[#default_value] =>
[#description] => You can unsubscribe at any time.
[#required] => 0
)
)

If anyone can spot the error here, I'd be extremely grateful.

enochr00t’s picture

Try it without the angle brackets:
$form['Subscriptions']['profile_iwitness_subscriber']['#default_value'] = 'TRUE';

colemanw’s picture

Thanks... I've learned a lot about php in the last 6 months -- I feel like a different person than the one who posted that comment back in December. And of course, you're right, those angle brackets shouldn't be there because they're not part of the original array key string. (before, I didn't even know what an array was, or form api, or any api for that matter).
But I'm now using civicrm to manage subscriptions, so that little checkbox didn't make it onto my website anyway :>

henrykuo’s picture

Thank you tbartels!

As always, Drupal can be an incredibly frustrating experience, with terrible documentation for beginners, tutorials making so many assumptions of the reader. Spent an entire day reading through other posts mentioning solving this with hooks, but absolutely nothing at all in how to actually implement them! Lots of lovely code, but no one ever ever mentioned where that code would actually go. One post did in fact mention it should go in so-and-so.module, but never having created a custom module, I assumed that meant it should be inserted into an existing module, so I spent another couple hours searching for which module to insert the code into. Ugh!

But your simple straightforward post spelled it out bright as day as to how to create this custom module to solve this in nearly 1/100th the explanation that Drupal's own documentation goes into creating a custom module. I know there's a ton more to learn, but your simple post showed me a simple path in, whereas everything else I read just kept me running around in circles.