/** * {@inheritdoc} */ public function process(OrderInterface $order, array $form, FormStateInterface $form_state) { $user = \Drupal::currentUser(); $cart_config = \Drupal::config('uc_cart.settings'); $pane = $form_state->getValue(['panes', 'customer']); $order->setEmail($pane['primary_email']); if ($user->isAuthenticated()) { $order->setUserId($user->id()); } else { // 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', 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', 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) { drupal_set_message(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', 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.', array('%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', 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; }