diff --git a/geolocation.devel_generate.inc b/geolocation.devel_generate.inc index 64b5ee7..caf0f93 100644 --- a/geolocation.devel_generate.inc +++ b/geolocation.devel_generate.inc @@ -21,19 +21,10 @@ function geolocation_devel_generate($object, $field, $instance, $bundle) { * Callback for hook_devel_generate(). */ function _geolocation_devel_generate($object, $field, $instance, $bundle) { - $lat = rand(-90, 90) - rand(0, 999999) / 1000000; - $lng = rand(-180, 180) - rand(0, 999999) / 1000000; - $lat_sin = sin(deg2rad($lat));; - $lat_cos = cos(deg2rad($lat)); - $lng_rad = deg2rad($lng); - $geolocation = array( - 'lat' => $lat, - 'lng' => $lng, - 'lat_sin' => $lat_sin, - 'lat_cos' => $lat_cos, - 'lng_rad' => $lng_rad, + 'lat' => rand(-90, 90) - rand(0, 999999) / 1000000, + 'lng' => rand(-180, 180) - rand(0, 999999) / 1000000, ); - return $geolocation; + return _geolocation_field_precalculate_values($geolocation); } diff --git a/geolocation.migrate.inc b/geolocation.migrate.inc new file mode 100644 index 0000000..38bd4fc --- /dev/null +++ b/geolocation.migrate.inc @@ -0,0 +1,16 @@ + 2, + 'field handlers' => array('MigrateGeolocationFieldHandler'), + ); +} diff --git a/geolocation.module b/geolocation.module index c956eae..1e4d524 100644 --- a/geolocation.module +++ b/geolocation.module @@ -144,10 +144,7 @@ function geolocation_field_validate($entity_type, $entity, $field, $instance, $l function geolocation_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) { foreach ($items as $delta => $item) { // Precalculate some goodness. - $item['lat_sin'] = sin(deg2rad($item['lat'])); - $item['lat_cos'] = cos(deg2rad($item['lat'])); - $item['lng_rad'] = deg2rad($item['lng']); - $items[$delta] = $item; + $items[$delta] = _geolocation_field_precalculate_values($item); } } @@ -305,3 +302,25 @@ function geolocation_field_widget_error($element, $error, $form, &$form_state) { break; } } + +/** + * Helper function. Precalculates values for geolocation field item. + * + * @param array $item + * Geolocation field item with the following keys: + * - lat: (required) Latitude + * - lng: (required) Longitude + * + * @return array + * Geolocation field item with added precalculated values: + * - lat_sin: Latitude sine + * - lat_cos: Latitude cosine + * - lng_rad: Radian longitude + */ +function _geolocation_field_precalculate_values(array $item) { + $item['lat_sin'] = sin(deg2rad($item['lat'])); + $item['lat_cos'] = cos(deg2rad($item['lat'])); + $item['lng_rad'] = deg2rad($item['lng']); + + return $item; +} diff --git a/migrate/destinations/geolocation.inc b/migrate/destinations/geolocation.inc index 5e5e527..4581499 100644 --- a/migrate/destinations/geolocation.inc +++ b/migrate/destinations/geolocation.inc @@ -18,9 +18,9 @@ * - 'lng': Geolocation field longitude value. * - 'language': Geolocation field value language. * - * See more at https://www.drupal.org/node/2417365 + * See more at https://www.drupal.org/node/2671068 */ -class GeolocationMigrateFieldHandler extends MigrateFieldHandler { +class MigrateGeolocationFieldHandler extends MigrateFieldHandler { /** * {@inheritdoc} @@ -85,7 +85,7 @@ class GeolocationMigrateFieldHandler extends MigrateFieldHandler { if ($this->validate($entity, $field_info, $instance, $item)) { $language = $this->getGeolocationFieldLanguage($entity, $field_info, $arguments, $delta); - $return[$language][] = $item; + $return[$language][] = _geolocation_field_precalculate_values($item); } }