You are here

public function MoveToCart::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/MoveToCart.php, line 167

Class

MoveToCart
Defines a form element for moving or copying the wishlist item to the cart.

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($this->view->result[$row_index]);
    if ($this
      ->isValid($wishlist_item)) {
      $form[$this->options['id']][$row_index] = [
        '#type' => 'submit',
        '#value' => $this->options['keep_item'] ? $this
          ->t('Copy to cart') : $this
          ->t('Move to cart'),
        '#name' => 'move-wishlist-item-' . $row_index,
        '#move_wishlist_item' => TRUE,
        '#row_index' => $row_index,
        '#attributes' => [
          'class' => [
            'move-wishlist-item',
          ],
        ],
      ];
    }
    else {
      $form[$this->options['id']][$row_index] = [];
    }
  }
}