You are here

public function DonationForm::submitForm in Commerce Donate 8

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

src/Form/DonationForm.php, line 226

Class

DonationForm
Provides the donation form.

Namespace

Drupal\commerce_donate\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $current_currency = \Drupal::service('commerce_currency_resolver.current_currency');
  $selected_currency = $current_currency
    ->getCurrency();
  $entity_type_manager = \Drupal::service('entity_type.manager');
  $currency = $entity_type_manager
    ->getStorage('commerce_currency')
    ->load($selected_currency);
  $currency_symbol = $currency
    ->getSymbol();
  $donation_freq = $form_state
    ->getValue('frequency');
  if ($donation_freq == 'onetime') {
    $amount = $form_state
      ->getValue('amount_onetime')[0];
  }
  if ($donation_freq == 'monthly') {
    $amount = $form_state
      ->getValue('amount_monthly')[0];
  }
  $currency_formatter = \Drupal::service('commerce_price.currency_formatter');
  $amount_label = $currency_formatter
    ->format($amount, $selected_currency);

  // Single donation - add to cart and checkout.
  if ($donation_freq == 'onetime') {
    $donation_order_item = NULL;
    $store = $this->currentStore
      ->getStore();
    $cart = $this->cartProvider
      ->getCart('default', $store);
    if (!$cart) {
      $cart = $this->cartProvider
        ->createCart('default', $store);
    }

    // Try to find an existing order item.
    foreach ($cart
      ->getItems() as $order_item) {
      if ($order_item
        ->bundle() == 'donation') {
        $cart
          ->removeItem($order_item);
        break;
      }
    }
    $order_item = $this->entityTypeManager
      ->getStorage('commerce_order_item')
      ->create([
      'type' => 'donation',
      'title' => t('@amount donation', [
        '@amount' => $amount_label,
      ]),
      'unit_price' => [
        'number' => $amount,
        'currency_code' => $selected_currency,
      ],
      'field_in_memory' => $form_state
        ->getValue('in_memory'),
      'field_in_memory_name' => $form_state
        ->getValue('in_memory_name'),
      'field_in_memory_memorial' => $form_state
        ->getValue('in_memory_memorial'),
    ]);
    $store = $this->currentStore
      ->getStore();

    // Always use the 'default' order type.
    $cart = $this->cartProvider
      ->getCart('default', $store);
    if (!$cart) {
      $cart = $this->cartProvider
        ->createCart('default', $store);
    }
    $this->cartManager
      ->addOrderItem($cart, $order_item, FALSE);

    // Go to checkout.
    $form_state
      ->setRedirect('commerce_checkout.form', [
      'commerce_order' => $cart
        ->id(),
    ]);
  }
  else {
    $options = [
      'webform' => 'monthly_donation',
      'donate_amount' => $amount,
      'in_memory' => $form_state
        ->getValue('in_memory'),
      'in_memory_name' => $form_state
        ->getValue('in_memory_name'),
      'in_memory_memorial' => $form_state
        ->getValue('in_memory_memorial'),
    ];
    $form_state
      ->setRedirect('entity.webform.canonical', $options);
  }
}