leaflet.module 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * @file
  4. * Provide Leaflet API integration and field formatters.
  5. */
  6. module_load_include('inc', 'leaflet', 'leaflet.formatters');
  7. /**
  8. * Implements hook_theme().
  9. */
  10. function leaflet_theme($existing, $type, $theme, $path) {
  11. return array(
  12. 'leaflet_map' => array(
  13. 'arguments' => array('map_id' => NULL, 'height' => '400px'),
  14. 'template' => 'leaflet_map',
  15. ),
  16. );
  17. }
  18. /**
  19. * Implements hook_libraries_info().
  20. */
  21. function leaflet_libraries_info() {
  22. $libraries['leaflet'] = array(
  23. // Only used in administrative UI of Libraries API.
  24. 'name' => 'Leaflet JavaScript Library',
  25. 'vendor url' => 'http://leafletjs.com/',
  26. 'download url' => 'http://cdn.leafletjs.com/downloads/leaflet-0.7.5.zip',
  27. 'version arguments' => array(
  28. 'file' => 'leaflet.js',
  29. // Handle things like version:"1.0.0-beta.2"
  30. 'pattern' => '/version[=: ]*[\'"]([\d+\.]+[\-a-z\.\d]*)[\'"]/',
  31. ),
  32. 'files' => array(
  33. 'js' => array(
  34. // This setting is needed in order to properly render market.
  35. 'leaflet_root_url' => array(
  36. 'type' => 'inline',
  37. 'data' => 'L_ROOT_URL = "' . base_path() . libraries_get_path('leaflet') . '/";',
  38. 'group' => JS_LIBRARY,
  39. ),
  40. 'leaflet.js' => array(
  41. 'type' => 'file',
  42. 'group' => JS_LIBRARY,
  43. ),
  44. // For AdvAgg module. See [#2294639] This runs after leaflet.js.
  45. 'leaflet_imagepath' => array(
  46. 'type' => 'inline',
  47. 'data' => 'L.Icon.Default.imagePath = "' . base_path() . libraries_get_path('leaflet') . '/images/";',
  48. ),
  49. ),
  50. 'css' => array(
  51. 'leaflet.css' => array(
  52. 'type' => 'file',
  53. 'media' => 'all',
  54. ),
  55. 'leaflet.ie.css' => array(
  56. 'browsers' => array(
  57. 'IE' => 'lte IE 8',
  58. '!IE' => FALSE,
  59. ),
  60. ),
  61. ),
  62. ),
  63. 'integration files' => array(
  64. 'leaflet' => array(
  65. 'js' => array('leaflet.drupal.js'),
  66. ),
  67. ),
  68. );
  69. return $libraries;
  70. }
  71. /**
  72. * Attach Leaflet-required client files and return renderable array for a map.
  73. *
  74. * @param array $map
  75. * Map definition as returned my leaflet_map_get_info();
  76. * @param array $features
  77. * Associative array of map features.
  78. * @param string $height
  79. * The height of the map.
  80. *
  81. * @return array
  82. * A renderable array.
  83. */
  84. function leaflet_build_map($map, $features = array(), $height = '400px') {
  85. $map_id = drupal_html_id('leaflet_map');
  86. $build = array(
  87. '#theme' => 'html_tag',
  88. '#tag' => 'div',
  89. '#value' => '',
  90. '#attributes' => array(
  91. 'id' => $map_id,
  92. 'style' => 'height: ' . $height,
  93. ),
  94. );
  95. // Allow map definitions to provide a default icon:
  96. if (isset($map['icon']['iconUrl'])) {
  97. foreach ($features as &$feature) {
  98. if (!isset($feature['icon'])) {
  99. $feature['icon'] = $map['icon'];
  100. }
  101. }
  102. }
  103. $settings = array(
  104. 'mapId' => $map_id,
  105. 'map' => $map,
  106. 'features' => $features,
  107. );
  108. drupal_alter('leaflet_map_prebuild', $settings);
  109. $build['#attached']['js'][] = array(
  110. 'data' => array('leaflet' => array($settings)),
  111. 'type' => 'setting',
  112. );
  113. $build['#attached']['css'][] = array(
  114. 'data' => drupal_get_path('module', 'leaflet') . '/leaflet_extras.css',
  115. );
  116. // Load the leaflet library, which includes integration files.
  117. // libraries_load('leaflet');
  118. // See [2460643]
  119. $build['#attached']['libraries_load'][] = array('leaflet');
  120. // Let other modules properly attach libraries as well [#2567387]
  121. drupal_alter('leaflet_build_map', $build);
  122. return $build;
  123. }
  124. /**
  125. * DEPRECATED. Use leaflet_build_map() instead.
  126. *
  127. * Load all Leaflet required client files and return markup for a map.
  128. *
  129. * @param array $map
  130. * Map definition as returned my leaflet_map_get_info();
  131. * @param array $features
  132. * Associative array of map features.
  133. * @param string $height
  134. * The height of the map.
  135. *
  136. * @return string
  137. * map markup
  138. */
  139. function leaflet_render_map($map, $features = array(), $height = '400px') {
  140. $build = leaflet_build_map($map, $features, $height);
  141. return render($build);
  142. }
  143. /**
  144. * Get all available Leaflet map definitions.
  145. *
  146. * @string $map
  147. * The name of the map defined in hook_leaflet_map_get_info().
  148. */
  149. function leaflet_map_get_info($map = NULL) {
  150. static $drupal_static_fast;
  151. if (!isset($drupal_static_fast)) {
  152. $drupal_static_fast['leaflet_map_info'] = &drupal_static(__FUNCTION__);
  153. }
  154. $map_info = &$drupal_static_fast['leaflet_map_info'];
  155. if (empty($map_info)) {
  156. if ($cache = cache_get("leaflet_map_info")) {
  157. $map_info = $cache->data;
  158. }
  159. else {
  160. $map_info = module_invoke_all('leaflet_map_info');
  161. // Let other modules alter the map info.
  162. drupal_alter('leaflet_map_info', $map_info);
  163. cache_set("leaflet_map_info", $map_info);
  164. }
  165. }
  166. if (empty($map)) {
  167. return $map_info;
  168. }
  169. elseif (isset($map_info[$map])) {
  170. return $map_info[$map];
  171. }
  172. }
  173. /**
  174. * Implements hook_leaflet_map_info().
  175. *
  176. * Return a default map for the module.
  177. */
  178. function leaflet_leaflet_map_info() {
  179. return array(
  180. 'OSM Mapnik' =>
  181. array(
  182. 'label' => 'OSM Mapnik',
  183. 'description' => t('Leaflet default map.'),
  184. // 'center' is used when map contains no features, or every time the map
  185. // is loaded if "force" is TRUE. Otherwise, the map will center itself
  186. // intelligently based on the features in the map.
  187. // RdB: bad things happen when 'center' is specified and Leaflet
  188. // MarkerCluster is used, see https://drupal.org/node/2144935
  189. // Also, a hard-coded center is not a great idea.
  190. //'center' => array(
  191. // 'lat' => 45.526513,
  192. // 'lon' => -122.674833,
  193. // 'force' => FALSE,
  194. //),
  195. 'settings' => array(
  196. // Setting "zoom" forces a zoom level on every map load.
  197. //'zoom' => 17,
  198. // The "zoomDefault" is only used when no features are present.
  199. 'zoomDefault' => 10,
  200. 'minZoom' => 0,
  201. 'maxZoom' => 18,
  202. 'dragging' => TRUE,
  203. 'touchZoom' => TRUE,
  204. 'scrollWheelZoom' => TRUE,
  205. 'doubleClickZoom' => TRUE,
  206. 'zoomControl' => TRUE,
  207. 'attributionControl' => TRUE,
  208. 'trackResize' => TRUE,
  209. 'fadeAnimation' => TRUE,
  210. 'zoomAnimation' => TRUE,
  211. 'closePopupOnClick' => TRUE,
  212. ),
  213. 'layers' => array(
  214. 'earth' => array(
  215. 'urlTemplate' => '//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
  216. 'options' => array(
  217. 'attribution' => 'OSM Mapnik',
  218. ),
  219. ),
  220. ),
  221. ),
  222. );
  223. }