You are here

function commerce_admin_order_advanced_form_commerce_order_ui_order_form_alter in Commerce Admin Order Advanced 7

Implements hook_form_FORM_ID_alter().

Alter the admin checkout form to add copy address functionality and a custom line item search form.

File

./commerce_admin_order_advanced.module, line 68
Provides advanced admin order interface.

Code

function commerce_admin_order_advanced_form_commerce_order_ui_order_form_alter(&$form, &$form_state) {
  $profile_types = commerce_customer_profile_types();

  // Loop through each of the commerce customer profile types to add copy
  // address functionality.
  foreach ($profile_types as $profile_type) {
    $copy_dest = $profile_type['type'];

    // Determine if this field should have the copy profile option.
    $copy_profile = variable_get('commerce_customer_profile_' . $copy_dest . '_profile_copy', FALSE);
    if (!empty($copy_profile)) {
      $language = $form['commerce_customer_' . $copy_dest]['#language'];

      // Get the field that profile values should copy from.
      $copy_source = variable_get('commerce_customer_profile_' . $copy_dest . '_profile_copy_source');

      // Add the ajax wrappers around the customer address form fields.
      $form['commerce_customer_' . $copy_dest]['#prefix'] = '<div id="commerce-admin-order-advanced-commerce-customer-' . $copy_dest . '-ajax-wrapper">';
      $form['commerce_customer_' . $copy_dest]['commerce_customer_address']['#suffix'] = '</div>';

      // Add a visible state to hide the profile if copy existing is clicked.
      $form['commerce_customer_' . $copy_dest][$language]['profiles'][0]['commerce_customer_address']['#states'] = array(
        'visible' => array(
          ':input[name="profile_copy_' . $copy_dest . '"]' => array(
            'checked' => FALSE,
          ),
        ),
      );

      // Create the checkbox to trigger the profile copy.
      $form['commerce_customer_' . $copy_dest][$language]['profiles'][0]['profile_copy_' . $copy_dest] = array(
        '#type' => 'checkbox',
        '#tree' => FALSE,
        '#title' => t('Copy the customers @copy_source to this @copy_dest profile.', array(
          '@copy_source' => $copy_source,
          '@copy_dest' => $copy_dest,
        )),
        '#options' => array(
          0 => FALSE,
          TRUE => $copy_dest,
        ),
        '#validate' => array(
          'commerce_admin_order_advanced_profile_copy_validate',
        ),
        '#weight' => -10,
        '#ajax' => array(
          'callback' => 'commerce_admin_order_advanced_profile_refresh',
          'wrapper' => 'commerce-admin-order-advanced-commerce-customer-' . $copy_dest . '-ajax-wrapper',
        ),
      );
    }
  }
}