You are here

geofield.install in Geofield 7

Same filename and directory in other branches
  1. 7.2 geofield.install

Install, update and uninstall functions for the geofield module.

File

geofield.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the geofield module.
 */

/**
 * Implements hook_field_schema().
 */
function geofield_field_schema($field) {
  return array(
    'columns' => array(
      'wkt' => array(
        'type' => 'text',
        'size' => 'big',
        'not null' => FALSE,
      ),
      'geo_type' => array(
        'type' => 'text',
        'size' => 'normal',
        'not null' => FALSE,
      ),
      'lat' => array(
        'type' => 'float',
        'not null' => FALSE,
      ),
      'lon' => array(
        'type' => 'float',
        'not null' => FALSE,
      ),
      'left' => array(
        'type' => 'float',
        'not null' => FALSE,
      ),
      'top' => array(
        'type' => 'float',
        'not null' => FALSE,
      ),
      'right' => array(
        'type' => 'float',
        'not null' => FALSE,
      ),
      'bottom' => array(
        'type' => 'float',
        'not null' => FALSE,
      ),
      'srid' => array(
        'type' => 'text',
        'not null' => FALSE,
      ),
      'accuracy' => array(
        'type' => 'int',
        'not null' => FALSE,
      ),
      'source' => array(
        'type' => 'text',
        'not null' => FALSE,
      ),
    ),
  );
}

/**
 * Changing srid from int to text
 */
function geofield_update_7100() {
  $fields = field_info_fields();
  foreach ($fields as $field_name => $field) {
    if ($field['type'] == 'geofield' && $field['storage']['type'] == 'field_sql_storage') {

      // get new schema
      $schema = geofield_field_schema(array());
      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, $schema['columns']['srid']);
          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();
}

Functions

Namesort descending Description
geofield_field_schema Implements hook_field_schema().
geofield_update_7100 Changing srid from int to text