Geocoding with an computed field works great after saving the node for the first time. After editing/changing the computed field shows updated values but not the geofield/geocoded field.

The computed field does something like:

$point = geocoder('google',$address);
$entity_field[0]['value'] =$point->out('json'); 

The geofield encodes from the computed field using the geojson encoder.

Comments

drewschmaltz’s picture

I have exactly the same problem. I was like "didn't this work before?" - now I realize it is only on edits when it doesn't work... let's try and figure it out - tell me if you've gotten anywhere.

drewschmaltz’s picture

Component: Miscellaneous » Code
Status: Active » Needs work

This code causes computed fields to be ignored always. Commenting out this "return FALSE" allows computed fields to be geocoded.

If I knew where to start I'd edit this code to deal with these fields properly, but I'm not really sure. Maybe someone more familiar with what is being done here could help...

if ($entity) {
      if (!empty($entity->original)) {
        //@@TODO: Deal with entity-properties (non-fields)
        //@@TODO: This isn't working with file fields. Should use some kind of lookup / map
        $field_original = field_get_items($entity_type, $entity->original, $field_name, isset($entity->original->language) ? $entity->original->language : NULL);
        if (!empty($field_original)) {
          $diff = geocoder_widget_array_recursive_diff($field_original, $source_field_values);
          if (empty($diff)) {
 //         return FALSE;
          }
        }
      }
    }
dwalker51’s picture

I had similar problems, but even on creating a new node with the following code: http://drupal.org/node/1534654#comment-6757108

drewschmaltz’s picture

Confirmed on created the new node. Check my post ^ - it fixes it, but isn't a fix.

varunity’s picture

Thanks @drewschmaltz, this workaround helps!

drewschmaltz’s picture

Okay, so my fix only helped with editing and essentially used the data from the last form submission to create location data. The problem is that computed field runs on update. I simply added a presave function to computed_field and the problem was solved. I could even go back to geocoder and remove this comment.

greenskunk’s picture

I have this issue with the Address field being geocoded and shown on an OpenLayers map is working on the Edit form, however, after you click save then the data is not saved in the geofield.

Is your fix specific to your computed field or did you modify a module?

greenskunk’s picture

I recommend testing @phayes patch at http://drupal.org/node/1862922#comment-6980730

foopang’s picture

Tested the patch from phayes, it doesn't solve this problem.

@drewschmaltz, you are right. The problem is that computed field runs on update, but geocoder check the source field value on presave. But I just can't figure out how to added a presave function to computed_field, would you please advise? Thanks so much.

lhugg’s picture

I was going to add this issue, but found it here. It seems that many of us are still having this problem.
From what I can see, geocoder_google_field in google.inc never executes when a computed field is used. Has any of the above been resolved, or is there any additional guidance on how to use a presave function to work around?

Morasta’s picture

The problem is that computed field runs on update. I simply added a presave function to computed_field and the problem was solved.

@drewschmaltz : can you elaborate on this presave function or share what you came up with?

adognameddog’s picture

I've had the same problem. I use the Geolocation module.

I changed the widget from Geocode from another field to Latitude/Longitude.

Next, I implemented the hook_field_attach_presave() hook. This is the code. It's not quite generic, so you'll have to see what parts you can reuse.

/**
 * Implements hook_field_attach_presave().
 *
 * Add the latitude/longitude values for the provided address to the
 * field_geolocation field.
 */
function MYMODULE_field_attach_presave($entity_type, $entity) {
  // Only use for entity_type node.
  if ($entity_type == 'node') {
    // And only if we're inserting or updating nodes of type 'premises'.
    if($entity->type == 'premises') {

      // Generate the address from several fields in the entity.
      $street = field_get_items('node', $entity, 'field_premise_address_street');
      $number = field_get_items('node', $entity, 'field_premise_address_number');
      $city = field_get_items('node', $entity, 'field_premise_address_citypart');
      $address = $street[0]['value'] . ' ' . $number[0]['value'] . ' ' . $city[0]['value'];

      // Get the latitude and longitude.
      $point = geocoder('google', $address);

      // Use that data for the field_geolocation field. Note that we have to
      // add more than just lat/lng.
      $entity->field_geolocation[LANGUAGE_NONE][0] = array(
        'lat' => $point->coords[1],
        'lng' => $point->coords[0],
        'lat_sin' => sin($point->coords[1]),
        'lat_cos' => cos($point->coords[1]),
        'lng_rad' => deg2rad($point->coords[0]),
      );
    }
  }
}

This is how I get the lat/lng from multiple fields. Note that street, number and city(part) are required fields in my setup. Do not just reuse this code, but check thoroughly.

khan2ims’s picture

I am using geofield in a user account page. As a very simple fix for presave function, I have added a rule with event "After updating an existing user account". Then added an action "Save entity". What it does is, it saves the user account page AGAIN after updating it first. This way the computed field is also getting updated.

I know this is not ideal fix, but it works for me!

zmove’s picture

Same problem here. Why geocoder is marked as compatible with computed on the module page whereas it doesn't seems to work ?

jsibley’s picture

Issue summary: View changes

@khan2ims, what data selector are you using for your rule?

Thanks.

antonello.dipinto’s picture

any issue ?
thanks

Molfar’s picture

StatusFileSize
new1015 bytes

Here is the patch. It overheats some functionality from computedfield, but works fine.

simon georges’s picture

candelas’s picture

Status: Needs work » Closed (fixed)

Solved in last dev, so I close the issue. Now when you edit a event and the address is not changed, the geocoder coordinates don't get overridden to nothing.

candelas’s picture

At the end I had to make a patch because it was not deleting the coordinates when it had an error #1293794: Geocoder doesn't clear Geofield if it receives a malformed address

khalor’s picture

Status: Closed (fixed) » Active

This is still an issue in the latest dev, even after applying patches cleanly from #17 and also the patch in #1293794-9: Geocoder doesn't clear Geofield if it receives a malformed address. The 'fix' in #2 solves the issue, but it's not a solution...

candelas’s picture

@Khalor did they change the dev? I have a site going with the patch in #1293794-9: Geocoder doesn't clear Geofield if it receives a malformed address and it works well. I didn't use the patch in this issue.

khalor’s picture

Those patches aren't in dev yet (@candelas do you have module access?) so regardless this isn't resolved.
Looking at it closer it seems that #1293794: Geocoder doesn't clear Geofield if it receives a malformed address may(?) solve the edit/save issue, but my experience has been neither of those patches + dev allowed Computed Fields to be Geocoded on a node save on either edit or creation. So not sure what to mark this issue as (maybe RTBC if it's working for you?), and will move over to #1970288: computed field not geocoding for the more general computed field problem.

candelas’s picture

@Khalor I have the patch #1293794-9: Geocoder doesn't clear Geofield if it receives a malformed address working in 7.x-1.2+18-dev that is the actual dev and it is working. Have you cleared cache? What isn't it working?

khalor’s picture

Geocoding from a computed field to a geofield doesn't work (on new node save or node edit) as it seems field value isn't computed (which it does on update) when geocoder runs (which it does on presave). So I think #1293794: Geocoder doesn't clear Geofield if it receives a malformed address is a separate issue, possibly allowing computed fields to be geocoded when a node is edited (although it'll be the old computed value that existed before the save/update) but new nodes still aren't geocoded so I don't think it's a true fix to this (editing) issue as it'll be using old values.

candelas’s picture

@Khalor: I have just created a new event.
Which version are you using? I use 7.x-1.2+9-dev with the patch
Which other modules are you using?

puddyglum’s picture

@candelas, I'm having this same issue, using 7.x-1.2+18-dev. When I Geocode from another field it simply never works. If I use Long/Lat it works fine. Is it working for you?

puddyglum’s picture

I can replicate this pretty easily.

Drush 6:
drush qd geocode-1679926 --dev geofield geocoder geophp computed_field ctools

Manage fields of Basic Page

Add Computed field, set value to be "1600 Pennsylvania Avenue, Washington DC"

Add Geofield, set widget to be Geocode from another field, select Computed

Create a Basic Page
(no geofield)

Edit the Basic Page
(no geofield)

Unless I'm missing something obvious, geofield from computed field basically broken in dev and stable?

puddyglum’s picture

My workaround was to not use computed_field, and use addressfield instead. I used Rules to populate the Address Field values on a condition of "Before saving content". Then set Geocode from another field to the Address field. This works pretty well for me, although I'd rather use computed field for simplicity and flexibility.

IMO using Rules was so quick and easy that I think it should be the preferred method instead of Computed Field.

candelas’s picture

@jmonkfish I use the Geocoder 7.x-1.2+18-dev , Geofield 7.x-2.1 and the patch in #1293794-9: Geocoder doesn't clear Geofield if it receives a malformed address and works.

puddyglum’s picture

Downgraded to geofield-7.x-2.1 and applying that patch didn't make any difference, unfortunately. It seems like Khalor is having the same issue. It won't save for newly created content or for editing content.

capynet’s picture

Status: Active » Needs review
StatusFileSize
new3.34 KB
new3.43 KB

Hi, I had the same problem and after some debug I realized computed field was called after geocoder stores the values on node edit scenario.

I made 2 patches may help. The first one is for stable release and the second for dev code.

patch for stable was tested for two months right now without problem.

bye!

pol’s picture

Status: Needs review » Reviewed & tested by the community

Those last patch make sense to me.

rob.costello’s picture

@capynet how are you setting your computed field? For me, setting the computed field value as per the module instructions ($entity_field[0]['value'] = 'bla') results in the geocoder_google function being passed an array and returning an error. I've created a patch which just takes the first value in this situation, but this won't work for multi-value computed fields so this might need a rethink.

rob.costello’s picture

Status: Reviewed & tested by the community » Needs work
indigoxela’s picture

Hi,
I'd like to test the latest patch, but unfortunately it won't apply.

First I have to add "-p 6", which is very uncommon for drupal patches, then I realize that it isn't compatible with latest geocoder code anymore.

@rob.costello and/or capynet could you please provide a compatible new patch?

Many thanks in advance.

pol’s picture

Status: Needs work » Closed (outdated)