openlayers.module 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * @file
  4. * Openlayers module.
  5. */
  6. /**
  7. * Implements hook_ctools_plugin_api().
  8. */
  9. function openlayers_ctools_plugin_api($module, $api) {
  10. return array(
  11. 'version' => 1,
  12. 'path' => drupal_get_path('module', 'openlayers') . '/includes',
  13. );
  14. }
  15. /**
  16. * Implements hook_permission().
  17. */
  18. function openlayers_permission() {
  19. return array(
  20. 'administer openlayers' => array(
  21. 'title' => t('Administer Openlayers'),
  22. 'description' => t('Configure Openlayers settings, styles, maps, and layers.'),
  23. ),
  24. );
  25. }
  26. /**
  27. * Implements hook_element_info().
  28. */
  29. function openlayers_element_info() {
  30. return array(
  31. 'openlayers' => array(
  32. '#input' => FALSE,
  33. '#tree' => TRUE,
  34. '#process' => array('openlayers_element_process_callback'),
  35. '#pre_render' => array('openlayers_element_prerender_callback'),
  36. '#theme_wrappers' => array('form_element'),
  37. ),
  38. );
  39. }
  40. /**
  41. * Process callback for the openlayers form element.
  42. *
  43. * Renders the map and returns the markup in an renderable array.
  44. */
  45. function openlayers_element_process_callback($element, $form_state, $complete_form) {
  46. /* @var \Drupal\openlayers\Types\MapInterface $map */
  47. if (($map = \Drupal\openlayers\Openlayers::load('Map', $element['#map'])) == TRUE) {
  48. $element['map'] = $map->build();
  49. }
  50. return $element;
  51. }
  52. /**
  53. * Pre render callback for the openlayers form element.
  54. */
  55. function openlayers_element_prerender_callback($element) {
  56. if (empty($element['map'])) {
  57. /* @var \Drupal\openlayers\Types\Map $map */
  58. if (($map = \Drupal\openlayers\Openlayers::load('Map', $element['#map'])) == TRUE) {
  59. $element['map'] = $map->build();
  60. }
  61. }
  62. return $element;
  63. }
  64. /**
  65. * Implements hook_theme().
  66. */
  67. function openlayers_theme($existing, $type, $theme, $path) {
  68. return array(
  69. 'openlayers' => array(
  70. 'path' => $path . '/theme',
  71. 'template' => 'openlayers',
  72. 'render element' => 'openlayers',
  73. 'file' => 'openlayers.theme.inc',
  74. ),
  75. );
  76. }
  77. /**
  78. * Implements hook_i18n_string_info().
  79. */
  80. function openlayers_i18n_string_info() {
  81. $groups['openlayers'] = array(
  82. 'title' => t('Openlayers'),
  83. 'format' => TRUE,
  84. );
  85. return $groups;
  86. }
  87. /**
  88. * Translate a string using i18n_string, if available.
  89. *
  90. * @param string $name
  91. * Textgroup and context glued with ':'.
  92. * @param string $default
  93. * String in default language. Default language may or may not be English.
  94. * @param array $options
  95. * An associative array of additional options, with the following keys:
  96. * - langcode: the language code to translate to a language other than what is
  97. * used to display the page; defaults to the current language
  98. * - filter: filtering callback to apply to the translated string only
  99. * - format: input format to apply to the translated string only
  100. * - callback: callback to apply to the result (both to the translated or
  101. * untranslated string)
  102. * - update: whether to update source table; defaults to FALSE
  103. * - translate: whether to return a translation; defaults to TRUE.
  104. *
  105. * @return string
  106. * The translated string if i18n_string is available, the original otherwise.
  107. *
  108. * @see i18n_string()
  109. */
  110. function openlayers_i18n_string($name, $default, $options = array()) {
  111. if (module_exists('i18n_string')) {
  112. $result = i18n_string($name, $default, $options);
  113. }
  114. else {
  115. $result = $default;
  116. $options += array(
  117. 'format' => NULL,
  118. 'sanitize' => FALSE,
  119. );
  120. if ($options['sanitize']) {
  121. $result = check_markup($result, $options['format']);
  122. }
  123. }
  124. return $result;
  125. }
  126. /**
  127. * Wrapper to update / create translation source for user defined strings.
  128. *
  129. * Wrapper around i18n_string_update() that ensures the module is available.
  130. *
  131. * @param string|array $name
  132. * Array or string concatenated with ':' that contains textgroup and string
  133. * context.
  134. * @param string $string
  135. * Source string in default language. Default language may or may not be
  136. * English. Array of key => string to update multiple strings at once.
  137. * @param array $options
  138. * Array with additional options:
  139. * - 'format', String format if the string has text format
  140. * - 'messages', Whether to print out status messages.
  141. *
  142. * @return mixed
  143. * Same return as from i18n_string_update()
  144. *
  145. * @see i18n_string_update()
  146. */
  147. function openlayers_i18n_string_update($name, $string, $options = array()) {
  148. if (module_exists('i18n_string')) {
  149. return i18n_string_update($name, $string, $options);
  150. }
  151. }
  152. /**
  153. * Wrapper to remove source and translations for user defined string.
  154. *
  155. * Though for most strings the 'name' or 'string id' uniquely identifies that
  156. * string, there are some exceptions (like profile categories) for which we need
  157. * to use the source string itself as a search key.
  158. *
  159. * @param string $name
  160. * String name.
  161. * @param string $string
  162. * Optional source string (string in default language).
  163. * @param array $options
  164. * Array of string properties to remove.
  165. *
  166. * @return mixed
  167. * Same return as from i18n_string_remove()
  168. *
  169. * @see i18n_string_remove()
  170. */
  171. function openlayers_i18n_string_remove($name, $string = NULL, $options = array()) {
  172. if (module_exists('i18n_string')) {
  173. return i18n_string_remove($name, $string, $options);
  174. }
  175. }
  176. /**
  177. * Implements hook_i18n_string_refresh().
  178. */
  179. function openlayers_i18n_string_refresh() {
  180. foreach (\Drupal\openlayers\Openlayers::getPluginTypes() as $type) {
  181. foreach (\Drupal\openlayers\Openlayers::loadAllExportable($type) as $exportable) {
  182. $object = \Drupal\openlayers\Openlayers::load($type, $exportable);
  183. $object->i18nStringsRefresh();
  184. }
  185. }
  186. }
  187. /**
  188. * Implements hook_libraries_info().
  189. */
  190. function openlayers_libraries_info() {
  191. // During an upgrade from Openlayers 7.x-2.x branch, the new registry_autoload
  192. // dependency may not be enabled, which makes it impossible to use the new
  193. // namespaced classes. So provide a fallback variable instead.
  194. $js_css_group = 'openlayers';
  195. if (class_exists('\Drupal\openlayers\Config')) {
  196. $js_css_group = \Drupal\openlayers\Config::get('openlayers.js_css.group');
  197. }
  198. $info = system_get_info('module', 'openlayers');
  199. $version = $info['version'] ? $info['version'] : '7.x-3.x';
  200. $module_path = drupal_get_path('module', 'openlayers');
  201. $libraries['openlayers3_integration'] = array(
  202. 'name' => 'Openlayers 3 Drupal integration files',
  203. 'vendor url' => 'https://drupal.org/project/openlayers',
  204. 'download url' => 'https://drupal.org/project/openlayers',
  205. 'version' => $version,
  206. 'library path' => $module_path,
  207. 'files' => array(
  208. 'js' => array(
  209. $module_path . '/js/openlayers.js' => array(
  210. 'data' => $module_path . '/js/openlayers.js',
  211. 'type' => 'file',
  212. 'weight' => 8,
  213. 'group' => $js_css_group,
  214. ),
  215. $module_path . '/js/openlayers.pluginManager.js' => array(
  216. 'data' => $module_path . '/js/openlayers.pluginManager.js',
  217. 'type' => 'file',
  218. 'weight' => 12,
  219. 'group' => $js_css_group,
  220. ),
  221. $module_path . '/js/openlayers.behaviors.js' => array(
  222. 'data' => $module_path . '/js/openlayers.behaviors.js',
  223. 'type' => 'file',
  224. 'weight' => 14,
  225. 'group' => $js_css_group,
  226. ),
  227. ),
  228. 'css' => array(
  229. $module_path . '/css/openlayers.css' => array(
  230. 'data' => $module_path . '/css/openlayers.css',
  231. 'type' => 'file',
  232. 'weight' => 4,
  233. 'group' => $js_css_group,
  234. ),
  235. ),
  236. ),
  237. 'variants' => array(
  238. 'debug' => array(
  239. 'files' => array(
  240. 'js' => array(
  241. 'js/openlayers.js' => array(
  242. 'type' => 'file',
  243. 'weight' => 8,
  244. 'group' => $js_css_group,
  245. ),
  246. 'js/openlayers.pluginManager.js' => array(
  247. 'type' => 'file',
  248. 'weight' => 12,
  249. 'group' => $js_css_group,
  250. ),
  251. 'js/openlayers.behaviors.js' => array(
  252. 'type' => 'file',
  253. 'weight' => 14,
  254. 'group' => $js_css_group,
  255. ),
  256. 'js/openlayers.console.js' => array(
  257. 'type' => 'file',
  258. 'weight' => 16,
  259. 'group' => $js_css_group,
  260. ),
  261. 'js/openlayers.debug.js' => array(
  262. 'type' => 'file',
  263. 'weight' => 18,
  264. 'group' => $js_css_group,
  265. ),
  266. ),
  267. 'css' => array(
  268. 'css/openlayers.css' => array(
  269. 'type' => 'file',
  270. 'weight' => 4,
  271. 'group' => $js_css_group,
  272. ),
  273. ),
  274. ),
  275. ),
  276. ),
  277. );
  278. $libraries['openlayers3'] = array(
  279. 'name' => 'Openlayers 3',
  280. 'vendor url' => 'http://openlayers.org/',
  281. 'download url' => 'https://github.com/openlayers/ol3/releases/download/v3.11.2/v3.11.2.zip',
  282. 'version callback' => array('\Drupal\openlayers\Openlayers', 'getLibraryVersion'),
  283. 'version arguments' => array(),
  284. 'library path' => $module_path,
  285. // This is taken in account if the module libraries_cdn is enabled.
  286. 'cdn' => array(
  287. 'aliases' => array('ol3'),
  288. 'options' => array(
  289. 'weight' => 0,
  290. 'group' => $js_css_group,
  291. ),
  292. 'request' => array(
  293. // Maximum 10 seconds for the http requests to CDNs.
  294. 'timeout' => 10,
  295. ),
  296. ),
  297. 'variants' => array(),
  298. 'dependencies' => array(
  299. 'openlayers3_integration',
  300. ),
  301. );
  302. if ($version = \Drupal\openlayers\Openlayers::getLocalLibraryVersion()) {
  303. unset($libraries['openlayers3']['library path']);
  304. $libraries['openlayers3']['variants']['local:' . $version] = array(
  305. 'name' => 'Openlayers ' . $version,
  306. 'files' => array(
  307. 'js' => array(
  308. 'build/ol.js' => array(
  309. 'type' => 'file',
  310. 'weight' => 6,
  311. 'group' => $js_css_group,
  312. ),
  313. ),
  314. 'css' => array(
  315. 'css/ol.css' => array(
  316. 'type' => 'file',
  317. 'weight' => 4,
  318. 'group' => $js_css_group,
  319. ),
  320. ),
  321. ),
  322. );
  323. }
  324. return $libraries;
  325. }
  326. /**
  327. * Implements hook_hook_info().
  328. */
  329. function openlayers_hook_info() {
  330. $hooks = array(
  331. 'openlayers_object_preprocess_alter',
  332. 'openlayers_object_postprocess_alter',
  333. );
  334. return array_fill_keys($hooks, array('group' => 'openlayers'));
  335. }