You are here

function commerce_avatax_form_alter in Drupal Commerce Connector for AvaTax 7.5

Same name and namespace in other branches
  1. 7.4 commerce_avatax.module \commerce_avatax_form_alter()

Implements hook_form_alter().

Validate addresses during checkout if necessary.

File

./commerce_avatax.module, line 769
AvaTax service integration from Avalara, Inc.

Code

function commerce_avatax_form_alter(&$form, &$form_state, $form_id) {

  // If we're dealing with a commerce checkout form.
  if (strpos($form_id, 'commerce_checkout_form_') === 0) {
    $address_validation_enabled = variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'validate_address', TRUE);

    // Only alter the checkout form when the Address validation is enabled.
    if (!$address_validation_enabled) {
      return;
    }
    $checkout_page_id = substr($form_id, 23);
    $customer_profile_pane_found = FALSE;

    // Find all panes for our current checkout page.
    foreach (commerce_checkout_panes(array(
      'enabled' => TRUE,
      'page' => $checkout_page_id,
    )) as $pane_id => $checkout_pane) {

      // If this pane is a customer profile based pane build a list of previous
      // profiles from which to pick that are of the same bundle.
      if (substr($pane_id, 0, 16) == 'customer_profile' && isset($form[$pane_id])) {
        $customer_profile_pane_found = TRUE;

        // HACK: We need to add a custom element validate handler if the profile
        // copy checkbox is present.
        // The profile copy element validate code will check if the triggering
        // element is the "continue" button.
        // However, in case the address validation button is present, it would
        // be the triggering element making the profile copy validation to fail.
        if (isset($form[$pane_id]['commerce_customer_profile_copy'])) {
          array_unshift($form[$pane_id]['commerce_customer_profile_copy']['#element_validate'], 'commerce_avatax_profile_copy_validate');
          break;
        }
      }
    }

    // If there are customer profiles pane on that page, we need to perform
    // Address validation.
    if ($customer_profile_pane_found) {
      $form['#attached']['library'][] = array(
        'system',
        'ui.dialog',
      );
      $module_path = drupal_get_path('module', 'commerce_avatax');
      $form['#attached']['js'][] = $module_path . '/js/commerce_avatax.js';
      $form['#attached']['css'][] = $module_path . '/css/commerce_avatax.checkout.css';
      $form['address_validation_result'] = array(
        '#type' => 'container',
        '#id' => 'commerce-avatax-address-validation-wrapper',
      );

      // Since we're preventing the default form submit, we need our custom
      // button to call the checkout continue validate functions.
      $validate_handlers = $form['buttons']['continue']['#validate'];
      $validate_handlers[] = 'commerce_avatax_checkout_validate';
      array_unshift($form['buttons']['continue']['#submit'], 'commerce_avatax_address_suggestion_apply');

      // This submit button will be clicked when the main form is submitted.
      $form['buttons']['validate-address'] = array(
        '#value' => t('Validate address'),
        '#type' => 'submit',
        '#attributes' => array(
          'class' => array(
            'element-invisible',
          ),
        ),
        '#id' => 'commerce-avatax-address-validate-btn',
        '#validate' => $validate_handlers,
        '#ajax' => array(
          'callback' => 'commerce_avatax_validate_shipping_address_ajax_callback',
          'progress' => array(
            'type' => 'none',
          ),
        ),
      );

      // Store the address suggestion delta in order to use it.
      $form['use_suggested_address'] = array(
        '#type' => 'hidden',
      );
    }
  }
}