This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
leaflet.module
274 lines (253 loc) · 7.63 KB
/
leaflet.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
<?php
/**
* Implements hook_theme().
*/
function leaflet_theme($existing, $type, $theme, $path) {
return array(
'leaflet_map' => array(
'variables' => array(
'map_id' => NULL,
'height' => '400px',
'map' => array(),
),
// When theme('leaflet_map'...) is called, the system will look for
// /leaflet/templates/leaflet_map.html.twig.
'template' => 'leaflet_map',
),
);
}
/**
* Load all Leaflet required client files and return markup for a map.
*
* @param array $map
* @param array $features
* @param string $height
*
* @return array render array
*/
function leaflet_render_map($map, $features = array(), $height = '400px') {
$map_id = Drupal\Component\Utility\Html::getUniqueId('leaflet_map');
// Allow map definitions to provide a default icon.
if (isset($map['icon']['iconUrl'])) {
foreach ($features as &$feature) {
if (!isset($feature['icon'])) {
$feature['icon'] = $map['icon'];
}
}
}
$settings[$map_id] = array(
'mapId' => $map_id,
'map' => $map,
// JS only works with arrays, make sure we have one with numeric keys.
'features' => array_values($features),
);
return array(
'#theme' => 'leaflet_map',
'#map_id' => $map_id,
'#height' => $height,
'#map' => $map,
'#attached' => array(
'library' => array('leaflet/leaflet-drupal'),
'drupalSettings' => array(
'leaflet' => $settings
),
),
);
}
/**
* Get all available Leaflet map definitions.
*
* @param string $map
*/
function leaflet_map_get_info($map = NULL) {
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['leaflet_map_info'] = &drupal_static(__FUNCTION__);
}
$map_info = &$drupal_static_fast['leaflet_map_info'];
if (empty($map_info)) {
if ($cached = Drupal::cache()->get('leaflet_map_info')) {
$map_info = $cached->data;
}
else {
$map_info = Drupal::moduleHandler()->invokeAll('leaflet_map_info');
// Let other modules alter the map info.
Drupal::moduleHandler()->alter('leaflet_map_info', $map_info);
Drupal::cache()->set('leaflet_map_info', $map_info);
}
}
if (empty($map)) {
return $map_info;
}
elseif (isset($map_info[$map])) {
return $map_info[$map];
}
}
/**
* Implements hook_leaflet_map_info() to return a default map.
*
* @return array
*/
function leaflet_leaflet_map_info() {
return array(
'OSM Mapnik' =>
array(
'label' => 'OSM Mapnik',
'description' => t('Leaflet default map.'),
'settings' => array(
// 'zoom' => 18,
'minZoom' => 0,
'maxZoom' => 18,
'dragging' => TRUE,
'touchZoom' => TRUE,
'scrollWheelZoom' => TRUE,
'doubleClickZoom' => TRUE,
'zoomControl' => TRUE,
'attributionControl' => TRUE,
'trackResize' => TRUE,
'fadeAnimation' => TRUE,
'zoomAnimation' => TRUE,
'closePopupOnClick' => TRUE,
),
'layers' => array(
'earth' => array(
'urlTemplate' => 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
'options' => array(
'attribution' => 'OSM Mapnik'
)
),
),
),
);
}
/**
* Convert a geofield into an array of map points.
*
* The map points can then be fed into leaflet_render_map().
*
* @param mixed $items
* A single vlaue or array of geo values, each as a string in any of the
* supported formats or as an array of $item elements, each with a
* $item['wkt'] field.
*
* @return array
*/
function leaflet_process_geofield($items = array()) {
geophp_load();
if (!is_array($items)) {
$items = array($items);
}
$data = array();
foreach ($items as $item) {
// Auto-detect and parse the format (e.g. WKT, JSON etc)
if (!($geom = geoPHP::load(isset($item['wkt']) ? $item['wkt'] : $item))) {
continue;
}
$datum = array('type' => strtolower($geom->geometryType()));
switch ($datum['type']) {
case 'point':
$datum += array(
'lat' => $geom->getY(),
'lon' => $geom->getX(),
);
break;
case 'linestring':
$components = $geom->getComponents();
foreach ($components as $component) {
$datum['points'][] = array(
'lat' => $component->getY(),
'lon' => $component->getX(),
);
}
break;
case 'polygon':
$tmp = $geom->getComponents();
$components = $tmp[0]->getComponents();
foreach ($components as $component) {
$datum['points'][] = array(
'lat' => $component->getY(),
'lon' => $component->getX(),
);
}
break;
case 'multipolygon':
case 'multipolyline':
case 'multilinestring':
if ($datum['type'] == 'multilinestring') {
$datum['type'] = 'multipolyline';
}
if ($datum['type'] == 'multipolygon') {
$tmp = $geom->getComponents();
$components = $tmp[0]->getComponents();
}
else {
$components = $geom->getComponents();
}
foreach ($components as $key => $component) {
$subcomponents = $component->getComponents();
foreach ($subcomponents as $subcomponent) {
$datum['component'][$key]['points'][] = array(
'lat' => $subcomponent->getY(),
'lon' => $subcomponent->getX(),
);
}
unset($subcomponent);
}
break;
}
$data[] = $datum;
}
return $data;
}
/**
* Implements hook_requirements().
*/
function leaflet_requirements($phase) {
$requirements = array();
if ($phase != 'runtime') {
return $requirements;
}
$library = Drupal::service('library.discovery')
->getLibraryByName('leaflet', 'leaflet');
$requirements['leaflet'] = array(
'title' => Drupal::translation()->translate('Leaflet library')
);
$maps_info = Drupal::translation()->translate('@maps available.', array(
'@maps' => Drupal::translation()
->formatPlural(count(leaflet_map_get_info()), 'One map', '@count maps')
)
);
// Check the defined type of the leaflet.js file; if it is external then
// assume that we are using a CDN version.
if ($library['js'][0]['type'] == 'external') {
$requirements['leaflet']['value'] = Drupal::translation()
->translate('Using CDN version @version.', array(
'@version' => $library['version']
)) . ' ' . $maps_info;
}
// If leaflet.js is defined to be a local file, check that it exists and show
// an error if it does not exist.
else {
if (file_exists($library['js'][0]['data'])) {
$requirements['leaflet']['value'] = Drupal::translation()
->translate('Leaflet @version library installed at @path.', array(
'@version' => $library['version'],
'@path' => $library['js'][0]['data'],
)) . ' ' . $maps_info;
$requirements['leaflet']['severity'] = REQUIREMENT_OK;
}
else {
$requirements['leaflet']['value'] = Drupal::translation()
->translate('Leaflet @version library not found at @path. Please !download it to @directory, or undo your changes to the libraries registry to use the CDN version.',
array(
'@version' => $library['version'],
'@path' => $library['js'][0]['data'],
'@directory' => dirname($library['js'][0]['data']),
'!download' => Drupal::l('download', Drupal\Core\Url::fromUri($library['remote'])),
)
);
$requirements['leaflet']['severity'] = REQUIREMENT_ERROR;
}
}
return $requirements;
}