View source
<?php
function rpt_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'user_admin_settings':
$form['registration_cancellation']['rpt_password_generate'] = array(
'#type' => 'checkbox',
'#title' => t('Generate password automatically'),
'#description' => t('Hide password fields on user register form.'),
'#default_value' => variable_get('rpt_password_generate', 0),
'#weight' => 0,
);
$form['registration_cancellation']['rpt_password_length'] = array(
'#type' => 'textfield',
'#title' => t('Password length'),
'#description' => t('Length for generated password.'),
'#default_value' => variable_get('rpt_password_length', 10),
'#weight' => 1,
);
foreach ($form as $key => $option) {
if (is_array($option) && isset($option['#group']) && $option['#group'] == 'email') {
$form[$key]['#description'] = str_replace('[user:name]', '[user:name], [user:password]', $option['#description']);
}
}
break;
case 'user_register_form':
$generate_password = variable_get('rpt_password_generate', 0);
if ($generate_password) {
$form['account']['pass']['#type'] = 'value';
$password_length = variable_get('rpt_password_length', 10);
$form['account']['pass']['#value'] = user_password($password_length);
}
break;
case 'user_profile_form':
array_unshift($form['#submit'], 'rpt_user_profile_form_submit');
break;
}
}
function rpt_user_profile_form_submit(&$form, &$form_state) {
$form_state['user']->password = $form_state['values']['pass'];
}
function rpt_token_info() {
$info['tokens']['user']['password'] = array(
'name' => t('User password'),
'description' => t('Provides user password. May be used only during registration.'),
);
return $info;
}
function rpt_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
foreach ($tokens as $name => $value) {
if ($name == 'password') {
if (isset($data['user']) && isset($data['user']->password)) {
$replacements['[user:password]'] = $data['user']->password;
$replacements['[account:password]'] = $data['user']->password;
}
elseif (isset($data['user']) && !isset($data['user']->password)) {
$replacements['[user:password]'] = t('Your password');
$replacements['[account:password]'] = t('Your password');
}
}
}
return $replacements;
}