You are here

public function EmailRegistrationLogin::validatePaneForm in Email Registration 8

Validates the pane form.

Parameters

array $pane_form: The pane form.

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

array $complete_form: The complete form structure.

Overrides Login::validatePaneForm

File

src/Plugin/Commerce/CheckoutPane/EmailRegistrationLogin.php, line 102

Class

EmailRegistrationLogin
Provides the email registration login pane.

Namespace

Drupal\email_registration\Plugin\Commerce\CheckoutPane

Code

public function validatePaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
  $subformId = $pane_form['#parents'][0];
  $values = $form_state
    ->getValue($pane_form['#parents']);
  $triggering_element = $form_state
    ->getTriggeringElement();
  if ($triggering_element['#op'] === 'login') {
    $mail = $values['returning_customer']['name'];
    if (!empty($mail)) {

      // Try to load the user by mail.
      $user = user_load_by_mail($mail);
    }
    if (empty($user)) {

      // Check if users are allowed to login with username as well.
      if (!$this->config
        ->get('login_with_username')) {

        // Users are not allowed to login with username. Since no user was
        // found with the specified mail address, fail with an error and
        // bail out.
        $user_input = $form_state
          ->getUserInput();
        $query = isset($user_input[$subformId]['returning_customer']['name']) ? [
          'name' => $user_input[$subformId]['returning_customer']['name'],
        ] : [];
        $form_state
          ->setError($pane_form['returning_customer'], $this
          ->t('Unrecognized email address or password. <a href=":password">Forgot your password?</a>', [
          ':password' => Url::fromRoute('user.pass', [], [
            'query' => $query,
          ])
            ->toString(),
        ]));
        return;
      }
    }
    else {

      // We have found an user! Save username on the form state, as that is
      // what the parent class expects in their submit handler.
      $username = $user
        ->getAccountName();
      $form_state
        ->setValue([
        $subformId,
        'returning_customer',
        'name',
      ], $username);

      // Perform several checks for this user account
      // Copied from parent to override error messages.
      $name_element = $pane_form['returning_customer']['name'];
      $password = trim($values['returning_customer']['password']);

      // Generate the "reset password" url.
      $query = !empty($username) ? [
        'name' => $username,
      ] : [];
      $password_url = Url::fromRoute('user.pass', [], [
        'query' => $query,
      ])
        ->toString();
      if (user_is_blocked($username)) {
        $form_state
          ->setError($name_element, $this
          ->t('The account with email address %mail has not been activated or is blocked.', [
          '%mail' => $mail,
        ]));
        return;
      }
      $uid = $this->userAuth
        ->authenticate($username, $password);
      if (!$uid) {
        $this->credentialsCheckFlood
          ->register($this->clientIp, $username);

        // Changing the wrong credentials error message.
        if (!$this->config
          ->get('login_with_username')) {
          $form_state
            ->setError($name_element, $this
            ->t('Unrecognized email address or password. <a href=":password">Forgot your password?</a>', [
            ':password' => $password_url,
          ]));

          // Adding return to avoid the parent error when password is empty.
          return;
        }
        else {
          $form_state
            ->setError($name_element, $this
            ->t('Unrecognized username, email, or password. <a href=":url">Have you forgotten your password?</a>', [
            ':url' => $password_url,
          ]));
        }
      }
    }
  }
  parent::validatePaneForm($pane_form, $form_state, $complete_form);
}