You are here

public function CustomerInfoPane::process in Ubercart 8.4

Processes a checkout pane.

Parameters

\Drupal\uc_order\OrderInterface $order: The order that is being processed.

array $form: The checkout form array.

\Drupal\Core\Form\FormStateInterface $form_state: The checkout form state array.

Return value

bool TRUE if the pane is valid, FALSE otherwise.

Overrides CheckoutPanePluginBase::process

File

uc_cart/src/Plugin/Ubercart/CheckoutPane/CustomerInfoPane.php, line 100

Class

CustomerInfoPane
Gets the user's email address for login.

Namespace

Drupal\uc_cart\Plugin\Ubercart\CheckoutPane

Code

public function process(OrderInterface $order, array $form, FormStateInterface $form_state) {
  if (\Drupal::currentUser()
    ->isAnonymous()) {
    $cart_config = \Drupal::config('uc_cart.settings');
    $pane = $form_state
      ->getValue([
      'panes',
      'customer',
    ]);
    $order
      ->setEmail($pane['primary_email']);

    // Check if the email address is already taken.
    $mail_taken = (bool) \Drupal::entityQuery('user')
      ->condition('mail', $pane['primary_email'])
      ->range(0, 1)
      ->count()
      ->execute();
    if ($cart_config
      ->get('email_validation') && $pane['primary_email'] !== $pane['primary_email_confirm']) {
      $form_state
        ->setErrorByName('panes][customer][primary_email_confirm', $this
        ->t('The e-mail address did not match.'));
    }

    // Invalidate if an account already exists for this e-mail address,
    // and the user is not logged into that account.
    if (!$cart_config
      ->get('mail_existing') && !empty($pane['primary_email']) && $mail_taken) {
      $form_state
        ->setErrorByName('panes][customer][primary_email', $this
        ->t('An account already exists for your e-mail address. You will either need to login with this e-mail address or use a different e-mail address.'));
    }

    // If new users can specify names or passwords then...
    if ($cart_config
      ->get('new_account_name') || $cart_config
      ->get('new_account_password')) {

      // Skip if an account already exists for this e-mail address.
      if ($cart_config
        ->get('mail_existing') && $mail_taken) {
        $this
          ->messenger()
          ->addMessage($this
          ->t('An account already exists for your e-mail address. The new account details you entered will be disregarded.'));
      }
      else {

        // Validate the username.
        if ($cart_config
          ->get('new_account_name') && !empty($pane['new_account']['name'])) {
          $message = user_validate_name($pane['new_account']['name']);
          $name_taken = (bool) \Drupal::entityQuery('user')
            ->condition('name', $pane['new_account']['name'])
            ->range(0, 1)
            ->count()
            ->execute();
          if (!empty($message)) {
            $form_state
              ->setErrorByName('panes][customer][new_account][name', $message);
          }
          elseif ($name_taken) {
            $form_state
              ->setErrorByName('panes][customer][new_account][name', $this
              ->t('The username %name is already taken. Please enter a different name or leave the field blank for your username to be your e-mail address.', [
              '%name' => $pane['new_account']['name'],
            ]));
          }
          else {
            $order->data->new_user_name = $pane['new_account']['name'];
          }
        }

        // Validate the password.
        if ($cart_config
          ->get('new_account_password')) {
          if (strcmp($pane['new_account']['pass'], $pane['new_account']['pass_confirm'])) {
            $form_state
              ->setErrorByName('panes][customer][new_account][pass_confirm', $this
              ->t('The passwords you entered did not match. Please try again.'));
          }
          if (!empty($pane['new_account']['pass'])) {
            $order->data->new_user_hash = \Drupal::service('password')
              ->hash(trim($pane['new_account']['pass']));
          }
        }
      }
    }
  }
  return TRUE;
}