You are here

public function XquantityEditQuantity::viewsForm in Commerce Extended Quantity 8

Form constructor for the views form.

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::viewsForm

File

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

Class

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

Namespace

Drupal\commerce_xquantity\Plugin\views\field

Code

public function viewsForm(array &$form, FormStateInterface $form_state) {
  parent::viewsForm($form, $form_state);
  $form_object = $form_state
    ->getFormObject();
  foreach ($this->view->result as $row_index => $row) {

    /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
    $order_item = $this
      ->getEntity($row);
    $default_value = $order_item
      ->getQuantity() + 0;
    $settings = $order_item
      ->setQuantityPrices($form_object, $this, $form_state);
    if (is_string($settings['prefix']) && !empty($settings['prefix'])) {
      $prefixes = explode('|', $settings['prefix']);
      $prefix = count($prefixes) > 1 ? $this
        ->formatPlural($default_value, $prefixes[0], $prefixes[1]) : $prefixes[0];
      $settings['prefix'] = FieldFilteredMarkup::create($prefix);
    }
    if (is_string($settings['suffix']) && !empty($settings['suffix'])) {
      $suffixes = explode('|', $settings['suffix']);
      $suffix = count($suffixes) > 1 ? $this
        ->formatPlural($default_value, $suffixes[0], $suffixes[1]) : $suffixes[0];
      $settings['suffix'] = FieldFilteredMarkup::create($suffix);
    }
    $form[$this->options['id']][$row_index] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Quantity'),
      '#title_display' => 'invisible',
      '#default_value' => $default_value,
      '#size' => 4,
      '#min' => isset($settings['min']) && is_numeric($settings['min']) ? $settings['min'] : '1',
      '#max' => isset($settings['max']) && is_numeric($settings['max']) ? $settings['max'] : '9999',
      '#step' => isset($settings['step']) && is_numeric($settings['step']) ? $settings['step'] : '1',
      '#placeholder' => empty($settings['placeholder']) ? '' : $settings['placeholder'],
      '#field_prefix' => $settings['prefix'],
      '#field_suffix' => $settings['suffix'],
      // Might be disabled on the quantity field form display widget.
      '#disabled' => isset($settings['disable_on_cart']) ? $settings['disable_on_cart'] : FALSE,
    ];
  }
}