Commit c49b9da6 authored by Steve Gerbino's avatar Steve Gerbino

Issue #1182552 by Vasu: Implemented middle name field capabilities,

including both username generation and tokens.
parent 9a7ac216
......@@ -27,6 +27,19 @@ function realname_registration_settings_form() {
'#title' => t('First name field'),
'#default_value' => variable_get('realname_registration_firstname_field'),
'#description' => t("The name of your first name field. Ensure that:").$field_req,
'#required' => TRUE,
);
$form['realname_registration_middlename_field'] = array(
'#type' => 'textfield',
'#title' => t('Middle name field'),
'#default_value' => variable_get('realname_registration_middlename_field'),
'#description' =>
t("The name of your middle name field. Ensure that:")
. '<ul>'
. '<li>' . t('The field name is correct and represents an existing field') . '</li>'
. '<li>' . t('The field is associated with the') . ' <em>' . t('user') . '</em> ' . t('entity') . '</li>'
. '</ul>',
);
$form['realname_registration_lastname_field'] = array(
......@@ -34,6 +47,7 @@ function realname_registration_settings_form() {
'#title' => t('Last name field'),
'#default_value' => variable_get('realname_registration_lastname_field'),
'#description' => t("The name of your last name field. Ensure that:").$field_req,
'#required' => TRUE,
);
$form['realname_registration_format'] = array(
......@@ -41,7 +55,12 @@ function realname_registration_settings_form() {
'#title' => t('Username format'),
'#description' => t('Select the format in which Realname registration will create new usernames.'),
'#default_value' => variable_get('realname_registration_format', 0),
'#options' => array(t('First name and last name separated by a space (e.g., John Smith)'), t('First initial and last name (e.g., JSmith)')),
'#options' => array(
t('First name and last name separated by a space (e.g., John Smith)'),
t('First initial and last name (e.g., JSmith)'),
t('First name, middle name, and last name (e.g., John Jacob Smith)'),
t('First initial, middle initial, and last name (e.g., JJSmith)')
),
'#required' => TRUE,
);
......@@ -73,6 +92,7 @@ function realname_registration_settings_form() {
function realname_registration_settings_validate($form, &$form_state) {
// Retrieve the user instance and bundle using the field names provided.
$firstname_instance = field_info_instance('user', $form_state['values']['realname_registration_firstname_field'], 'user');
$middlename_instance = field_info_instance('user', $form_state['values']['realname_registration_middlename_field'], 'user');
$lastname_instance = field_info_instance('user', $form_state['values']['realname_registration_lastname_field'], 'user');
$err_pretext = t('The field name').', <em>';
......@@ -80,10 +100,19 @@ function realname_registration_settings_validate($form, &$form_state) {
$err_unknown = '</em>, '.t("you've provided either does not exist or is not associated with the user entity and bundle.");
$err_textfield = '</em>, '.t('must use textfield widget.');
if($form_state['values']['realname_registration_format'] == 2 || $form_state['values']['realname_registration_format'] == 3) {
if( empty($form_state['values']['realname_registration_middlename_field']) ) {
form_set_error('realname_registration_format', t('A valid middle name field is required for this username format.'));
}
}
// We must check to see if the provided fields return a valid instance.
if(!isset($firstname_instance)) {
form_set_error('realname_registration_firstname_field', $err_pretext.$form_state['values']['realname_registration_firstname_field'].$err_unknown);
}
if(!isset($middlename_instance)) {
form_set_error('realname_registration_middlename_field', $err_pretext.$form_state['values']['realname_registration_middlename_field'].$err_unknown);
}
if(!isset($lastname_instance)) {
form_set_error('realname_registration_lastname_field', $err_pretext.$form_state['values']['realname_registration_lastname_field'].$err_unknown);
}
......@@ -92,11 +121,14 @@ function realname_registration_settings_validate($form, &$form_state) {
if($firstname_instance['widget']['type'] != 'text_textfield') {
form_set_error('realname_registration_firstname_field', $err_pretext.$form_state['values']['realname_registration_firstname_field'].$err_textfield);
}
if($middlename_instance['widget']['type'] != 'text_textfield') {
form_set_error('realname_registration_middlename_field', $err_pretext.$form_state['values']['realname_registration_middlename_field'].$err_textfield);
}
if($lastname_instance['widget']['type'] != 'text_textfield') {
form_set_error('realname_registration_lastname_field', $err_pretext.$form_state['values']['realname_registration_lastname_field'].$err_textfield);
}
// Because the field is used in creating usernames, it should be required.
// Because the first and last name fields are needed to create usernames, it should be required.
if($firstname_instance['required'] != 1) {
form_set_error('realname_registration_firstname_field', $err_pretext.$form_state['values']['realname_registration_firstname_field'].$err_req);
}
......
......@@ -44,6 +44,7 @@ function realname_registration_form_alter(&$form, $form_state, $form_id) {
}
$lastname_field = variable_get('realname_registration_lastname_field');
$middlename_field = variable_get('realname_registration_middlename_field');
$firstname_field = variable_get('realname_registration_firstname_field');
if(isset($lastname_field) && isset($firstname_field)) {
......@@ -57,7 +58,12 @@ function realname_registration_form_alter(&$form, $form_state, $form_id) {
}
$form[$firstname_field]['#weight'] = -500;
$form[$lastname_field]['#weight'] = -499;
if(!empty($middlename_field)) {
$form[$middlename_field]['#weight'] = -499;
}
$form[$lastname_field]['#weight'] = -498;
if(variable_get('realname_registration_use_validation')) {
$form['#validate'][] = 'realname_registration_validate';
......@@ -71,22 +77,43 @@ function realname_registration_form_alter(&$form, $form_state, $form_id) {
function realname_registration_user_presave(&$edit, $account, $category) {
if(isset($edit['name'])) {
if ($edit['name'] == 'unset_username') {
$lastname_field = variable_get('realname_registration_lastname_field');
$firstname_field = variable_get('realname_registration_firstname_field');
$middlename_field = variable_get('realname_registration_middlename_field');
$lastname_field = variable_get('realname_registration_lastname_field');
if((variable_get('realname_registration_ucfirst')) == 1) {
$edit[$firstname_field]['und'][0]['value'] = ucfirst($edit[$firstname_field]['und'][0]['value']);
if(!empty($edit[$middlename_field]['und'][0]['value'])) {
$edit[$middlename_field]['und'][0]['value'] = ucfirst($edit[$middlename_field]['und'][0]['value']);
}
$edit[$lastname_field]['und']['0']['value'] = ucfirst($edit[$lastname_field]['und'][0]['value']);
}
// Generate the username based on first name and last name fields.
if((variable_get('realname_registration_format')) == 0) {
$username = $edit[$firstname_field]['und'][0]['value']." ".$edit[$lastname_field]['und'][0]['value'];
$username = $edit[$firstname_field]['und'][0]['value'] . " " . $edit[$lastname_field]['und'][0]['value'];
}
if((variable_get('realname_registration_format')) == 1) {
$first_init = drupal_substr($edit[$firstname_field]['und'][0]['value'], 0, 1);
$lastname = $edit[$lastname_field]['und'][0]['value'];
$username = $first_init.$lastname;
$username = $first_init . $lastname;
}
if((variable_get('realname_registration_format')) == 2) {
if(!empty($edit[$middlename_field]['und'][0]['value'])) {
$username = $edit[$firstname_field]['und'][0]['value'] . " " . $edit[$middlename_field]['und'][0]['value'] . " " . $edit[$lastname_field]['und'][0]['value'];
}
else {
$username = $edit[$firstname_field]['und'][0]['value'] . " " . $edit[$lastname_field]['und'][0]['value'];
}
}
if((variable_get('realname_registration_format')) == 3) {
$first_init = drupal_substr($edit[$firstname_field]['und'][0]['value'], 0, 1);
if(!empty($edit[$middlename_field]['und'][0]['value'])) {
$middle_init = drupal_substr($edit[$middlename_field]['und'][0]['value'], 0, 1);
}
$lastname = $edit[$lastname_field]['und'][0]['value'];
$username = $first_init . $middle_init . $lastname;
}
if(variable_get('realname_registration_tolower')) {
$username = strtolower($username);
}
......@@ -98,7 +125,7 @@ function realname_registration_user_presave(&$edit, $account, $category) {
$i = 0;
while ($result->rowCount()) {
$result = db_query("SELECT name FROM {users} WHERE name = :username", array(':username' => $username . ++$i));
if (!($result->rowCount())) {
if(!($result->rowCount())) {
$username = $username . $i;
}
}
......@@ -115,13 +142,20 @@ function realname_registration_user_presave(&$edit, $account, $category) {
* @see user_register_submit()
*/
function realname_registration_validate($form, &$form_state) {
$lastname_field = variable_get('realname_registration_lastname_field');
$firstname_field = variable_get('realname_registration_firstname_field');
$middlename_field = variable_get('realname_registration_middlename_field');
$lastname_field = variable_get('realname_registration_lastname_field');
// A first name must be letters only.
if (!preg_match("/^[A-ZÀ-ÖØ-öø-ÿ]+$/i", $form_state['values'][$firstname_field]['und'][0]['value'])) {
form_set_error($firstname_field, t('First name must only contain letters.'));
}
// A first name must be letters only.
if (!preg_match("/^[A-ZÀ-ÖØ-öø-ÿ]+$/i", $form_state['values'][$middlename_field]['und'][0]['value'])) {
form_set_error($middlename_field, t('Middle name must only contain letters.'));
}
// A last name must be letters only.
if (!preg_match("/^[A-ZÀ-ÖØ-öø-ÿ]+$/i", $form_state['values'][$lastname_field]['und'][0]['value'])) {
form_set_error($lastname_field, t('Last name must only contain letters.'));
......
......@@ -12,6 +12,10 @@ function realname_registration_token_info() {
'name' => t('First name'),
'description' => t("The first name of the user."),
);
$user['middle-name'] = array(
'name' => t('Middle name'),
'description' => t("The middle name of the user."),
);
$user['last-name'] = array(
'name' => t('Last name'),
'description' => t("The last name of the user."),
......@@ -20,6 +24,10 @@ function realname_registration_token_info() {
'name' => t('First inital'),
'description' => t("The first initial of the user."),
);
$user['middle-initial'] = array(
'name' => t('Middle inital'),
'description' => t("The middle initial of the user."),
);
$user['last-initial'] = array(
'name' => t('Last initial'),
'description' => t("The last initial of the user."),
......@@ -36,7 +44,9 @@ function realname_registration_tokens($type, $tokens, array $data = array(), arr
$replacements = array();
$field_firstname = variable_get('realname_registration_firstname_field');
$field_middlename = variable_get('realname_registration_middlename_field');
$field_lastname = variable_get('realname_registration_lastname_field');
if(isset($field_firstname) && isset($field_lastname)) {
if ($type == 'user' && !empty($data['user'])) {
$account = $data['user'];
......@@ -46,6 +56,10 @@ function realname_registration_tokens($type, $tokens, array $data = array(), arr
$replacements[$original] = !empty($account->$field_firstname) ? $account->{$field_firstname}['und']['0']['value'] : '';
break;
case 'middle-name':
$replacements[$original] = !empty($account->$field_middlename) ? $account->{$field_middlename}['und']['0']['value'] : '';
break;
case 'last-name':
$replacements[$original] = !empty($account->$field_lastname) ? $account->{$field_lastname}['und']['0']['value'] : '';
break;
......@@ -54,6 +68,10 @@ function realname_registration_tokens($type, $tokens, array $data = array(), arr
$replacements[$original] = !empty($account->$field_firstname) ? drupal_substr($account->{$field_firstname}['und']['0']['value'], 0, 1) : '';
break;
case 'middle-initial':
$replacements[$original] = !empty($account->$field_middlename) ? drupal_substr($account->{$field_middlename}['und']['0']['value'], 0, 1) : '';
break;
case 'last-initial':
$replacements[$original] = !empty($account->$field_lastname) ? drupal_substr($account->{$field_lastname}['und']['0']['value'], 0, 1) : '';
break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment