View source
<?php
module_load_include('inc', 'leaflet', 'leaflet.formatters');
function leaflet_help($path, $arg) {
switch ($path) {
case 'admin/help#leaflet':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Leaflet module provides integration with Leaflet, the modern open-source JavaScript library for mobile-friendly interactive maps.') . '</p>';
$output .= '<p>' . t('The module provides a field formatter that can show a map for fields that contain geospatial data. It includes Views integration that plots data on a map (using the sub module Leaflet Views) and an API for displaying data on a map. For more information, see the online documentation for the <a href="@leaflet">Leaflet module</a>.', array(
'@leaflet' => 'http://drupal.org/node/1645460',
)) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Field Formatter') . '</dt>';
$output .= '<dd>' . t('Leaflet includes a field formatter that makes it possible to show geospatial data (for example longitude and latitude) on a map. You can use tokens instead of static text for Popups that use the Leaflet field formatter.') . '</dd>';
$output .= '<dt>' . t('Views Integration') . '</dt>';
$output .= '<dd>' . t('You can have maps on node displays and maps on views displays, thanks to Views integration. The process is very similar to adding and setting up a plugin on Views. To render a map using Views, enable the included module Leaflet_views.') . '</dd>';
$output .= '<dt>' . t('Leaflet API') . '</dt>';
$output .= '<dd>' . t('Rendering a map is as simple as calling a single method, leaflet_render_map(), which takes 3 parameters $map, $features, and $height.') . '</dd>';
return $output;
}
}
function leaflet_theme($existing, $type, $theme, $path) {
return array(
'leaflet_map' => array(
'arguments' => array(
'map_id' => NULL,
'height' => '400px',
),
'template' => 'leaflet_map',
),
);
}
function leaflet_libraries_info() {
$libraries['leaflet'] = array(
'name' => 'Leaflet JavaScript Library',
'vendor url' => 'http://leafletjs.com/',
'download url' => 'http://cdn.leafletjs.com/leaflet/v1.0.2/leaflet.zip',
'version arguments' => array(
'file' => 'leaflet.js',
'pattern' => '/version[=: ]*[\'"]([\\d+\\.]+[\\-a-z\\.\\d]*)[\'"]/',
),
'files' => array(
'js' => array(
'leaflet_root_url' => array(
'type' => 'inline',
'data' => 'L_ROOT_URL = "' . base_path() . libraries_get_path('leaflet') . '/";',
'group' => JS_LIBRARY,
),
'leaflet.js' => array(
'type' => 'file',
'group' => JS_LIBRARY,
),
'leaflet_imagepath' => array(
'type' => 'inline',
'data' => 'L.Icon.Default.imagePath = "' . base_path() . libraries_get_path('leaflet') . '/images/";',
),
),
'css' => array(
'leaflet.css' => array(
'type' => 'file',
'media' => 'all',
),
'leaflet.ie.css' => array(
'browsers' => array(
'IE' => 'lte IE 8',
'!IE' => FALSE,
),
),
),
),
'integration files' => array(
'leaflet' => array(
'js' => array(
'leaflet.drupal.js',
),
),
),
);
return $libraries;
}
function leaflet_build_map($map, $features = array(), $height = '400px') {
$map_id = drupal_html_id('leaflet_map') . '-' . rand();
$build = array(
'#theme' => 'html_tag',
'#tag' => 'div',
'#value' => '',
'#attributes' => array(
'id' => $map_id,
'style' => 'height: ' . $height,
),
);
if (isset($map['icon']['iconUrl'])) {
foreach ($features as &$feature) {
if (!isset($feature['icon'])) {
$feature['icon'] = $map['icon'];
}
}
}
$settings = array(
'mapId' => $map_id,
'map' => $map,
'features' => $features,
);
drupal_alter('leaflet_map_prebuild', $settings);
$build['#attached']['js'][] = array(
'data' => array(
'leaflet' => array(
$settings,
),
),
'type' => 'setting',
);
$build['#attached']['css'][] = array(
'data' => drupal_get_path('module', 'leaflet') . '/leaflet_extras.css',
);
$build['#attached']['libraries_load'][] = array(
'leaflet',
);
drupal_alter('leaflet_build_map', $build);
return $build;
}
function leaflet_render_map($map, $features = array(), $height = '400px') {
$build = leaflet_build_map($map, $features, $height);
return render($build);
}
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 ($cache = cache_get("leaflet_map_info")) {
$map_info = $cache->data;
}
else {
$map_info = module_invoke_all('leaflet_map_info');
drupal_alter('leaflet_map_info', $map_info);
cache_set("leaflet_map_info", $map_info);
}
}
if (empty($map)) {
return $map_info;
}
elseif (isset($map_info[$map])) {
return $map_info[$map];
}
}
function leaflet_leaflet_map_info() {
return array(
'OSM Mapnik' => array(
'label' => 'OSM Mapnik',
'description' => t('Leaflet default map.'),
'settings' => array(
'zoomDefault' => 10,
'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' => '//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
'options' => array(
'attribution' => '<a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap </a> contributors',
),
),
),
),
);
}