You are here

public function MoveToWishlist::viewsFormSubmit in Commerce Wishlist 8.3

Submit handler 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/MoveToWishlist.php, line 167

Class

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

Namespace

Drupal\commerce_wishlist\Plugin\views\field

Code

public function viewsFormSubmit(array &$form, FormStateInterface $form_state) {
  $triggering_element = $form_state
    ->getTriggeringElement();
  if (!empty($triggering_element['#move_cart_item'])) {
    $row_index = $triggering_element['#row_index'];

    /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
    $order_item = $this
      ->getEntity($this->view->result[$row_index]);
    $purchased_entity = $order_item
      ->getPurchasedEntity();
    $quantity = $order_item
      ->getQuantity();
    $wishlist_type = 'default';
    $wishlist = $this->wishlistProvider
      ->getWishlist($wishlist_type);
    if (!$wishlist) {
      $wishlist = $this->wishlistProvider
        ->createWishlist($wishlist_type);
    }
    $this->wishlistManager
      ->addEntity($wishlist, $purchased_entity, $quantity, $this->options['combine']);
    if (!$this->options['keep_item']) {
      $this->cartManager
        ->removeOrderItem($order_item
        ->getOrder(), $order_item);
    }
  }
}