You are here

function geocoder_field_widget_form in Geocoder 7

Implements hook_field_widget_form().

File

./geocoder.widget.inc, line 198
geocoder.widget.inc

Code

function geocoder_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
  $widget = $instance['widget'];
  $settings = $widget['settings'];
  if ($widget['type'] === 'geocoder' && !empty($settings['latlng_override'])) {
    $instance['widget']['type'] = 'geofield_latlon';
    $element = geofield_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $base);

    // At present, the only way we know if the user has overridden the lat/long
    // values is to perform a geocode and compare with the existing values.
    $geocoded_value = geocoder_widget_get_field_value($instance['entity_type'], $instance, $form['#entity']);
    $override_default = FALSE;
    if ($geocoded_value && (round($element['geom']['#default_value']['lat'], 3) !== round($geocoded_value[$element['#language']][$delta]['lat'], 3) || round($element['geom']['#default_value']['lon'], 3) !== round($geocoded_value[$element['#language']][$delta]['lon'], 3))) {
      $override_default = TRUE;
    }
    $element['geocoder_overridden'] = array(
      '#type' => 'checkbox',
      '#title' => t('Override lat/long values'),
      '#weight' => -10,
      '#default_value' => $override_default,
      '#description' => t('If address geocoding is inaccurate, check this box to override the lat/long values manually.'),
    );

    // Add element process to states to lat/long.
    $element['#process'][] = 'geocoder_process_override';
    $element['#element_validate'][] = 'geocoder_element_validate';
    return array(
      $delta => $element,
    );
  }
}