Closed (outdated)
Project:
Geocoder
Version:
7.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
13 Mar 2014 at 16:52 UTC
Updated:
6 Mar 2018 at 13:28 UTC
Jump to comment: Most recent
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
Comment #1
simon georges commentedWould you be willing to provide such a patch? ;-)
Comment #2
nwom commentedThis 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).Comment #3
polGeocoder Fix ?!
Comment #4
nwom commentedI 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.
Comment #5
polAs @simon said,
Could you provide a patch so we can test it and commit it eventually ?
Thanks.
Comment #6
nwom commentedI 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.
Comment #7
socialnicheguru commentedI am finding that if I have an addressfield and geofield in a field_collection, then it will not geocode on save of the entity.
Comment #8
pol