You are here

function hook_addressfield_default_values_alter in Address Field 7

Allows modules to alter the default values for an address field.

Parameters

$default_values: The array of default values. The country is populated from the 'default_country' widget setting.

$context: An array with the following keys:

  • field: The field array.
  • instance: The instance array.
  • address: The current address values. Allows for per-country defaults.
1 invocation of hook_addressfield_default_values_alter()
addressfield_default_values in ./addressfield.module
Returns an array of default values for the addressfield form elements.

File

./addressfield.api.php, line 44
API documentation for Addressfield.

Code

function hook_addressfield_default_values_alter(&$default_values, $context) {

  // If no other default country was provided, set it to France.
  // Note: you might want to check $context['instance']['required'] and
  // skip setting the default country if the field is optional.
  if (empty($default_values['country'])) {
    $default_values['country'] = 'FR';
  }

  // Determine the country for which other defaults should be provided.
  $selected_country = $default_values['country'];
  if (isset($context['address']['country'])) {
    $selected_country = $context['address']['country'];
  }

  // Add defaults for the US.
  if ($selected_country == 'US') {
    $default_values['locality'] = 'New York';
    $default_values['administrative_area'] = 'NY';
  }
}