You are here

function commerce_addressbook_form_alter in Commerce Addressbook 7.2

Same name and namespace in other branches
  1. 7.3 commerce_addressbook.module \commerce_addressbook_form_alter()

Implements hook_form_alter().

File

./commerce_addressbook.module, line 300
Defines addressbook functionality for customer profiles, allowing them to be reused and managed on a per-user basis.

Code

function commerce_addressbook_form_alter(&$form, &$form_state, $form_id) {
  global $user;

  // If we're dealing with a commerce checkout form.
  if (strpos($form_id, 'commerce_checkout_form_') === 0 && $user->uid > 0) {
    $checkout_page_id = substr($form_id, 23);

    // 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]) && variable_get('commerce_' . $pane_id . '_addressbook', FALSE)) {
        $field = field_info_field(variable_get('commerce_' . $pane_id . '_field', ''));

        // Load the customer profiles via an EntityFieldQuery(), tag it in order
        // to allow query alterations.
        $query = new EntityFieldQuery();
        $query
          ->entityCondition('entity_type', 'commerce_customer_profile')
          ->entityCondition('bundle', $field['settings']['profile_type'])
          ->propertyCondition('uid', $user->uid)
          ->propertyCondition('status', TRUE)
          ->addTag('commerce_addressbook');
        $results = $query
          ->execute();
        if (isset($results['commerce_customer_profile'])) {
          $profiles = commerce_customer_profile_load_multiple(array_keys($results['commerce_customer_profile']));

          // Prepare the options.
          $options = array();
          foreach ($profiles as $id => $profile) {
            $field_values = field_get_items('commerce_customer_profile', $profile, 'commerce_customer_address');
            $options[$id] = $field_values[0]['thoroughfare'];
          }
          drupal_alter('commerce_addressbook_labels', $options, $profiles);

          // Prepare the default value.
          $reference_field_name = variable_get('commerce_' . $pane_id . '_field', '');
          $order_wrapper = entity_metadata_wrapper('commerce_order', $form_state['order']);
          $profile_reference = $order_wrapper->{$reference_field_name}
            ->value();
          $default_value = 'none';
          if (!empty($form_state['values'][$pane_id]['addressbook'])) {
            $default_value = $form_state['values'][$pane_id]['addressbook'];
          }
          elseif (!empty($profile_reference->profile_id)) {
            $default_value = $profile_reference->profile_id;
          }

          // Check if the default profile is in the enabled profiles array.
          if (!isset($profiles[$default_value])) {
            $default_value = 'none';
          }
          if ($default_value != 'none') {

            // Make sure our profile type still exists..
            if (!empty($form[$pane_id]['commerce_customer_profile_copy'])) {
              if (($source_profile_type_name = variable_get('commerce_' . $pane_id . '_profile_copy_source', NULL)) && ($source_profile_type = commerce_customer_profile_type_load($source_profile_type_name))) {

                // Disable the profile copy checkbox.
                $form[$pane_id]['commerce_customer_profile_copy']['#default_value'] = FALSE;
                $form[$pane_id]['commerce_customer_profile_copy']['#access'] = FALSE;

                // Loop over source profile fields and enable previously disabled
                // fields.
                foreach (field_info_instances('commerce_customer_profile', $source_profile_type['type']) as $field_name => $field) {
                  if (!empty($form[$pane_id][$field_name])) {
                    $langcode = $form[$pane_id][$field_name]['#language'];
                    $form[$pane_id][$field_name][$langcode]['#access'] = TRUE;
                    foreach (element_children($form[$pane_id][$field_name][$langcode]) as $key) {
                      $form[$pane_id][$field_name][$langcode][$key]['#access'] = TRUE;

                      // Disable the editing of the profile if the user doesn't
                      // have the right to edit customer profiles.
                      if (!commerce_customer_profile_access('update', $profiles[$default_value])) {
                        $form[$pane_id][$field_name][$langcode][$key]['#disabled'] = TRUE;
                      }
                    }
                  }
                }
              }
            }
          }
          $form[$pane_id]['#prefix'] = '<div id="' . strtr($pane_id, '_', '-') . '-ajax-wrapper">';
          $form[$pane_id]['#suffix'] = '</div>';
          $form[$pane_id]['addressbook_entries'] = array(
            '#type' => 'value',
            '#value' => $profiles,
          );
          $form[$pane_id]['addressbook'] = array(
            '#addressbook' => TRUE,
            '#type' => 'select',
            '#title' => t('Addresses on File'),
            '#description' => t('You may select a pre-existing address on file.'),
            '#options' => $options,
            '#empty_option' => t('-- Choose --'),
            '#empty_value' => 'none',
            '#ajax' => array(
              'callback' => 'commerce_addressbook_checkout_form_callback',
              'wrapper' => strtr($pane_id, '_', '-') . '-ajax-wrapper',
            ),
            '#element_validate' => array(
              'commerce_addressbook_saved_addresses_validate',
            ),
            '#weight' => -100,
            '#default_value' => $default_value,
          );
        }
      }
    }
  }
  if ($form_id == 'commerce_checkout_pane_settings_form' && substr($form['checkout_pane']['#value']['pane_id'], 0, 16) == 'customer_profile') {
    $form['settings']['commerce_' . $form['checkout_pane']['#value']['pane_id'] . '_addressbook'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable the Address Book'),
      '#description' => t('This will allow authenticated users to reuse previously entered addresses.'),
      '#default_value' => variable_get('commerce_' . $form['checkout_pane']['#value']['pane_id'] . '_addressbook', FALSE),
    );
  }
  if ($form_id == 'commerce_addressbook_customer_profile_form') {

    // Make sure the submit handlers run.
    rsort($form['actions']['submit']['#submit']);
    foreach ($form['actions']['submit']['#submit'] as $callback) {
      array_unshift($form['#submit'], $callback);
    }
    unset($form['actions']['submit']['#submit']);

    // Hide the "Additional settings" vertical tabs.
    $form['additional_settings']['#access'] = FALSE;
  }
}