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

michaelfavia’s picture

@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).

indytechcook’s picture

Please 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.

scottrigby’s picture

Status: Active » Needs review
StatusFileSize
new9.35 KB

I 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 =)

pbuyle’s picture

I 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.

jief’s picture

Here is the patch from mongolito404's commit

jief’s picture

Same patch with a fix for widget type name

hnln’s picture

Would 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).

pbuyle’s picture

@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 patched geocoder_widget_validate).

camidoo’s picture

rerolled 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.

        $item += array('master_column' => $master_column);
        geofield_compute_values($item, $item['master_column']);
        $items[$delta] += $item; // changing this from $items[$delta] = $item to a `merge`

** note i've only tested this with the addressfield module, so it's the only one i've confirmed it working with.

camidoo’s picture

was a bit hasty with the above patch, here's a new one.

hnln’s picture

Last 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 :-).

tizzo’s picture

StatusFileSize
new11.72 KB

This 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

phayes’s picture

Assigned: Unassigned » phayes

Thanks for this! I'm going to see if I can get this implement and working correctly right now.

phayes’s picture

Commited! Thanks for the patch all!

phayes’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jsibley’s picture

Issue summary: View changes

Hope 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.

jsibley’s picture

Status: Closed (fixed) » Active
pedrosp’s picture

When 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

  • phayes committed 72f104f on 7.x-2.x
    [#1297654] Geocoding from field should wait for complete entity...
pol’s picture

Status: Active » Fixed

  • phayes committed 72f104f on 8.x-2.x
    [#1297654] Geocoding from field should wait for complete entity...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.