You are here

function commerce_cart_blocks_form_commerce_order_item_add_to_cart_form_alter in Commerce Cart Blocks 8

Implements hook_form_FORM_ID_alter().

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

File

./commerce_cart_blocks.module, line 220
Contains commerce_cart_blocks.module.

Code

function commerce_cart_blocks_form_commerce_order_item_add_to_cart_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $formObject = $form_state
    ->getFormObject();
  if (is_object($formObject) && method_exists($formObject, 'getEntity')) {
    $entity = $formObject
      ->getEntity();
    if ($entity instanceof EntityInterface) {
      $settings = commerce_cart_blocks_ajax_settings($entity);
      if ($settings['enabled']) {
        $form['commerce_cart_blocks_ajax'] = [
          '#type' => 'value',
          '#value' => $settings,
        ];
        $form['actions']['submit']['#ajax'] = [
          'callback' => 'commerce_cart_blocks_ajax_callback',
          'wrapper' => 'commerce-product-add-to-cart-form',
          'event' => 'click',
          'effect' => 'fade',
          'progress' => [
            'type' => 'throbber',
            'message' => t('Adding to Cart'),
          ],
        ];
        $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
        $form['#attached']['library'][] = 'commerce_cart_blocks/commerce_cart_blocks_add_to_cart_form';
        $form['#attached']['drupalSettings']['commerce_cart_blocks']['ajax'] = $settings;
      }
    }
  }
}