You are here

function _addressfield_process_format_form in Address Field 7

Related topics

1 call to _addressfield_process_format_form()
addressfield_process_format_form in ./addressfield.module
Generate a full-fledged form from a format snippet, as returned by addressfield_formats().

File

./addressfield.module, line 219
Defines a field for attaching country-specific addresses to entities.

Code

function _addressfield_process_format_form(&$format, $address) {
  foreach (element_children($format) as $key) {
    $child =& $format[$key];

    // Automatically convert any element in the format array to an appropriate
    // form element that matches one of the address component names.
    if (in_array($key, array(
      'name_line',
      'first_name',
      'last_name',
      'organisation_name',
      'country',
      'administrative_area',
      'sub_administrative_area',
      'locality',
      'dependent_locality',
      'postal_code',
      'thoroughfare',
      'premise',
      'sub_premise',
    ))) {

      // Set the form element type for the address component to whatever the
      // address format specified in its #widget_type property.
      if (isset($child['#widget_type'])) {
        $child['#type'] = $child['#widget_type'];
      }
      else {

        // If the element didn't specify a #widget_type and has options, turn it
        // into a select list and unset its #size value, which is typically used
        // to provide the width of a textfield.
        if (isset($child['#options'])) {
          $child['#type'] = 'select';
          unset($child['#size']);
        }
        else {

          // Otherwise go ahead and make it a textfield.
          $child['#type'] = 'textfield';
        }
      }
      if (isset($address[$key])) {
        $child['#default_value'] = $address[$key];
      }
    }

    // Recurse through the element's children if it has any.
    _addressfield_process_format_form($child, $address);
  }
}