Due to the order of the field saving workflow, geofield_field_presave() is executed before geocoder_field_attach_presave() thus skipping the saving callback of the GeoField backend.

As a workaround you can do this (you module must be after geocoder)


/**
 * Implements hook_field_attach_presave().
 *
 * This is to fix the fact that when a field is geocoded, it doesn't get again in the save callback of geofield
 */
function mymodule_field_attach_presave($entity_type, $entity) {
  // Loop over any geofield using the geocode widget
  $entity_info = entity_get_info($entity_type);
  $bundle_name = empty($entity_info['entity keys']['bundle']) ? $entity_type : $entity->{$entity_info['entity keys']['bundle']};
  foreach (field_info_instances($entity_type, $bundle_name) as $field_instance) {
    if ($field_instance['widget']['type'] === 'geocoder') {
      $field_name = $field_instance['field_name'];
      $field = field_info_field($field_name);
      $backend = ctools_get_plugins('geofield', 'geofield_backend', $field['settings']['backend']);
      $save_callback = $backend['save'];
      if (($field_value = geocoder_widget_get_field_value($entity_type, $field_instance, $entity)) !== FALSE) {
        $langcode  = field_language($entity_type, $entity, $field_name);
        $values = & $entity->{$field_name}[$langcode];
        foreach ($values as $delta => $item) {
          if (isset($item['geom']) && $item['geom']) {
            $values[$delta]['geom'] = $save_callback($item['geom']);
          }
        }
      }
    }
  }
}

But a patch testing for the type of the field (geofield) and executing the backend callback would be better

Comments

simon georges’s picture

Would you be willing to provide such a patch? ;-)

nwom’s picture

Status: Active » Needs work

This no longer works. Here is the resulting error message after saving an entity:

Warning: Invalid argument supplied for foreach() in mymodule_field_attach_presave() (line 20 of /var/aegir/platforms/panopoly-7.x-1.30/sites/{SITE}/modules/mymodule/mymodule.module).

pol’s picture

Geocoder Fix ?!

nwom’s picture

I added the above code as a module called "Geocoder Fix" even though it might not be the "fix" that I was looking for. I deleted all of my geocoded location fields from all entities using VBO, and now they no longer geocode on save, so I figured maybe the above code would solve the issue. I apologize if me labeling the custom module geocoder_fix was offensive somehow.

pol’s picture

As @simon said,

Could you provide a patch so we can test it and commit it eventually ?

Thanks.

nwom’s picture

Status: Needs work » Active

I would love to, but my PHP skills are lacking sadly. I went ahead and set it back to active, since I found out the problem that I was having and created a separate issue for it.

socialnicheguru’s picture

I am finding that if I have an addressfield and geofield in a field_collection, then it will not geocode on save of the entity.

pol’s picture

Status: Active » Closed (outdated)