You are here

rpt.module in Registration Password Token 8

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

Registration Password Token module.

File

rpt.module
View source
<?php

/**
 * @file
 * Registration Password Token module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BubbleableMetadata;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function rpt_form_user_admin_settings_alter(&$form, FormStateInterface $form_state) {
  $settings = \Drupal::config('rpt.settings');

  // Add checkbox with setting of password automatic generation.
  $form['registration_cancellation']['rpt_password_generate'] = [
    '#type' => 'checkbox',
    '#title' => t('Generate password automatically'),
    '#description' => t('Hide password fields on user register form.'),
    '#default_value' => $settings
      ->get('password_generate'),
    '#weight' => 0,
  ];
  $form['registration_cancellation']['rpt_password_length'] = [
    '#type' => 'textfield',
    '#title' => t('Password length'),
    '#description' => t('Length for generated password.'),
    '#default_value' => $settings
      ->get('password_length'),
    '#weight' => 1,
  ];

  // Add submit handler to save rpt configuration.
  $form['#submit'][] = 'rpt_user_admin_settings_submit';
}

/**
 * Form submission handler for 'user_admin_settings' form.
 *
 * @see rpt_form_user_admin_settings_alter()
 */
function rpt_user_admin_settings_submit($form, FormStateInterface $form_state) {
  \Drupal::configFactory()
    ->getEditable('rpt.settings')
    ->set('password_generate', $form_state
    ->getValue('rpt_password_generate'))
    ->set('password_length', $form_state
    ->getValue('rpt_password_length'))
    ->save();
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function rpt_form_user_register_form_alter(&$form, FormStateInterface $form_state) {
  $settings = \Drupal::config('rpt.settings');

  // Hide password field and generate password for user.
  $generate_password = $settings
    ->get('password_generate');
  if ($generate_password) {
    $form['account']['pass']['#type'] = 'value';
    $password_length = $settings
      ->get('password_length');
    $form['account']['pass']['#value'] = user_password($password_length);
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function rpt_form_user_form_alter(&$form, FormStateInterface $form_state) {
  array_unshift($form['#submit'], 'rpt_user_form_submit');
}

/**
 * Submit handler for 'user_form' form.
 *
 * @see: rpt_form_user_form_alter()
 */
function rpt_user_form_submit($form, FormStateInterface $form_state) {
  $pass = $form_state
    ->getValue('pass');
  if ($pass) {
    $account = $form_state
      ->getFormObject()
      ->getEntity();
    $account->password = $pass;
  }
}

/**
 * Implements hook_token_info().
 */
function rpt_token_info() {
  $info['tokens']['user']['password'] = [
    '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 $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  foreach ($tokens as $name => $value) {
    if ($name === 'password' && isset($data['user'])) {
      if (isset($data['user']->password)) {
        $replacements['[user:password]'] = $data['user']->password;
        $replacements['[account:password]'] = $data['user']->password;
      }
      else {
        $replacements['[user:password]'] = t('Your password');
        $replacements['[account:password]'] = t('Your password');
      }
    }
  }
  return $replacements;
}

Functions

Namesort descending Description
rpt_form_user_admin_settings_alter Implements hook_form_FORM_ID_alter().
rpt_form_user_form_alter Implements hook_form_FORM_ID_alter().
rpt_form_user_register_form_alter Implements hook_form_FORM_ID_alter().
rpt_tokens Implements hook_tokens().
rpt_token_info Implements hook_token_info().
rpt_user_admin_settings_submit Form submission handler for 'user_admin_settings' form.
rpt_user_form_submit Submit handler for 'user_form' form.