You are here

public function LogintobogganRegister::form in LoginToboggan 8

Gets the actual form array to be built.

Overrides RegisterForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/LogintobogganRegister.php, line 20

Class

LogintobogganRegister
Provide alternative registration form to include LT components and submit.

Namespace

Drupal\logintoboggan\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $user = $this
    ->currentUser();

  /** @var \Drupal\user\UserInterface $account */
  $account = $this->entity;
  $admin = $user
    ->hasPermission('administer users');

  // Pass access information to the submit handler. Running an access check
  // inside the submit function interferes with form processing and breaks
  // hook_form_alter().
  $form['administer_users'] = [
    '#type' => 'value',
    '#value' => $admin,
  ];
  $form['#attached']['library'][] = 'core/drupal.form';

  // For non-admin users, populate the form fields using data from the
  // browser.
  if (!$admin) {
    $form['#attributes']['data-user-info-from-browser'] = TRUE;
  }

  // Because the user status has security implications, users are blocked by
  // default when created programmatically and need to be actively activated
  // if needed. When administrators create users from the user interface,
  // however, we assume that they should be created as activated by default.
  if ($admin) {
    $account
      ->activate();
  }

  // Start with the default user account fields.
  $form = parent::form($form, $form_state, $account);

  // Check setting for email confirmation.
  $mail_confirm = $this
    ->config('logintoboggan.settings')
    ->get('confirm_email_at_registration');

  // Display a confirm e-mail address box if option is enabled.
  if ($mail_confirm) {
    $form['account']['conf_mail'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Confirm e-mail address'),
      '#weight' => -28,
      '#maxlength' => 64,
      '#description' => $this
        ->t('Please re-type your e-mail address to confirm it is accurate.'),
      '#required' => TRUE,
    ];

    // Weight things properly so that the order is name, mail, conf_mail.
    $form['account']['name']['#weight'] = -30;
    $form['account']['mail']['#weight'] = -29;
  }
  $pass = $this
    ->config('user.settings')
    ->get('user_email_verification');
  $min_pass = \Drupal::config('logintoboggan.settings')
    ->get('minimum_password_length', 0);
  if ($pass && $min_pass > 0) {
    $form['account']['pass']['#description'] = isset($form['account']['pass']['#description']) ? $form['account']['pass']['#description'] . " " : "";
    $form['account']['pass']['#description'] .= $this
      ->t('Password must be at least %length characters.', [
      '%length' => $min_pass,
    ]);
  }
  return $form;
}