You are here

rpt.module in Registration Password Token 7

Same filename and directory in other branches
  1. 8 rpt.module

File

rpt.module
View source
<?php

/**
 * Implements hook_form_alter().
 */
function rpt_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'user_admin_settings':

      // Add checkbox with setting of password automatic generation.
      $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':

      // Hide password field and generate password for user.
      $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;
  }
}

/**
 * Submit handler for 'user_profile_form' form.
 */
function rpt_user_profile_form_submit(&$form, &$form_state) {
  $form_state['user']->password = $form_state['values']['pass'];
}

/**
 * Implements hook_token_info().
 */
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;
}

/**
 * Implements hook_tokens().
 */
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;
}

Functions

Namesort descending Description
rpt_form_alter Implements hook_form_alter().
rpt_tokens Implements hook_tokens().
rpt_token_info Implements hook_token_info().
rpt_user_profile_form_submit Submit handler for 'user_profile_form' form.