You are here

public function XquantityEditQuantity::viewsFormValidate in Commerce Extended Quantity 8

Validate the views form input.

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 EditQuantity::viewsFormValidate

See also

https://www.drupal.org/project/commerce/issues/2903504#comment-12228721

https://www.drupal.org/project/commerce/issues/2903504#comment-12378700

File

src/Plugin/views/field/XquantityEditQuantity.php, line 70

Class

XquantityEditQuantity
Overrides a form element for editing the order item quantity.

Namespace

Drupal\commerce_xquantity\Plugin\views\field

Code

public function viewsFormValidate(array &$form, FormStateInterface $form_state) {
  $quantities = $form_state
    ->getValue($this->options['id'], []);
  if ($availability = $this->moduleHandler
    ->moduleExists('xquantity_stock')) {
    $availability = \Drupal::service('xquantity_stock.availability_checker');
  }
  foreach ($this->view->result as $row_index => $row) {

    /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
    $order_item = $this
      ->getEntity($row);

    /** @var \Drupal\commerce\PurchasableEntityInterface $purchased_entity */
    $purchased_entity = $order_item
      ->getPurchasedEntity();
    $qty = $order_item
      ->getQuantity();
    $settings = $order_item
      ->getQuantityWidgetSettings();
    $scale = Numeric::getDecimalDigits($settings['step']);
    if (!empty($quantities[$row_index]) && bccomp($qty, $quantities[$row_index], $scale)) {
      $quantity = $quantities[$row_index];
      $old = bccomp($qty, $quantity, $scale) === 1;
      if ($available = $purchased_entity && $availability) {
        if ($availability
          ->applies($order_item)) {
          $context = new Context(\Drupal::currentUser(), $order_item
            ->getOrder()
            ->getStore(), time(), [
            'xquantity' => 'cart',
            'old' => $old ? $qty : 0,
          ]);
          $qty = $old ? $quantity : bcsub($quantity, $qty, $scale);
          $available = !$availability
            ->check($order_item, $context, $qty)
            ->isUnavailable();
          if (!$available && $order_item
            ->rotateStock($purchased_entity, $qty, $context)) {
            $available = !$availability
              ->check($order_item, $context, $qty)
              ->isUnavailable();
          }
        }
      }
      if (!$available) {
        $args = [
          '%quantity' => $quantity,
          '%label' => $purchased_entity ? $purchased_entity
            ->label() : $this
            ->t('???'),
          ':href' => $purchased_entity ? $purchased_entity
            ->toUrl()
            ->toString() : '/',
        ];
        $msg = $this
          ->t('Unfortunately, the quantity %quantity of the %label is not available right at the moment.', $args);
        \Drupal::logger('xquantity_stock')
          ->warning($this
          ->t('Possibly the <a href=":href">%label</a> with the quantity %quantity is out of stock.', $args));
        $purchased_entity && $this->moduleHandler
          ->alter("xquantity_add_to_cart_not_available_msg", $msg, $quantity, $purchased_entity);
        $form_state
          ->setError($form[$this->options['id']][$row_index], $msg);
      }
    }
  }
}