-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeofield.module
102 lines (87 loc) · 3.03 KB
/
geofield.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* @TODO: Provide function/method to determine version.
*/
require_once('geofield.elements.inc');
/*require_once('geofield.widgets.inc');
require_once('geofield.formatters.inc');
require_once('geofield.openlayers.inc');
require_once('geofield.feeds.inc');
require_once('geofield.apachesolr.inc');
require_once('geofield.schemaorg.inc');
require_once('geofield.microdata.inc');*/
/* *
* Max length of geohashes (imposed by database column length).
*/
define('GEOFIELD_GEOHASH_LENGTH', 16);
/**
* Diameter of the Earth in kilometers.
*/
define('GEOFIELD_KILOMETERS', 6371);
/**
* Diameter of the Earth in meters.
*/
define('GEOFIELD_METERS', 6371000);
/**
* Diameter of the Earth in miles.
*/
define('GEOFIELD_MILES', 3959);
/**
* Diameter of the Earth in yards.
*/
define('GEOFIELD_YARDS', 6975175);
/**
* Diameter of the Earth in feet.
*/
define('GEOFIELD_FEET', 20925525);
/**
* Diameter of the Earth in nautical miles.
*/
define('GEOFIELD_NAUTICAL_MILES', 3444);
/**
* Implements hook_field_info().
*/
function geofield_field_info() {
return array(
'geofield' => array(
'label' => 'Geofield',
'description' => t('This field stores geo information.'),
'default_widget' => 'text_textfield',
'default_formatter' => 'text_plain',
'field item class' => 'Drupal\geofield\Type\GeofieldItem',
'settings' => array(
'backend' => 'geofield_backend_default',
),
),
);
}
/**
* Implements hook_field_is_empty().
* @TODO: Handle empty geometries. (i.e., a WKT value of GEOMETRYCOLLECTION EMPTY)
*/
function geofield_field_is_empty($item, $field) {
return !isset($item['value']) || $item['value'] === '';
}
/**
* Implements hook_field_presave().
* - There's a decent chance this hook will go away in favor of a method on the field item class.
*/
function geofield_field_presave($entity, $field, $instance, $langcode, &$items) {
$field_info = $instance->getField();
$geofield_name = $field_info->id;
// @TODO: actually check field settings ($field['settings']['backend'])
$backendPlugin = \Drupal::service('plugin.manager.geofield_backend')->createInstance('geofield_backend_default');
$field = $entity->get($geofield_name);
foreach ($items as $delta => $item) {
// @TODO: There must be a better way to do this. :-(
$items[$delta]['value'] = $backendPlugin->save($field->offsetGet($delta)->get('value')->getValue());
$items[$delta]['geo_type'] = $field->offsetGet($delta)->get('geo_type')->getValue();
$items[$delta]['lat'] = $field->offsetGet($delta)->get('lat')->getValue();
$items[$delta]['lon'] = $field->offsetGet($delta)->get('lon')->getValue();
$items[$delta]['left'] = $field->offsetGet($delta)->get('left')->getValue();
$items[$delta]['top'] = $field->offsetGet($delta)->get('top')->getValue();
$items[$delta]['right'] = $field->offsetGet($delta)->get('right')->getValue();
$items[$delta]['bottom'] = $field->offsetGet($delta)->get('bottom')->getValue();
$items[$delta]['geohash'] = $field->offsetGet($delta)->get('geohash')->getValue();
}
}