You are here

public function AjaxCartHelper::ajaxAddToCartAjaxForm in Ajax Add to Cart 8

Ajax add to cart Form.

Parameters

string $form_id: Form id.

array $form: Form array.

Return value

string Return Form array.

File

src/Helper/AjaxCartHelper.php, line 135

Class

AjaxCartHelper
Class AjaxCartHelper.

Namespace

Drupal\ajax_add_to_cart\Helper

Code

public function ajaxAddToCartAjaxForm($form_id, &$form) {
  $messages = [
    $form_id => t('Adding to cart ...'),
  ];
  $form['status_messages_' . $form_id] = [
    '#type' => 'status_messages',
    '#weight' => -10,
  ];
  $form['form_id'] = [
    '#type' => 'hidden',
    '#value' => $form_id,
  ];

  // // Add ajax callback to the form.
  $form['actions']['submit']['#attributes']['class'][] = 'use-ajax';
  $form['actions']['submit']['#ajax'] = [
    'callback' => 'ajax_add_to_cart_ajax_validate',
    'disable-refocus' => TRUE,
    'event' => 'click',
    'progress' => [
      'type' => 'throbber',
      'message' => $messages[$form_id],
    ],
  ];

  // Add ajax dialoge library to open the form in popup.
  // Adding own library to add extra functionality.
  $form['#attached']['library'][] = 'ajax_add_to_cart/ajax_add_to_cart.commands';
  $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
  $form['#attached']['drupalSettings']['ajax_add_to_cart']['ajax_add_to_cart']['time'] = $this->configFactory
    ->get('ajax_add_to_cart.ajaxconfig')
    ->get('time_ajax_modal');
  return $form;
}