You are here

public function UserEmailVerificationRequestForm::buildForm in User email verification 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/UserEmailVerificationRequestForm.php, line 92

Class

UserEmailVerificationRequestForm
Class UserEmailVerificationRequestForm.

Namespace

Drupal\user_email_verification\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Prevent form usage with users who already verified email.
  if ($this->currentUser
    ->isAuthenticated() && !$this->userEmailVerification
    ->isVerificationNeeded()) {
    return [
      'notification' => [
        '#markup' => $this
          ->t('Your Email %email was already verified.', [
          '%email' => $this->currentUser
            ->getEmail(),
        ]),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
      ],
    ];
  }
  if ($this->currentUser
    ->isAnonymous()) {
    $form['name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Username or Email address'),
      '#size' => 40,
      '#maxlength' => max(UserInterface::USERNAME_MAX_LENGTH, Email::EMAIL_MAX_LENGTH),
      '#required' => TRUE,
      '#default_value' => $this->request->query
        ->get('name', ''),
    ];
  }
  else {
    $form['name'] = [
      '#type' => 'value',
      '#value' => $this->currentUser
        ->getEmail(),
    ];
    $form['mail'] = [
      '#markup' => $this
        ->t('Verify email message will be send to %email.', [
        '%email' => $this->currentUser
          ->getEmail(),
      ]),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    ];
  }
  $form['uid'] = [
    '#type' => 'value',
    '#value' => 0,
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Send verify mail'),
  ];
  return $form;
}