You are here

function geofield_update_7100 in Geofield 7.2

Same name and namespace in other branches
  1. 7 geofield.install \geofield_update_7100()

Changing srid from int to text

File

./geofield.install, line 106
Install, update and uninstall functions for the geofield module.

Code

function geofield_update_7100() {
  $fields = field_info_fields();
  foreach ($fields as $field_name => $field) {
    if ($field['type'] == 'geofield' && $field['storage']['type'] == 'field_sql_storage') {
      $srid_schema = array(
        'type' => 'text',
        'not null' => FALSE,
      );
      foreach ($field['storage']['details']['sql'] as $type => $table_info) {
        foreach ($table_info as $table_name => $columns) {
          $column_name = _field_sql_storage_columnname($field_name, 'srid');

          // Get srid int values
          $values = db_select($table_name, 'f')
            ->fields('f', array(
            $column_name,
            'entity_type',
            'bundle',
            'entity_id',
          ))
            ->execute()
            ->fetchAssoc();
          db_change_field($table_name, $column_name, $column_name, $srid_schema);
          if (!empty($values)) {

            // Put the values back as text
            foreach ($values as $value) {
              if ($value->{$column_name}) {
                $new_value = 'EPSG:' . $value->{$column_name};
                db_update($table_name)
                  ->fields(array(
                  $column_name => $new_value,
                ))
                  ->condition('entity_type', $value->entity_type)
                  ->condition('bundle', $value->bundle)
                  ->condition('entity_id', $value->entity_id)
                  ->execute();
              }
            }
          }
        }
      }
    }
  }
  field_cache_clear();
}