You are here

function regcode_form_user_register_form_alter in Registration codes 8

Same name and namespace in other branches
  1. 7.2 regcode.module \regcode_form_user_register_form_alter()
  2. 7 regcode.module \regcode_form_user_register_form_alter()

Implements hook_form_FORM_ID_alter() for user_register_form.

File

./regcode.module, line 70
Main functionality and hooks of regcode module.

Code

function regcode_form_user_register_form_alter(&$form, FormStateInterface $form_state) {

  // Only display the regcode field when it's attached to the form display.
  if ($form_state
    ->get('form_display')
    ->getComponent('regcode')) {
    $config = \Drupal::config('regcode.settings');
    $form['regcode'] = [
      '#type' => 'textfield',
      '#title' => Html::escape($config
        ->get('regcode_field_title')),
      '#description' => Html::escape($config
        ->get('regcode_field_description')),
      '#required' => !($config
        ->get('regcode_optional') || \Drupal::currentUser()
        ->hasPermission('administer users')),
      '#element_validate' => [
        'regcode_code_element_validate',
      ],
    ];
    $form['actions']['submit']['#submit'][] = 'regcode_user_register_form_submit_handler';

    // Capture the code from the URL, if present, and inject it into the
    // registration form.
    if (\Drupal::request()->query
      ->has('regcode')) {

      // The Form API can handle potentially unsafe characters as long as they
      // are not printed directly. This code gets trimmed in
      // regcode_code_validate().
      $form['regcode']['#value'] = \Drupal::request()->query
        ->get('regcode');
      $form['regcode']['#description'] = NULL;
      $form['regcode']['#disabled'] = TRUE;
    }
  }
}