You are here

function geocoder_field_attach_presave in Geocoder 7

Implements hook_field_attach_presave().

Geocoding for the geocoder widget is done here to ensure that only validated and fully processed fields values are accessed.

File

./geocoder.widget.inc, line 294
geocoder.widget.inc

Code

function geocoder_field_attach_presave($entity_type, $entity) {
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);

  // Loop over any geofield using our geocode widget.
  foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
    if ($instance['widget']['type'] === 'geocoder') {
      if ($geocoded_value = geocoder_widget_get_field_value($entity_type, $instance, $entity)) {
        if (!empty($entity->{$field_name})) {
          foreach ($entity->{$field_name} as $langcode => $items) {
            if (empty($items)) {
              $entity->{$field_name} = $geocoded_value;
              break;
            }
            foreach ($items as $delta => $item) {

              // Check the entity value.
              if (!(isset($entity->geocoder_overridden) && isset($entity->geocoder_overridden[$field_name]) && isset($entity->geocoder_overridden[$field_name][$langcode]) && !empty($entity->geocoder_overridden[$field_name][$langcode][$delta]))) {
                $entity->{$field_name}[$langcode][$delta] = $geocoded_value[$langcode][$delta];
              }
            }
          }
        }
        else {
          $entity->{$field_name} = $geocoded_value;
        }
      }
      else {
        $entity->{$field_name} = $geocoded_value;
      }
    }
  }
}