You are here

function uc_addresses_process_address_field in Ubercart Addresses 6.2

Same name and namespace in other branches
  1. 7 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_elements()

uc_addresses_validate_address_field()

1 string reference to 'uc_addresses_process_address_field'
uc_addresses_elements in ./uc_addresses.module
Implementation of hook_elements().

File

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

Code

function uc_addresses_process_address_field(&$element, $form_state) {
  if (!$element['#uc_addresses_address'] instanceof UcAddressesSchemaAddress) {
    $element['#uc_addresses_address'] = UcAddressesAddress::newAddress();
  }
  $element['#tree'] = TRUE;
  $prefix = $element['#key_prefix'] ? $element['#key_prefix'] . '_' : '';
  if (isset($form_state['uc_addresses_address'])) {

    // Use submitted Ajax values.
    $values = $form_state['uc_addresses_address'];
  }
  elseif (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);
    }
  }

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

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

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