I have altered the user registration page with the Content Profile module and a custom module of my own:
http://img525.imageshack.us/img525/2433/useraccountozindustry12.png

This is the code from my module:

<?php
function customregister_form_alter(&$form, $form_state, $form_id) {
	if ($form_id === 'user_register') {

		//var_dump($form);
		
		/********* contact details *********/
		// change fieldset title
		$form['account']['#title'] = t('Contact details');
		
		// change username field
		$form['account']['name']['#title'] = t('Company Name');
		$form['account']['name']['#description'] = FALSE;
		$form['account']['name']['#weight'] = 1;
		
		// change email field
		$form['account']['mail']['#title'] = t('Email');
		$form['account']['mail']['#description'] = FALSE;
		$form['account']['mail']['#weight'] = 3;

		// move fields added by content profile to the account fieldset
		$form['account']['contact_name'] = $form['contact']['profile_company_contact'];
		$form['account']['phone'] = $form['contact']['profile_company_phone'];
		$form['account']['fax'] = $form['contact']['profile_company_fax'];
		
		$form['account']['contact_name']['#weight'] = 2;
		$form['account']['phone']['#weight'] = 4;
		$form['account']['fax']['#weight'] = 5;	
		
		unset($form['contact']['profile_company_phone']);
		unset($form['contact']['profile_company_contact']);
		unset($form['contact']['profile_company_fax']);
					
		
		/********* business details *********/
		// change the extra fields fieldset title
		$form['Business details']['#title'] = t('Business details');
		
		// change the picture fieldset title
		$form['picture']['#title'] = t('Logo');
		$form['picture']['picture_upload_register']['#title'] = t('Logo');
		$form['picture']['picture_upload_register']['#description'] = FALSE;
		
		// move picture field to business details fieldset
		$form['Business details']['logo'] = $form['picture']['picture_upload_register'];
		unset($form['picture']);
		
		// add weight
		$form['Business details']['profile_company_newsletter']['#weight'] = 9;
		$form['Business details']['#weight'] = 9;
		
		/********* location details *********/
		$form['contact']['#title'] = t('Location');
		
		/********* validate and submit *********/
		// validate fields
		$form['#validate'][] = 'customregister_user_register_validate';	

		// submit
		$form['#submit'][] = 'customregister_user_register_submit';
	
	}
}

function customregister_user_register_validate($form, &$form_state) {
	
}
function customregister_user_register_submit($form, &$form_state) {

}

?>

But what do i put in the validate and submit functions?

If I enter data into the fields now, the fields that I moved (from fieldset to fieldset) aren't submitted to the user's profile. All I did was move the fields around. I didn't add any new ones of my own.

Comments

nevets’s picture

Moving elements between field sets can cause problems if the fieldset has '#tree' set to TRUE. Even if not set, code like

        $form['account']['contact_name'] = $form['contact']['profile_company_contact'];

because it not only moves the field but renames it which will cause the information not to be saved.

For simply changing labels you might want to look at String Override which also you to change the text through its settings page.

tinny’s picture

How can I tell if it has #tree set to true?

How can I do this without String Overrides?

tinny’s picture

ok lets say i added a new field. How do i submit it so that it shows up in the profile?

tinny’s picture

Using the default profile module and Content Profile (with Content Profile User Registration enabled) module, I added these fields:
http://img526.imageshack.us/img526/5322/profilesozindustry12907.png

So now the user registration form looks like this:
http://img841.imageshack.us/img841/5215/useraccountmywebsite129.png

Then I created a module of my own called customregister.module (code below) and now the form looks like this:
http://img251.imageshack.us/img251/5215/useraccountmywebsite129.png

function customregister_form_alter(&$form, $form_state, $form_id) {
	if ($form_id === 'user_register') {

		//var_dump($form);
		
		// change username field
		$form['Business details']['name'] = $form['account']['name'];
		$form['Business details']['name']['#title'] = t('Company name');
		$form['Business details']['name']['#description'] = FALSE;
		$form['Business details']['name']['#weight'] = -13;
		unset($form['account']['name']);
		
		// change email field
		$form['Business details']['mail'] = $form['account']['mail'];
		$form['Business details']['mail']['#title'] = t('Contact email');
		$form['Business details']['mail']['#description'] = FALSE;
		$form['Business details']['mail']['#weight'] = -11;
		unset($form['account']['mail']);
		
		// unset account information fieldset
		unset($form['account']);
		
		// change contact name field
		$form['Business details']['profile_company_contact']['#weight'] = -12;
		
		// change website field
		$form['Business details']['profile_company_website']['#default_value'] = t('http://');
		
		// change picture field
		$form['Business details']['pic'] = $form['picture']['picture_upload_register'];
		$form['Business details']['pic']['#title'] = t('Upload logo');
		$form['Business details']['pic']['#description'] = FALSE;
		unset($form['picture']);
		
		// change checkbox 
		$form['Business details']['profile_company_newsletter']['#weight'] = 99;
		
		// change submit button
		$form['submit']['#value'] = t('Add my business - FREE');
	
	}
}

HOW TO:
Step 1: Add some fields with content profile and enable Content Profile User Registration. (http://img526.imageshack.us/img526/5322/profilesozindustry12907.png)
Step 2: create a custom module with this code to see the structure of the form:

function customregister_form_alter(&$form, $form_state, $form_id) {

	if ($form_id === 'user_register') {
		var_dump($form);
	}

Then go back to the reg form (http://localhost/user/register) and you will see something like this:
http://img196.imageshack.us/img196/9302/useraccountvardump.png

Step 3: Right click and select View page source. You will then see something like this:
http://img153.imageshack.us/img153/944/sourcepage.png

Step 4: Now to change some things in the form:
http://img51.imageshack.us/img51/8868/sourcepagedisect.png

function customregister_form_alter(&$form, $form_state, $form_id) {
	if ($form_id === 'user_register') {
		//var_dump($form);
		
		// change fieldset title
		$form['account']['#title'] = t('Info for user account blah blah');
		
		// change username field
		$form['account']['name']['#title'] = t('Name of user person');
		$form['account']['name']['#description'] = FALSE;
		$form['account']['name']['#maxlength'] = 16;
		
		// change email field
		$form['account']['mail']['#title'] = t('Your email');
	}
}
light9’s picture

thank you very much!

tinny’s picture

Edit: you don't actually need the Content Profile module for any of this.

Edit 2: the picture field comes from a module called reg_with_pic. This allows people to upload an image upon registration.

Edit 3:
Also, the code which says:
// change submit button
$form['submit']['#value'] = t('Add my business - FREE');

This is WRONG. Users can register at user/register but creating a user through admin/user/user/create doesnt work. I don't know why it doesn't work.

You can change the weight of the submit button eg

$form['submit']['#weight'] = 9;

but not the value.

Edit 4:
if ($form_id === 'user_register') SHOULD BE
if ($form_id == 'user_register')
double equal not triple

jgrubb’s picture

Holy cow, is that annoying but thank you for posting this. I've been diddling with this custom user registration form for two weeks now, and as it turns out if you try to change the ['#value'] of the submit button you will derail the entire thing. That's got to be a bug, right?

Jerimee’s picture

This is WRONG. Users can register at user/register but creating a user through admin/user/user/create doesnt work. I don't know why it doesn't work.

Ditto. Changing the value of the user_register submit button causes admins to lose the ability to create users.

Anyone know another way to do this? Is this a bug or is there a right way to do it?

In the meantime, the stringoverrides modules successfully changes the text of the user_register submit button:

http://drupal.org/project/stringoverrides

Vishwadeep-1’s picture

Is it possible to check or set the #tree state to false in this way:
$form[account][name]['#tree']=FALSE;