You are here

public function CartForm::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_cart/src/Form/CartForm.php, line 235

Class

CartForm
Displays the contents of the customer's cart.

Namespace

Drupal\uc_cart\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // If a remove button was clicked, set the quantity for that item to 0.
  $triggering_element = $form_state
    ->getTriggeringElement();
  if (substr($triggering_element['#name'], 0, 7) == 'remove-') {
    $item = substr($triggering_element['#name'], 7);
    $form_state
      ->setValue([
      'items',
      $item,
      'qty',
    ], 0);
    $this
      ->messenger()
      ->addMessage($this
      ->t('<strong>@product</strong> removed from your shopping cart.', [
      '@product' => $form['data'][$item]['title']['#value'],
    ]));
  }

  // Update the items in the shopping cart based on the form values, but only
  // if a qty has changed.
  foreach ($form_state
    ->getValue('items') as $key => $item) {
    if (isset($form['items'][$key]['qty']['#default_value']) && $form['items'][$key]['qty']['#default_value'] != $item['qty']) {
      $this->moduleHandler
        ->invoke($item['module'], 'uc_update_cart_item', [
        $item['nid'],
        unserialize($item['data']),
        $item['qty'],
      ]);
    }
  }

  // Invalidate the cart order.
  $this->session
    ->set('uc_cart_order_rebuild', TRUE);
}