You are here

public function VariationXquantityStockAdjust::submitConfigurationForm in Commerce Extended Quantity 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Overrides PluginFormInterface::submitConfigurationForm

File

modules/xquantity_stock/src/Plugin/Action/VariationXquantityStockAdjust.php, line 104

Class

VariationXquantityStockAdjust
Adjust variation stock.

Namespace

Drupal\xquantity_stock\Plugin\Action

Code

public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getTriggeringElement()['#id'] != 'edit-cancel') {
    $values = $form_state
      ->getValues();
    $xquantity_stock = $form_state
      ->get('xquantity_stock');
    if (empty($xquantity_stock) || !is_numeric($value = $values['adjust_value'])) {
      \Drupal::messenger()
        ->AddError($this
        ->t('The inserted adjust value is not numeric.'));
      return;
    }
    $op = $values['adjust_op'] == 'add' ? 'bcadd' : 'bcsub';
    $scale = $form_state
      ->get('xquantity_stock_scale');
    $step = $form_state
      ->get('xquantity_stock_step');
    foreach ($form_state
      ->get('variations') as $variation) {
      $stock = $variation
        ->get($xquantity_stock)->value;
      if ($values['adjust_type'] == 'fixed_number') {
        $quantity = $op($stock, $value, $scale);
      }
      else {
        $quantity = $op($stock, bcmul($step, (int) $value, $scale), $scale);
      }
      $quantity = bccomp($quantity, '0', $scale) === 1 ? $quantity : '0';
      $variation
        ->set($xquantity_stock, $quantity)
        ->save();
    }
  }
}