forked from GetDKAN/leaflet_draw_widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet_widget.module
421 lines (369 loc) · 13.5 KB
/
leaflet_widget.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/**
* Implements hook_field_widget_info().
*/
function leaflet_widget_field_widget_info() {
$widgets = array();
$base_layers = leaflet_widget_base_layers();
$urls = array_keys($base_layers);
$widgets['leaflet_widget_widget'] = array(
'label' => 'Leaflet.widget',
'description' => 'Provides a map powered by Leaflet and Leaflet.widget.',
'field types' => array('geofield'),
'settings' => array(
'map' => array(
'base_url' => array_shift($urls),
'center' => array(
'lat' => 0.0,
'lng' => 0.0
),
'auto_center' => TRUE,
'zoom' => 10,
),
'toggle' => '',
'geographic_areas' => FALSE,
'draw' => array(
'tools' => array(
'polyline' => TRUE,
'polygon' => TRUE,
'rectangle' => TRUE,
'circle' => TRUE,
'marker' => TRUE,
),
),
),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
'default value' => FIELD_BEHAVIOR_NONE,
),
);
return $widgets;
}
/**
* Implements hook_field_widget_settings_form().
*/
function leaflet_widget_field_widget_settings_form($field, $instance) {
$form = array();
$settings = $instance['widget']['settings'];
switch ($instance['widget']['type']) {
case 'leaflet_widget_widget':
$form['map'] = array(
'#type' => 'fieldset',
'#title' => t('Default map settings'),
);
$form['map']['base_url'] = array(
'#type' => 'select',
'#title' => t('Leaflet.draw widget settings'),
'#default_value' => $settings['map']['base_url'],
'#options' => leaflet_widget_base_layers(),
);
$form['map']['center'] = array('#type' => 'fieldset', '#collapsed' => TRUE, '#collapsible' => TRUE, '#title' => 'Default map center');
$form['map']['center']['lat'] = array(
'#type' => 'textfield', // 'hidden',
'#title' => t('Latitude'),
'#default_value' => $settings['map']['center']['lat'],
'#required' => TRUE,
);
$form['map']['center']['lng'] = array(
'#type' => 'textfield', // 'hidden',
'#title' => t('Longtitude'),
'#default_value' => $settings['map']['center']['lng'],
'#required' => TRUE,
);
$form['map']['auto_center'] = array(
'#type' => 'checkbox',
'#title' => t('Automatically center map on existing features'),
'#description' => t("This option overrides the widget's default center."),
'#default_value' => $settings['map']['auto_center'],
);
$form['map']['zoom'] = array(
'#type' => 'textfield', // 'hidden',
'#title' => t('Default zoom level'),
'#default_value' => $settings['map']['zoom'],
'#required' => TRUE,
);
$form['toggle'] = array(
'#type' => 'checkbox',
'#title' => t('Allow users option to input values directly.'),
'#description' => t('When checked this option allows users to add to input form directly instead of using the map (geojson or point).'),
'#default_value' => $settings['toggle'],
);
$form['geographic_areas'] = array(
'#type' => 'checkbox',
'#title' => t('Allow users use a predefined geographic areas'),
'#description' => t('Show a select list on the top with predefined areas'),
'#default_value' => $settings['geographic_areas'],
);
$form['draw'] = array(
'#type' => 'fieldset',
'#title' => 'Default Leaflet.draw settings',
);
$options = array(
'polyline' => t('Polyline'),
'polygon' => t('Polygon'),
'rectangle' => t('Rectangle'),
'circle' => t('Circle'),
'marker' => t('Marker'),
);
$form['draw']['tools'] = array(
'#type' => 'checkboxes',
'#title' => t('Tools to include on the map.'),
'#description' => t('Decide which tools you want to add. Defaults to all.'),
'#default_value' => $settings['draw']['tools'],
'#options' => $options,
);
break;
}
return $form;
}
/**
* Implements hook_field_widget_form().
*/
function leaflet_widget_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$settings = $instance['widget']['settings'];
// @see
// https://github.com/chrsnlsn/leaflet-widget-point/blob/master/leaflet_widget_point.module#L94
//$widget = geofield_get_base_element($element, $items, $delta);
// this function is gone in geofield 2.0 so reproducing what it returned here
// @TODO: Change this to be generic, so that we don't have poor DX to input as WKT.
$element['wkt'] = array(
'#type' => 'hidden',
'#attributes' => array('class' => array('geofield_wkt')),
'#default_value' => isset($items[$delta]['wkt']) ? $items[$delta]['wkt'] : NULL,
);
$element['input_format'] = array(
'#type' => 'value',
'#attributes' => array('class' => array('geofield_input_format')),
'#value' => 'wkt',
);
$element['geo_type'] = array(
'#type' => 'hidden',
'#attributes' => array('class' => array('geofield_geo_type')),
'#default_value' => isset($items[$delta]['geo_type']) ? $items[$delta]['geo_type'] : NULL,
);
$element['lat'] = array(
'#type' => 'hidden',
'#attributes' => array('class' => array('geofield_lat')),
'#default_value' => isset($items[$delta]['lat']) ? $items[$delta]['lat'] : NULL,
);
$element['lon'] = array(
'#type' => 'hidden',
'#attributes' => array('class' => array('geofield_lon')),
'#default_value' => isset($items[$delta]['lon']) ? $items[$delta]['lon'] : NULL,
);
$element['left'] = array(
'#type' => 'hidden',
'#attributes' => array('class' => array('geofield_left')),
'#default_value' => isset($items[$delta]['left']) ? $items[$delta]['left'] : NULL,
);
$element['right'] = array(
'#type' => 'hidden',
'#attributes' => array('class' => array('geofield_right')),
'#default_value' => isset($items[$delta]['right']) ? $items[$delta]['right'] : NULL,
);
$element['bottom'] = array(
'#type' => 'hidden',
'#attributes' => array('class' => array('geofield_bottom')),
'#default_value' => isset($items[$delta]['bottom']) ? $items[$delta]['bottom'] : NULL,
);
$element['top'] = array(
'#type' => 'hidden',
'#attributes' => array('class' => array('geofield_top')),
'#default_value' => isset($items[$delta]['top']) ? $items[$delta]['top'] : NULL,
);
$element['description'] = array(
'#markup' => (!empty($element['#description'])) ? '<div class="description">' . $element['#description'] . '</div>' : '',
);
// Master column is used by element-validate to decide which set of columns it should use to compute all other values.
// By default, wkt is the master-column, all we compute all other values from it. For other widget (such as lat/lon) this will be different
$element['master_column'] = array(
'#type' => 'hidden',
'#value' => 'wkt',
);
// This validate function computes all other columns from the master field
$element['#element_validate'] = array('geofield_element_validate');
/* END FUNCTION geofield_get_base_element from geofield 1.0 */
$widget['#type'] = 'fieldset';
// Geofield adds its own description even though #description is available.
unset($widget['description']);
switch ($instance['widget']['type']) {
case 'leaflet_widget_widget':
$id = 'leaflet-widget_' . str_replace('_', '-', $instance['field_name']);
$class = 'leaflet-widget';
$style = 'height: 300px;';
$settings['map']['widget'] = array();
$settings['map']['widget']['attach'] = "$id-input";
$settings['map']['widget']['multiple'] = FALSE;
$settings['map']['widget']['autoCenter'] = $settings['map']['auto_center'];
if ($field['cardinality'] != 1) {
$settings['map']['widget']['multiple'] = TRUE;
// Leaflet.widget treats multiple == true && !cardinality as 'unlimited'.
$settings['map']['widget']['cardinality'] = $field['cardinality'] > 0 ? $field['cardinality'] : 0;
}
// Provide container markup for map form element.
$container = "<div id=\"$id\" class=\"$class\" style=\"$style\"></div>";
$widget['leaflet_widget'] = array('#markup' => $container);
// Overriding Geofield's validation handler.
$widget['#element_validate'] = array('leaflet_widget_widget_validate');
// Tell Geofield to expect GeoJSON instead of WKT.
$widget['input_format']['#value'] = 'geojson';
// Prepare existing field values to be rendered in widget.
$collection = leaflet_widget_widget_prepare_items($items);
// Set default value incase form is submitted without javascript enabled.
$widget['wkt']['#default_value'] = drupal_json_encode($collection);
$widget['wkt']['#attributes']['id'] = $settings['map']['widget']['attach'];
// Include javascript.
$widget['#attached']['library'][] = array('leaflet_widget', 'draw');
//Check if we need to add geographic areas
if (isset($settings['geographic_areas']) && $settings['geographic_areas']) {
//TODO: Add documentation about this hook.
$areas = module_invoke_all('leaflet_widget_geographic_areas');
$settings['areas'] = $areas;
}
// Settings and geo-data are passed to the widget keyed by field id.
$widget['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('leaflet_widget_widget' => array($id => $settings)),
);
// Add JSON utility js.
$widget['#attached']['js'][] = array(
'type' => 'file',
'data' => drupal_get_path('module', 'leaflet_widget') . '/js/json2.min.js',
);
break;
}
return $widget;
}
function leaflet_widget_widget_prepare_items($items) {
$features = array();
foreach ($items as $item) {
if (isset($item['wkt'])) {
$features[] = leaflet_widget_geojson_feature($item['wkt']);
}
}
return leaflet_widget_geojson_feature_collection($features);
}
function leaflet_widget_widget_validate($element, &$form_state) {
$geophp = geophp_load();
if (!$geophp) return FALSE;
//Validate json type is valid.
$geojson = json_decode($element['wkt']['#value']);
$types = array('FeatureCollection', 'GeometryCollection', 'Feature', 'Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon');
$results = array();
if ($geojson) {
//Validate type
if (!in_array($geojson->type, $types)) {
form_set_error($element['wkt']['#name'], 'Please enter valid GeoJSON');
return FALSE;
}
//validate structure
try {
$geometry = geoPHP::load($element['wkt']['#value'], 'json');
$geometry->out('wkt');
}
catch (Exception $e) {
form_set_error($element['wkt']['#name'], 'Please enter valid GeoJSON');
return FALSE;
}
if ($geojson->type === 'FeatureCollection') {
foreach ($geojson->features as $feature) {
$results[] = array_merge($results, leaflet_widget_process_geojson($feature));
}
}
else {
$results = array(leaflet_widget_process_geojson($geojson));
}
}
else {
$results = array();
}
form_set_value($element, $results, $form_state);
}
function leaflet_widget_process_geojson($geojson) {
$geom = geoPHP::load($geojson, 'json');
$type = $geom->geometryType();
$result = array('wkt' => $geom->out('wkt'));
geofield_compute_values($result, 'wkt');
return $result;
}
/**
* Implements hook_library().
*/
function leaflet_widget_library() {
$path = drupal_get_path('module', 'leaflet_widget');
$leaflet_draw = libraries_get_path('Leaflet.draw');
$leaflet_core = libraries_get_path('leaflet');
$libraries = array();
$libraries['draw'] = array(
'title' => 'Widget behavior',
'version' => '1.x',
'js' => array(
"$path/js/draw.js" => array(),
),
'dependencies' => array(
array('leaflet_widget', 'leaflet_draw'),
),
);
$libraries['leaflet_draw'] = array(
'title' => 'Leaflet.draw',
'version' => '0.2.4',
'css' => array(
"$leaflet_draw/dist/leaflet.draw.css" => array(),
),
'js' => array(
"$leaflet_draw/dist/leaflet.draw.js" => array(),
),
'dependencies' => array(
array('leaflet_widget', 'leaflet_core'),
),
);
$libraries['leaflet_core'] = array(
'title' => 'Leaflet (Leaflet Widget)',
'version' => '0.7.3',
'css' => array(
"$leaflet_core/leaflet.css" => array(),
),
'js' => array(
"$leaflet_core/leaflet.js" => array(),
),
);
return $libraries;
}
function leaflet_widget_geojson_feature_collection($features, $properties = array()) {
if (!is_array($features)) {
$features = array($features);
}
return array(
'type' => 'FeatureCollection',
'features' => $features,
);
}
function leaflet_widget_geojson_feature($wkt, $properties = array()) {
$geophp = geophp_load();
if (!$geophp) return FALSE;
$geometry = geoPHP::load($wkt, 'wkt');
return array(
'type' => 'Feature',
'geometry' => json_decode($geometry->out('json')),
'properties' => $properties,
);
}
/**
* Implements hook_leaflet_widget_base_layers().
*/
function leaflet_widget_leaflet_widget_base_layers() {
return array(
'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png' => 'OSM Mapnik',
);
}
function leaflet_widget_base_layers() {
$options = array();
if (module_exists('leaflet')) {
foreach (leaflet_map_get_info() as $id => $map) {
foreach ($map['layers'] as $layer_id => $layer) {
$options[$layer['urlTemplate']] = "$id - $layer_id";
}
}
}
return array_merge($options, module_invoke_all('leaflet_widget_base_layers'));
}