Currently, the geocoding of field data happens in the geocoder_widget_validate during validation of Geocoder widget's inputs. But the geocoding actually uses data from other fields, which may not have been validated and/or fully processed. Because of this, geocoding can happens on invalid or incomplete data.
Drupal 7's Field API provides the hook_field_attach_presave hook that let any module act before fields value are saved. It would be better to use this for server-side to only access fully processed and validated data for the geocoding.
A while ago, I made a GitHub fork of phayes' port that do just that, see https://github.com/K-Company/geocode/commit/92b7acfba0dd4fd99d5ed48a1877...
Comments
Comment #1
michaelfavia commented@mongolito404: thank you! I prefer this method as well. If you can reroll this commit to apply cleanly against the renamed module ill commit asap. Others seem to be arguing to keep both methods but i don't see much value in that (though i may be missing something).
Comment #2
indytechcook commentedPlease do!!! That really bothered me with the module. We ended up having to write separate code to encode outside of the UI. Especially with google's API limits.
Comment #3
scottrigbyI went ahead and made a git-formatted patch from mongolito404's github commit. I only changed the names to geocoder, and some minor coding convention stuff… have not tested =)
Comment #4
pbuyle commentedI merged my changes back into the 7.x-1.x branch from Drupal.org and published the result on GitHub at https://github.com/K-Company/geocode/tree/hook_field_attach_submit. You can get a aggregated diff at https://github.com/K-Company/geocode/compare/7.x-1.x...hook_field_attach.... This branch should be mergeable into the repository hosted in Drupal.org (https://github.com/K-Company/geocode must be added as remote of your local repository).
This is currently untested.
@scottrigby thanks for the patch. But in my repository, support for title/name was missing and parsing of field values was delegated in the handler themselves. In the new merged branch, I restored them.
Comment #5
jief commentedHere is the patch from mongolito404's commit
Comment #6
jief commentedSame patch with a fix for widget type name
Comment #7
hnln commentedWould this patch also fix the problem that nodes created programatically or saved via batch (vbo) don't get geocoded. As they do geocode when saving the node manually.
Patch didn't work for me though, I now get errors when saving manually:
Warning: Illegal offset type in isset or empty in field_info_field() (line 617 of modules/field/field.info.inc).
Warning: Illegal offset type in isset or empty in field_language() (line 294 of modules/field/field.multilingual.inc).
Warning: Illegal offset type in isset or empty in field_language() (line 294 of modules/field/field.multilingual.inc).
Notice: Array to string conversion in field_get_items() (line 972 of modules/field/field.module).
Notice: Undefined variable: values in geocoder_field_attach_submit() (line 330 of modules/contrib/geocoder/geocoder.module).
Warning: array_intersect_key(): Argument #1 is not an array in geocoder_field_attach_submit() (line 330 of modules/contrib/geocoder/geocoder.module).
Comment #8
pbuyle commented@HnLn the patch implements the geocoding in
hook_field_attach_presave. So yes, it should also works when saving node programmatically or batch. To trigger the geocoding, each value of the field must have a TRUE 'geocode' property (as set in the patchedgeocoder_widget_validate).Comment #9
camidoo commentedrerolled this patch to patch cleanly against the latest. Additionally there were a few problems, the ones HnLn had spoke of, the fact that setting the form values in the hook_widget_validate() were being overwritten by the hook_field_presave in the geofield module. Which meant the 'geocode' => TRUE, would never stick.
~ line 61 - 62 in geofield.module fixes the issue (patch attached)
not sure of the implications of this, however so far i haven't noticed any adverse effects.
** note i've only tested this with the addressfield module, so it's the only one i've confirmed it working with.
Comment #10
camidoo commentedwas a bit hasty with the above patch, here's a new one.
Comment #11
hnln commentedLast one didn't work for me, errors are gone indeed but now I get 0,0 coordinates (and that's not where my address is :-).
Comment #12
tizzo commentedThis wasn't working for me for programmatic saving. I'm not sure I follow the logic that was in there for marking geocoded fields for encoding, it seems like we need to try to encode all properly configured fields in case data changed in other fields. This patch seems to be working for us and I think it should successfully apply on top of 7.x-1.x
Comment #13
phayes commentedThanks for this! I'm going to see if I can get this implement and working correctly right now.
Comment #14
phayes commentedCommited! Thanks for the patch all!
Comment #15
phayes commentedComment #17
jsibley commentedHope it's ok to reopen this.
@phayes, in #14 you say that the patch is committed, but I'm not seeing anything about presave in geocoder.module for 7.x-1.2
Am I looking in the wrong place?
Thanks.
Comment #18
jsibley commentedComment #19
pedrospWhen an adressfield is updated/changed, there is no update for the geocode field related.
I guess it should be updated automatically, without having to hook alter the update form and force a new geocode. Right ?
I thought it could be related with this patch, but I cannot apply #12 patch to current 7.x-1.2
Comment #21
pol