You are here

function uc_addresses_process_address_field in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 uc_addresses.module \uc_addresses_process_address_field()

Element process hook for address fields.

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()

uc_addresses_validate_address_field()

2 string references to 'uc_addresses_process_address_field'
uc_addresses_element_info in ./uc_addresses.module
Implements hook_element_info().
uc_addresses_form_uc_cart_checkout_form_alter in ./uc_addresses.module
Implements hook_form_FORM_ID_alter() for form uc_cart_checkout_form().

File

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

Code

function uc_addresses_process_address_field(&$element, &$form_state) {
  $element['#tree'] = TRUE;
  $prefix = $element['#key_prefix'] ? $element['#key_prefix'] . '_' : '';

  // Make sure the form element is associated with an address.
  if (!$element['#uc_addresses_address'] instanceof UcAddressesSchemaAddress) {

    // Find address ID in form_state first.
    $address = NULL;
    $input = $form_state['input'];
    $parents = $element['#parents'];
    foreach ($parents as $parent) {
      if (isset($input[$parent])) {
        $input = $input[$parent];
      }
      else {
        break;
      }
    }
    if (isset($input['aid'])) {
      $address = UcAddressesAddressBook::get(0)
        ->getAddressById($input['aid']);
    }
    if (!$address) {
      $address = UcAddressesAddress::newAddress();
    }
    $element['#uc_addresses_address'] = $address;
  }
  if (function_exists('uc_store_address_field_weights')) {
    $weights = uc_addresses_address_field_weights();
  }

  // Ensure we working with the right address (sometimes we caught two address objects where on of them was serialized).
  $element['#uc_addresses_address'] = $element['#uc_addresses_address']
    ->getAddressBook()
    ->getAddressById($element['#uc_addresses_address']
    ->getId());
  if (is_array($element['#value']) || is_object($element['#value'])) {

    // Use provided default value.
    $values = (array) $element['#value'];
  }
  else {
    $values = array();
  }

  // Delete prefixes from value array if available.
  if ($prefix) {
    foreach ($values as $fieldname => $fieldvalue) {
      if (strpos($fieldname, $prefix) === 0) {
        $fixed_fieldname = drupal_substr($fieldname, drupal_strlen($prefix));
        $values[$fixed_fieldname] = $fieldvalue;
        unset($values[$fieldname]);
      }
    }
  }
  $handler_instances = uc_addresses_get_address_field_handler_instances($element['#uc_addresses_address'], $element['#uc_addresses_context']);
  foreach ($handler_instances as $instance) {
    if ($instance
      ->isFieldEnabled() && $instance
      ->checkContext()) {
      $element += $instance
        ->getFormField($element, $values);
    }
  }

  // Add address ID to the form (if not set).
  if (!isset($element['aid'])) {
    $element['aid'] = array(
      '#type' => 'hidden',
      '#value' => $element['#uc_addresses_address']
        ->getId(),
    );
  }

  // Save address in form state.
  $form_state['uc_addresses'][$element['#uc_addresses_address']
    ->getId()] = $element['#uc_addresses_address'];

  // Allow other modules to alter the element.
  drupal_alter('uc_addresses_address_field', $element);

  // Set common values for all address fields.
  foreach (element_children($element) as $fieldname) {
    $element[$fieldname] += array(
      '#access' => $element['#hidden'] ? FALSE : TRUE,
      '#weight' => isset($weights[$fieldname]) ? $weights[$fieldname] : 0,
    );

    // Add weight setting also to fields only used for display.
    if (isset($element[$fieldname . '_item']) && isset($weights[$fieldname])) {
      $element[$fieldname . '_item'] += array(
        '#weight' => $weights[$fieldname],
      );
    }

    // Make all fields non-required if property "uc_addresses_required" is set to FALSE.
    if ($element['#uc_addresses_required'] === FALSE) {
      $element[$fieldname]['#required'] = FALSE;
    }
    elseif (is_array($element['#uc_addresses_required']) && isset($element['#uc_addresses_required'][$fieldname])) {
      $element[$fieldname]['#required'] = $element['#uc_addresses_required'][$fieldname] ? TRUE : FALSE;
    }

    // Copy JavaScript states from the parent element.
    if (isset($element['#states'])) {
      $element[$fieldname]['#states'] = $element['#states'];
    }
  }

  // Add prefixes if set.
  if ($prefix) {
    foreach (element_children($element) as $fieldname) {
      $element[$prefix . $fieldname] = $element[$fieldname];
      unset($element[$fieldname]);
    }
  }
  return $element;
}