You are here

function geofield_element_validate in Geofield 7

Geofield Element Validate

1 string reference to 'geofield_element_validate'
geofield_get_base_element in ./geofield.widgets.inc

File

./geofield.widgets.inc, line 331
Provides field widget hooks for geofield module.

Code

function geofield_element_validate($element, &$form_state) {
  $master_column = $element['master_column']['#value'];
  $values = array(
    'wkt' => $element['wkt']['#value'],
    'lat' => $element['lat']['#value'] ? geofield_latlon_DMStoDEC($element['lat']['#value']) : '',
    'lon' => $element['lon']['#value'] ? geofield_latlon_DMStoDEC($element['lon']['#value']) : '',
    'top' => $element['top']['#value'],
    'bottom' => $element['bottom']['#value'],
    'right' => $element['right']['#value'],
    'left' => $element['left']['#value'],
  );

  // Because we have an odd flow to filter potential GeoJSON, we need a flag to determine whether or not to process.
  // @TODO: This could be expanded to cover WKT as well.
  $geo_process = "PROCESS";
  if ($element['input_format']['#value'] == 'geojson' && $values['wkt']) {

    // For geojson input, we've hijacked the wkt value.
    geophp_load();
    try {
      $geometry = geoPHP::load($values['wkt'], 'json');
      $values['wkt'] = $geometry
        ->out('wkt');
    } catch (Exception $e) {
      form_set_error($element['wkt']['#name'], 'Please enter valid GeoJSON');
      $geo_process = '';
    }
  }
  if ($geo_process == "PROCESS") {
    geofield_compute_values($values, $master_column);

    // Set form values from the $values array
    if (isset($values['wkt'])) {
      form_set_value($element['wkt'], $values['wkt'], $form_state);
    }
    if (isset($values['geo_type'])) {
      form_set_value($element['geo_type'], $values['geo_type'], $form_state);
    }
    if (isset($values['lat'])) {
      form_set_value($element['lat'], $values['lat'], $form_state);
    }
    if (isset($values['lon'])) {
      form_set_value($element['lon'], $values['lon'], $form_state);
    }
    if (isset($values['top'])) {
      form_set_value($element['top'], $values['top'], $form_state);
    }
    if (isset($values['bottom'])) {
      form_set_value($element['bottom'], $values['bottom'], $form_state);
    }
    if (isset($values['right'])) {
      form_set_value($element['right'], $values['right'], $form_state);
    }
    if (isset($values['left'])) {
      form_set_value($element['left'], $values['left'], $form_state);
    }
  }
}