You are here

function addressfield_field_widget_form in Address Field 7

Implements hook_field_widget_form()

File

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

Code

function addressfield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $settings = $instance['widget']['settings'];
  $address = array();

  // If the form has been rebuilt via AJAX, use the form state values.
  // $form_state['values'] is empty because of #limit_validation_errors, so
  // $form_state['input'] needs to be used instead.
  $parents = array_merge($element['#field_parents'], array(
    $element['#field_name'],
    $langcode,
    $delta,
  ));
  if (!empty($form_state['input'])) {
    $input_address = drupal_array_get_nested_value($form_state['input'], $parents);
  }
  if (!empty($input_address)) {
    $address = $input_address;
  }
  elseif (!empty($items[$delta]['country'])) {

    // Else use the saved value for the field.
    $address = $items[$delta];
  }

  // Determine the list of available countries, and if the currently selected
  // country is not in it, unset it so it can be reset to the default country.
  $countries = _addressfield_country_options_list($field, $instance);
  if (!empty($address['country']) && !isset($countries[$address['country']])) {
    unset($address['country']);
  }

  // Merge in default values.
  $address += addressfield_default_values($field, $instance, $address);

  // Add the form elements for the standard widget, which includes a country
  // select list at the top that reloads the available address elements when the
  // country is changed.
  if ($instance['widget']['type'] == 'addressfield_standard') {

    // Wrap everything in a fieldset. This is not the best looking element,
    // but it's the only wrapper available in Drupal we can properly use
    // in that context, and it is overridable if necessary.
    $element['#type'] = 'fieldset';
    if (!empty($instance['description'])) {

      // Checkout panes convert the fieldset into a container, causing
      // #description to not be rendered. Hence, a real element is added and
      // the old #description is removed.
      $element['#description'] = '';
      $element['element_description'] = array(
        '#markup' => $instance['description'],
        '#prefix' => '<div class="fieldset-description">',
        '#suffix' => '</div>',
        '#weight' => -999,
      );
    }

    // Generate the address form.
    $context = array(
      'mode' => 'form',
      'field' => $field,
      'instance' => $instance,
      'langcode' => $langcode,
      'delta' => $delta,
    );
    $element += addressfield_generate($address, $settings['format_handlers'], $context);

    // Remove any already saved default value.
    // See addressfield_form_field_ui_field_edit_form_alter() for the reasoning.
    if ($form_state['build_info']['form_id'] == 'field_ui_field_edit_form') {
      $element['#address'] = array(
        'country' => '',
      );
    }
  }
  return $element;
}