You are here

public function StockEditForm::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_stock/src/Form/StockEditForm.php, line 81

Class

StockEditForm
Defines the stock edit form.

Namespace

Drupal\uc_stock\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  foreach (Element::children($form_state
    ->getValue('stock')) as $sku) {
    $stock = $form_state
      ->getValue([
      'stock',
      $sku,
    ]);
    \Drupal::database()
      ->merge('uc_product_stock')
      ->key([
      'sku' => $sku,
    ])
      ->updateFields([
      'active' => $stock['active'],
      'stock' => $stock['stock'],
      'threshold' => $stock['threshold'],
    ])
      ->insertFields([
      'sku' => $sku,
      'active' => $stock['active'],
      'stock' => $stock['stock'],
      'threshold' => $stock['threshold'],
      'nid' => $form_state
        ->getValue('nid'),
    ])
      ->execute();
  }
  $this
    ->messenger()
    ->addMessage($this
    ->t('Stock settings saved.'));
}