-
Notifications
You must be signed in to change notification settings - Fork 0
/
geofield_gmap.module
executable file
·276 lines (242 loc) · 9.24 KB
/
geofield_gmap.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* @file
* Google Map widget for geofield. For now allow to use a
* google map v3 map to input a location. A quick text
* input + geocode allow you to specify an address.
* You can drag and drop the marker to narrow the position.
*/
/**
* Implements hook_menu().
*/
function geofield_gmap_menu() {
$items['admin/config/content/geofield_gmap'] = array(
'title' => 'Geofield Gmap',
'description' => 'Settings for Geofield Gmap.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('geofield_gmap_admin_settings'),
'access arguments' => array('administer geofield_gmap'),
'file' => 'geofield_gmap.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_field_widget_info().
*/
function geofield_gmap_field_widget_info() {
return array(
'geofield_gmap' => array(
'label' => t('Google Map'),
'field types' => array('geofield'),
'settings' => array(
'map_type' => 'ROADMAP',
'confirm_center_marker' => FALSE,
'click_to_place_marker' => FALSE,
),
'module' => 'geofield_gmap',
),
);
}
/**
* Implements hook_field_widget_form().
*/
function geofield_gmap_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
$element = $base;
$widget = $instance['widget'];
$settings = $widget['settings'];
if ($widget['type'] == 'geofield_gmap') {
if (!isset($settings['map_type'])) {
$settings['map_type'] = 'ROADMAP';
}
if (!isset($settings['confirm_center_marker'])) {
$settings['confirm_center_marker'] = FALSE;
}
if (!isset($settings['click_to_place_marker'])) {
$settings['click_to_place_marker'] = FALSE;
}
$latlon_value = array(
'lat' => '',
'lon' => '',
);
if (isset($items[$delta]['lat'])) {
$latlon_value['lat'] = floatval($items[$delta]['lat']);
}
if (isset($items[$delta]['lon'])) {
$latlon_value['lon'] = floatval($items[$delta]['lon']);
}
// Try to fetch data from form state if lon/lat is empty.
if (isset($form_state['input'][$field['field_name']][$langcode][$delta]['geom'])) {
$latlon_value = $form_state['input'][$field['field_name']][$langcode][$delta]['geom'];
}
// If we still have empty lat and long, use the field's default values.
if (!$latlon_value['lat'] || !$latlon_value['lon']) {
if ($default_field_value = field_get_default_value($base['#entity_type'], $base['#bundle'], $field, $instance, $langcode)) {
if ($default_geom_value = $default_field_value[0]['geom']) {
$latlon_value = $default_geom_value;
}
}
}
$element['geom'] = array(
'#type' => 'geofield_latlon',
'#title' => check_plain($instance['label']),
'#description' => field_filter_xss($instance['description']),
'#default_value' => $latlon_value,
'#required' => $instance['required'],
'#geolocation' => (!empty($settings['html5_geolocation'])) ? $settings['html5_geolocation'] : FALSE,
'#geofield_gmap_geolocation_override' => (!empty($settings['html5_geolocation'])) ? $settings['html5_geolocation'] : FALSE,
'#zoom_level' => (!empty($settings['zoom_level'])) ? $settings['zoom_level'] : 1,
);
// $element['input_format'] is not a db field, but we use it determine how
// to parse/calculate values in our widget.
$element['input_format'] = array(
'#type' => 'value',
'#attributes' => array('class' => array('geofield_input_format')),
'#value' => GEOFIELD_INPUT_LAT_LON,
);
$element['master_column']['#value'] = 'latlon';
$element['#gmap_id'] = geofield_gmap_get_id($element);
$element['#gmap_map_type'] = $settings['map_type'];
$element['#gmap_confirm_center_marker'] = $settings['confirm_center_marker'];
$element['#gmap_click_to_place_marker'] = $settings['click_to_place_marker'];
$element['#after_build'] = array('geofield_gmap_widget_gmap_afterbuild');
}
return $element;
}
/**
* Implements hook_config_info().
*/
function geofield_gmap_config_info() {
$prefixes['geofield_gmap.settings'] = array(
'label' => t('Geofield Gmap settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Callback for afterbuild for widget for js addition.
*/
function geofield_gmap_widget_gmap_afterbuild($element, $form_state) {
// Retrieve the Google API Key, if there is one.
$api_key = config_get('geofield_gmap.settings', 'geofield_gmap_google_api_key');
$api_key_query_param = !empty($api_key) ? '?key=' . $api_key : '';
// Attach GMAP API and autocomplete library.
$element['#attached']['library'][] = array('system', 'ui.autocomplete');
$element['#attached']['js'][] = array(
'data' => '//maps.googleapis.com/maps/api/js' . $api_key_query_param,
'type' => 'external',
'preprocess' => FALSE,
);
$id = $element['#gmap_id'];
$gmapid = 'gmap-' . $id;
$element['geom']['lat']['#prefix'] = '<div class="form-item">';
$element['geom']['lat']['#prefix'] .= '<label>' . t("Geocode address") . '</label><input size="64" id="search-' . $id . '" class="form-text form-autocomplete geofield-gmap-search" type="text"/>';
$element['geom']['lat']['#prefix'] .= '<div id="' . $gmapid . '" class="geofield-gmap-cnt"></div>';
$element['geom']['lat']['#prefix'] .= '<div class="geofield-gmap-buttons">';
$element['geom']['lat']['#prefix'] .= '<label>' . t("Drag the marker to narrow the location") . '</label>';
$element['geom']['lat']['#prefix'] .= '<button class="geofield-gmap-center" onclick="geofield_gmap_center(\'' . $gmapid . '\'); return false;">' . t('Find marker') . '</button>';
$element['geom']['lat']['#prefix'] .= '<button class="geofield-gmap-marker" onclick="geofield_gmap_marker(\'' . $gmapid . '\'); return false;">' . t('Place marker here') . '</button>';
$element['geom']['lat']['#prefix'] .= '</div>';
$element['geom']['lat']['#prefix'] .= '</div>';
// Attach JS settings.
$element['#attached']['js'][] = array(
'data' => array('geofield_gmap' => array(
$gmapid => array(
'lat' => floatval($element['geom']['lat']['#default_value']),
'lng' => floatval($element['geom']['lon']['#default_value']),
'zoom' => intval($element['geom']['#zoom_level']),
'latid' => $element['geom']['lat']['#id'],
'lngid' => $element['geom']['lon']['#id'],
'searchid' => 'search-' . $id,
'mapid' => $gmapid,
'widget' => TRUE,
'map_type' => $element['#gmap_map_type'],
'confirm_center_marker' => (bool) $element['#gmap_confirm_center_marker'],
'click_to_place_marker' => (bool) $element['#gmap_click_to_place_marker'],
),
)),
'type' => 'setting',
);
if (isset($element['geom']['#geofield_gmap_geolocation_override']) && $element['geom']['#geofield_gmap_geolocation_override']) {
// Add override behavior.
$element['#attached']['js'][] = backdrop_get_path('module', 'geofield_gmap') . '/geofield_gmap_geolocation_override.js';
}
return $element;
}
/**
* Helper function: Generate a unique id for the map element.
*/
function geofield_gmap_get_id($element, $type = 'id') {
$id = array_merge($element['#field_parents'], array(
$element['#field_name'],
$element['#language'],
$element['#delta'],
));
return strtr(implode('-', $id), '_', '-');
}
/**
* Implements hook_field_widget_settings_form().
*/
function geofield_gmap_field_widget_settings_form($field, $instance) {
$widget = $instance['widget'];
$settings = $widget['settings'];
if (!isset($settings['map_type'])) {
$settings['map_type'] = 'ROADMAP';
}
if (!isset($settings['confirm_center_marker'])) {
$settings['confirm_center_marker'] = FALSE;
}
if (!isset($settings['click_to_place_marker'])) {
$settings['click_to_place_marker'] = FALSE;
}
if (!isset($settings['html5_geolocation'])) {
$settings['html5_geolocation'] = FALSE;
}
$form = array();
if ($widget['type'] == 'geofield_gmap') {
$form['map_type'] = array(
'#type' => 'select',
'#title' => t('Map type'),
'#default_value' => $settings['map_type'],
'#options' => array(
'ROADMAP' => t('Roadmap'),
'SATELLITE' => t('Satellite'),
'HYBRID' => t('Hybrid'),
'TERRAIN' => t('Terrain'),
),
);
$form['zoom_level'] = array(
'#type' => 'textfield',
'#title' => t('Default zoom level'),
'#default_value' => (!empty($settings['zoom_level'])) ? $settings['zoom_level'] : 1,
'#required' => FALSE,
);
$form['confirm_center_marker'] = array(
'#type' => 'checkbox',
'#title' => t('Confirm center marker'),
'#default_value' => $settings['confirm_center_marker'],
);
$form['click_to_place_marker'] = array(
'#type' => 'checkbox',
'#title' => t('Click to place marker'),
'#default_value' => $settings['click_to_place_marker'],
);
$form['html5_geolocation'] = array(
'#type' => 'checkbox',
'#title' => t('HTML5 geolocation'),
'#default_value' => $settings['html5_geolocation'],
);
}
return $form;
}
/**
* Implements hook_permission().
*/
function geofield_gmap_permission() {
return array(
'administer geofield_gmap' => array(
'title' => t('Administer Geofield Gmap'),
'description' => t('Administer Geofield Gmap.'),
),
);
}