In my opinion it's a matter of some missing "['geom']" in function geofield_field_widget_form.

Lines 153-158 should be:

153 if (isset($items[$delta]['geom']['lat'])) {
154 $latlon_value['lat'] = floatval($items[$delta]['geom']['lat']);
155 }
156 if (isset($items[$delta]['geom']['lon'])) {
157 $latlon_value['lon'] = floatval($items[$delta]['geom']['lon']);
158 }

And lines 177-188:

177 if (isset($items[$delta]['geom']['top'])) {
178 $bounds_value['top'] = $items[$delta]['geom']['top']; // TODO Use floatval?
179 }
180 if (isset($items[$delta]['right'])) {
181 $bounds_value['right'] = $items[$delta]['geom']['right'];
182 }
183 if (isset($items[$delta]['geom']['bottom'])) {
184 $bounds_value['bottom'] = $items[$delta]['geom']['bottom'];
185 }
186 if (isset($items[$delta]['geom']['left'])) {
187 $bounds_value['left'] = $items[$delta]['geom']['left'];
188 }

Comments

Brandonian’s picture

Offhand, that's definitely not the solution, since 'geom' is a separate column from lat/lon, not an overarching array. Will look into it, though

tito.brasolin’s picture

Maybe the right way to go is to remove "['geom']" from geofield_field_is_empty, geofield_geometry_from_values and so on? Possibly related: Cannot safely change widget type.

Brandonian’s picture

Status: Active » Fixed

@tito.brasolin, this is where I eat crow...

You were correct in the original post. I had forgotten that as a part of our updated widget input, I had implemented it to work with an array with a geom attribute. @phayes and I had recently talked about tweaks to how we interact with fields to simplify this process on a code level even further, and I guess I thought I had already implemented that... :-/

Thanks again for catching this. As a reward for being a better debugger than one of the co-maintainers, I gave you the commit credit, along with any bragging rights that come along with it. :-)

Brandonian’s picture

malberts’s picture

Status: Fixed » Active

Excuse me if I'm missing something obvious, but with the current dev version my LAT/LON default values are not populated.

In geofield.widgets.inc:

      if (isset($items[$delta]['geom']['lat'])) {
        $latlon_value['lat'] = floatval($items[$delta]['geom']['lat']);
      }
      if (isset($items[$delta]['geom']['lon'])) {
        $latlon_value['lon'] = floatval($items[$delta]['geom']['lon']);
      }

When I inspect $items I get:

Array
(
    [0] => Array
        (
            [geom] => POINT (31 29)
            [geo_type] => point
            [lat] => 29.000000000000
            [lon] => 31.000000000000
            [left] => 31.000000000000
            [top] => 29.000000000000
            [right] => 31.000000000000
            [bottom] => 29.000000000000
            [geohash] => stnhc
        )
)

I don't have the data structure that was "fixed" here.

The field used to be "Geocode from another field" with Geometry type "Point (default)". Now it uses the "Latitude / Longitude" widget.

malberts’s picture

On second thought, this issue is for the default value. I was actually thinking about the value that was set in the field. When editing, shouldn't the LAT/LON field be populated with the value that was saved? My output above is when I'm on an edit form. When I edit the Default value it does put 'lat' and 'lon' in the 'geom' structure.

Brandonian’s picture

Title: Widgets: bounds and lat/lon do not display default values » Add documentation on how to save a Geofield programatically.

@malberts, Geofield does some processing on the input that converts it to what's finally saved in the database; it's not a 1 to 1 relationship with the data. Geofield currently accepts data as an array with an 'input_format' key and an 'geom' key. This is described in more detail in #1648976: Make it simpler to programmatically add Geofield data, and should be added to the 2.x documentation.

balagan’s picture

After some debugging, I found that the suggested 7.x-2.0-alpha2 version should be updated something like

  $node->field_wptgeofield[$node->language][0]['wkt'] = 'POINT ('.$lon.' '.$lat.')';
  $node->field_wptgeofield[$node->language][0]['geo_type'] = 'point';

Until the recommended version does not support the ['geom'] and input_format arrays, please update documentation to indicate that the ['geom'] array and the input_format is only used in the devel version.

p4trizio’s picture

I know it's an old post, but you must use geofield_compute_values function, it is enough documented in latest module versions

AaronBauman’s picture

Version: 7.x-2.x-dev » 8.x-1.x-dev

Same doc needed for 8.x

I'm setting lat, lon, and geo_type, but it's not saving correctly.

This info should be in README

  • itamair committed e84d908 on 8.x-1.x
    Issue #1840894: Add documentation on how to save a Geofield...
itamair’s picture

Last commit added detailed documentation on how to save a Geofield programmatically in Drupal 8.

itamair’s picture

Status: Active » Fixed

  • itamair committed f2fb95d on 8.x-1.x
    Issue #1840894: Updated documentation on how to save a Geofield...

Status: Fixed » Closed (fixed)

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