You are here

function uc_addresses_form_process_select_address in Ubercart Addresses 7

Element process hook for an selectable address field.

Parameters

array $element: The form element to process.

array $form_state: The complete form state.

Return value

array The processed form element.

See also

uc_addresses_element_info()

1 string reference to 'uc_addresses_form_process_select_address'
uc_addresses_element_info in ./uc_addresses.module
Implements hook_element_info().

File

./uc_addresses.module, line 987
Adds user profile address support to Ubercart.

Code

function uc_addresses_form_process_select_address(&$element, &$form_state) {
  if (count($element['#uc_addresses_select_addresses']) < 1) {

    // Return NULL if there are no addresses.
    return NULL;
  }

  // Initialize default value, we don't know yet which address in the list is
  // equal to the given default value.
  $address_book_default_value = NULL;
  $options = array(
    '' => t('Select one...'),
  );
  $addresses = array_values($element['#uc_addresses_select_addresses']);
  foreach ($addresses as $key => $address) {
    $address_name = $address
      ->getName();
    if ($address_name) {
      $options[$key] = $address_name;
    }
    else {
      $options[$key] = preg_replace('/<.*?>/', ', ', trim(uc_addresses_format_address($address)));
    }

    // Check if this address is equal to given default value.
    if ($element['#default_value'] instanceof UcAddressesSchemaAddress && !$address_book_default_value) {
      if ($address
        ->compareAddress($element['#default_value'])) {

        // Default value found! Save it for later.
        $address_book_default_value = $key;
      }
    }
  }
  $element['#options'] = $options;

  // Add default value if found.
  if ($address_book_default_value) {
    $element['#default_value'] = $address_book_default_value;
  }
  else {
    unset($element['#default_value']);
  }

  // Replace address in form.
  if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#id'] === $element['#id']) {

    // Hey, we need to update an address.
    if (empty($element['#uc_addresses_address_input']) || empty($element['#uc_addresses_address_element'])) {

      // Hm, it's not specified where the address is in the form.
      // Abort.
    }
    elseif (isset($addresses[$element['#value']])) {
      $address = $addresses[$element['#value']];

      // Find address field to replace.
      $address_field =& $form_state['complete form'];
      foreach ($element['#uc_addresses_address_element'] as $key) {
        if (isset($address_field[$key])) {
          $address_field =& $address_field[$key];
        }
      }
      $address_values = $address
        ->getRawFieldData();

      // If there is a key prefix set, then prefix the address values.
      if (!empty($address_field['#key_prefix'])) {
        $prefix = $address_field['#key_prefix'] . '_';
        foreach ($address_values as $fieldname => $value) {
          $address_values[$prefix . $fieldname] = $address_values[$fieldname];
          unset($address_values[$fieldname]);
        }
      }
      $address_field['#value'] = $address_values;

      // Find input array to replace.
      $input =& $form_state['input'];
      foreach ($element['#uc_addresses_address_input'] as $key) {
        if (isset($input[$key])) {
          $input =& $input[$key];
        }
      }
      $input = $address_field['#value'];
    }
  }
  return $element;
}