My scenario:

  • OpenLayers widget
  • Shape modification/deletion: allowed
  • Geofield is not required (empty values should be allowed)
  • Geofield has a default value (e.g. "POLYGON ((...")

How to reproduce the error::

  1. New content
  2. Delete all shapes from map
  3. Save

Here it is:
PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect decimal value: 'P' for column 'field_my_geofield_lat' at row 1: INSERT INTO {field_data_field_my_geofield} (entity_type, entity_id, revision_id, bundle, delta, language, field_my_geofield_geom, field_my_geofield_geo_type, field_my_geofield_lat, field_my_geofield_lon, field_my_geofield_left, field_my_geofield_top, field_my_geofield_right, field_my_geofield_bottom, field_my_geofield_geohash) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13, :db_insert_placeholder_14); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 372 [:db_insert_placeholder_2] => 372 [:db_insert_placeholder_3] => my_bundle [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => P [:db_insert_placeholder_7] => P [:db_insert_placeholder_8] => P [:db_insert_placeholder_9] => P [:db_insert_placeholder_10] => P [:db_insert_placeholder_11] => P [:db_insert_placeholder_12] => P [:db_insert_placeholder_13] => P [:db_insert_placeholder_14] => P ) in field_sql_storage_field_storage_write() (linea 448 di /var/www/vhosts/example.com/httpdocs/modules/field/modules/field_sql_storage/field_sql_storage.module).

Possible explanation: default values are not handled correctly.

Function field_default_insert is called with $items = array()

It then gets the default value for the field, and sets $items to a string ("POLYGON ((...")

If we do not delete the shapes from the map, the $items variable may look like this:
... (Array, 1 element)
... ... 0 (Array, 9 elements)
... ... ... geom (String, 93 characters ) .... @??? ??9.h???H@W?1??k;@9.h???H@3k...
... ... ... geo_type (String, 7 characters ) polygon
... ... ... lat (Float) 42.07043421578
... ... ... lon (Float) 12.751242214244
... ... ... top (Float) 49.701744604858
... ... ... bottom (Float) 33.841218030871
... ... ... right (Float) 27.421874534701
... ... ... left (Float) -2.1093749642078
... ... ... geohash (Boolean) FALSE

... and field_default_insert does not alter it.

Comments

Brandonian’s picture

Title: OpenLayers widget, inserting a new node: PDOException in field_sql_storage_field_storage_write() » Geofield: Empty values on widgets with default values skip validation
Priority: Normal » Major

Thanks for the bug report, @tito.brasolin. It appears that this also affects our other widgets when setting a default value. For example, setting a default value with the WKT input, then clearing it out will cause a different, but likely related error. In the WKT widget's case, it actually saves raw WKT into our WKB field, causing an error on node load.

Marking as major, hope to have a commit fix today.

Brandonian’s picture

Assigned: Unassigned » Brandonian
Priority: Major » Normal

Ok, so this looks like we're reacting poorly to a core bug (#1887098: Clearing out a field's default value on insert causes entity to be saved with default value). I intend to fix the issue with Geofield by treating this as an edge case, and dumping default values into empty geofields on initial save. I don't think it's ideal, but IMHO it's the best option we have until the issue is fixed upstream. Also, any solution with #1855564: Make geometry fail non-lethal will probably make this less of an issue.

Brandonian’s picture

Status: Active » Fixed

The core issue I opened is a duplicate of #1253820: It's impossible to submit no value for a field that has a default value, which looks like it's being worked on for D8. We'll see how it goes for D7.

In the meantime, I've made a commit to duplicate the core functionality by dropping in the default value into empty Geofields whenever it's a new entity. We should remove this code once the core issue has been fixed in D7.

http://drupalcode.org/project/geofield.git/commit/5d8e010

Status: Fixed » Closed (fixed)

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

jpstrikesback’s picture

Status: Closed (fixed) » Active
StatusFileSize
new534 bytes

There are scenarios where the default value is NULL (or at least one from my testing - Lat / Lon Widget - no defaults), when this is the case the foreach at line 290 chokes. Here is a patch with a little ternary, I'm not sure if this should be handled here or at the widget level, but this sorts it. This isn't a big deal via the UI but gives Rules something to die on.

jpstrikesback’s picture

Status: Active » Needs review

Also, please let me know if this should be in a new issue :)

Yorgg’s picture

I've applied the patch though In my case Geofield refuses to save default values in the latitude/longitude input field.
The issue is I can't index nodes without a value in apache solr. 1955576 indexing geofield

mac_weber’s picture

There is a duplicate issue #1904190: Notice: Undefined index: default_value in geofield_field_presave() (line 280
Which one shall we close?

I tested the patch on comment #5. I don't get anymore the error message described on the duplicated issue, yet I'm not sure it really fixes the problem or just masquerades it. I think @Brandonian may answer it better.

@stamina I just replied you on the other issue. This one has nothing to do with the other one you had opened.

jpstrikesback’s picture

I vote for leaving this one open, since there is a workaround/fix here...

I think what was committed here:

http://drupalcode.org/project/geofield.git/commit/5d8e010

assumes that there is a default value in the field instance being passed to geofield_field_presave() but this is not always the case.

The patch in #5 should be fine unless an empty default value on a widget should be saved as an empty array?

mac_weber’s picture

Status: Needs review » Reviewed & tested by the community
mac_weber’s picture

Re-roling patch. Authorship is given to @jpstrikesback.

rogerhyam’s picture

I'm new to Geofield but am struggling with this one. The only way I have been able to create records with blank geofield entries is by commenting out the fix block for this problem!

/*
if ($instance['required'] == 0 && empty($items)) {
$entity_ids = entity_extract_ids($entity_type, $entity);
if (empty($entity_ids[0])) {
$items = array($instance['default_value']);
}
}
*/

The logic in the geofield_field_presave function appears to re-create an items array if you are creating a new node. This then causes the $items[$delta]['geom'] on line 294 to throw a warning but go on to create a row in the field_data_* table that is a non-valid geom so the view page won't display.

I spent 2 hours on this! Can it be that, out of the box, you can't create nodes with blank geofields in them?

It is OK if you edit and existing node so the field has no content though...

Otherwise thanks for all the work. Module looks cool.

xenophyle’s picture

The patch at comment 11 worked for me.

jelle_s’s picture

+++ b/geofield.moduleundefined
@@ -278,7 +278,7 @@ function geofield_field_presave($entity_type, $entity, $field, $instance, $langc
       if (empty($entity_ids[0])) {
-        $items = array($instance['default_value']);
+        $items = isset($instance['default_value']) ? $instance['default_value'] : array();

The default value should be in an array if it is set.

New patch attached.

jelle_s’s picture

Status: Reviewed & tested by the community » Needs review
jpstrikesback’s picture

deleted

mac_weber’s picture

@Jelle_S your patch is exactly the same as the patch at #11

jelle_s’s picture

Status: Needs review » Reviewed & tested by the community

No it isn't:

array($instance['default_value'])

vs

$instance['default_value']
mac_weber’s picture

@Jelle_S thanks, I haven't figured out that on late night. I think I was in need of a good sleep ;)
Your patch is the correct one.

jpstrikesback’s picture

Same here ;) hence my deleted comment :) cheers Jelle_S

jhedstrom’s picture

Patch in #14 resolves the issue.

dddbbb’s picture

#14 fixed it for me. Thanks!

Brandonian’s picture

Status: Reviewed & tested by the community » Fixed

Patch at #14 committed. Thanks for the patch, @Jelle_S!

http://drupalcode.org/project/geofield.git/commit/c693755

Status: Fixed » Closed (fixed)

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