I have a custom module (entitled ta_module) : in the user registration form, it hides the e-mail field, and add a custom field where user have to enter what's before @ and then choose his e-mail domain name between the one proposed in a list next to it.
Here's the code :
<?php
/**
* Implementation of hook_form_alter()
*/
function ta_module_form_alter(&$form, &$form_state, $form_id) {
// If the form is the user_register form
if ($form_id == 'user_register_form') {
// Attach our custom validation function as the first validation function
array_unshift($form['#validate'], '_ta_module_user_validate');
// Hide the email address field
$form['account']['mail']['#type'] = 'hidden';
// Give the email address field a random default value (in this case a random number and a timestamp)
// as we cannot unset it due to Drupal's validation rules
$form['account']['mail']['#default_value'] = rand(1111, 9999) . '@' .time() . '.com';
// Add our new text entry field
$form['mail_first'] = array(
'#type' => 'textfield',
'#title' => t('Email address'),
'#default_value' => '',
'#size' => 35,
'#maxlength' => 64,
'#required' => TRUE,
);
// Add our select list of possible email domain values
$form['mail_last'] = array(
'#type' => 'select',
'#title' => 'Domaine',
'#title_display' => 'before',
'#options' => array(
// These are the possible email address domains
'myentreprise.net' => '@myentreprise.net',
'myentreprise.com' => '@myentreprise.com',
),
'#default_value' => 'myentreprise.net',
'#required' => TRUE,
);
}
}
/**
* Function to piece together our email address
*/
function _ta_module_user_validate(&$form, &$form_state) {
// If our two form elements have values
if (!empty($form_state['values']['mail_first']) && !empty($form_state['values']['mail_last'])) {
// Set the email address to the correct value
$form_state['complete form']['account']['mail']['#value'] = $form_state['input']['mail_first'] . '@' . $form_state['input']['mail_last'];
}
}
(see This Q/A for more infos).
All appear quite brightly, but when the user click on "create a new account", the user is created but the last function of my module doesn't work : the e-mail finally saved is the default value (something like [email protected]) instead of the two pieces joined together...
HOW TO FIX THIS : Thus how to assign to mail the value of mail-first + mail-last ?
/!\ I'm working on Drupal 7, so I think it's an issue of uncorrect variables...
Thks for reading ! ;)