You are here

function geofield_field_presave in Geofield 7

Same name and namespace in other branches
  1. 7.2 geofield.module \geofield_field_presave()

Implements hook_field_presave(). PDO throws an error when attempting to insert an empty string into a float field. Go through all values and convert empty strings to NULL.

File

./geofield.module, line 36

Code

function geofield_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  if ($field['type'] === 'geofield') {
    foreach ($items as $delta => $item) {
      if (!empty($item)) {
        foreach ($item as $k => $v) {
          if ($v === '') {
            $item[$k] = NULL;
          }
        }
        $widget = $instance['widget'];
        if ($widget['type'] == 'geofield_wkt') {
          $master_column = 'wkt';
        }
        elseif ($widget['type'] == 'geofield_latlon') {
          $master_column = 'latlon';
        }
        elseif ($widget['type'] == 'geofield_bounds') {
          $master_column = 'bounds';
        }
        elseif ($widget['type'] == 'geofield_geolocation') {
          $master_column = 'latlon';
        }
        else {
          $master_column = 'wkt';
        }
        $item += array(
          'master_column' => $master_column,
        );
        geofield_compute_values($item, $item['master_column']);
        $items[$delta] = $item;
      }
    }
  }
}