You are here

public function OrderCreateForm::submitForm in Ubercart 8.4

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

uc_order/src/Form/OrderCreateForm.php, line 265

Class

OrderCreateForm
Creates a new order and redirect to its edit screen.

Namespace

Drupal\uc_order\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  switch ($form_state
    ->getValue('customer_type')) {
    case 'search':
      $uid = $form_state
        ->getValue([
        'customer',
        'uid',
      ]);
      break;
    case 'create':

      // Create new account.
      $email = trim($form_state
        ->getValue([
        'customer',
        'email',
      ]));
      $fields = [
        'name' => uc_store_email_to_username($email),
        'mail' => $email,
        // @todo Replace the following line with:
        // 'pass' => \Drupal::service('password_generator')->generate(),
        // when Drupal 9.1 becomes the lowest-supported version of Drupal.
        // @see https://www.drupal.org/project/ubercart/issues/3217319
        'pass' => user_password(),
        'status' => $this
          ->config('uc_cart.settings')
          ->get('new_customer_status_active') ? 1 : 0,
      ];
      $account = User::create($fields);
      $account
        ->save();
      $uid = $account
        ->id();
      if ($form_state
        ->getValue([
        'customer',
        'sendmail',
      ])) {

        // Manually set the password so it appears in the e-mail.
        $account->password = $fields['pass'];
        $this->mailManager
          ->mail('user', 'register_admin_created', $email, uc_store_mail_recipient_langcode($email), [
          'account' => $account,
        ], uc_store_email_from());
        $this
          ->messenger()
          ->addMessage($this
          ->t('A welcome message has been e-mailed to the new user.'));
      }
      break;
    default:
      $uid = 0;
  }
  $order = Order::create([
    'uid' => $uid,
    'order_status' => uc_order_state_default('post_checkout'),
  ]);
  $order
    ->save();
  uc_order_comment_save($order
    ->id(), $this
    ->currentUser()
    ->id(), $this
    ->t('Order created by the administration.'), 'admin');
  $form_state
    ->setRedirect('entity.uc_order.edit_form', [
    'uc_order' => $order
      ->id(),
  ]);
}