You are here

public function RemoveButton::viewsForm in Commerce Ajax Add to Cart 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 RemoveButton::viewsForm

File

modules/dc_ajax_add_cart_views/src/Plugin/views/field/RemoveButton.php, line 18

Class

RemoveButton
Defines a form element for removing the order item via ajax.

Namespace

Drupal\dc_ajax_add_cart_views\Plugin\views\field

Code

public function viewsForm(array &$form, FormStateInterface $form_state) {
  parent::viewsForm($form, $form_state);
  $wrapper_id = $this->view->storage
    ->id() . '-cart-ajax-wrapper';
  $form['#attached']['library'][] = 'core/jquery.form';
  $form['#attached']['library'][] = 'core/drupal.ajax';
  $form['#prefix'] = "<div id='{$wrapper_id}'>";
  $form['#suffix'] = '</div>';
  foreach ($this->view->result as $row_index => $row) {
    $form[$this->options['id']][$row_index] = [
      '#type' => 'submit',
      '#value' => t('Remove'),
      '#name' => 'delete-order-item-' . $row_index,
      '#remove_order_item' => TRUE,
      '#row_index' => $row_index,
      '#attributes' => [
        'class' => [
          'delete-order-item',
          'use-ajax-submit',
        ],
      ],
      '#ajax' => [
        'wrapper' => $wrapper_id,
      ],
    ];
  }
}