forked from GetDKAN/leaflet_draw_widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet_widget.module
312 lines (274 loc) · 9.28 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
<?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' => '',
'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' => 'Default map settings',
);
$form['map']['base_url'] = array(
'#type' => 'select',
'#title' => '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' => 'Latitude',
'#default_value' => $settings['map']['center']['lat'],
'#required' => TRUE,
);
$form['map']['center']['lng'] = array(
'#type' => 'textfield', // 'hidden',
'#title' => 'Longtitude',
'#default_value' => $settings['map']['center']['lng'],
'#required' => TRUE,
);
$form['map']['auto_center'] = array(
'#type' => 'checkbox',
'#title' => 'Automatically center map on existing features',
'#description' => "This option overrides the widget's default center.",
'#default_value' => $settings['map']['auto_center'],
);
$form['map']['zoom'] = array(
'#type' => 'textfield', // 'hidden',
'#title' => 'Default zoom level',
'#default_value' => $settings['map']['zoom'],
'#required' => TRUE,
);
$form['toggle'] = array(
'#type' => 'checkbox',
'#title' => 'Allow users option to input values directly.',
'#description' => "When checked this option allows users to add to input form directly instead of using the map.",
'#default_value' => $settings['toggle'],
);
$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' => 'Tools to include on the map.',
'#description' => "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'];
$widget = geofield_get_base_element($element, $items, $delta);
$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');
// 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)),
);
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;
$geojson = json_decode($element['wkt']['#value']);
$results = array();
if ($geojson) {
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_core');
$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/dist/leaflet.css" => array(),
),
'js' => array(
"$leaflet_core/dist/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'));
}