You are here

public function EditQuantity::viewsForm in Commerce Wishlist 8.3

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.

File

src/Plugin/views/field/EditQuantity.php, line 41

Class

EditQuantity
Defines a form element for editing the wishlist item quantity.

Namespace

Drupal\commerce_wishlist\Plugin\views\field

Code

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

  // Make sure we do not accidentally cache this form.
  $form['#cache']['max-age'] = 0;

  // The view is empty, abort.
  if (empty($this->view->result)) {
    unset($form['actions']);
    return;
  }
  $form[$this->options['id']]['#tree'] = TRUE;
  foreach ($this->view->result as $row_index => $row) {

    /** @var \Drupal\commerce_wishlist\Entity\WishlistItemInterface $wishlist_item */
    $wishlist_item = $this
      ->getEntity($row);
    $quantity = $wishlist_item
      ->getQuantity();
    $form[$this->options['id']][$row_index] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Quantity'),
      '#title_display' => 'invisible',
      '#default_value' => round($quantity),
      '#size' => 4,
      '#min' => 1,
      '#max' => 9999,
      '#step' => 1,
    ];
  }

  // Replace the form submit button label.
  $form['actions']['submit']['#value'] = $this
    ->t('Update wishlist');
}