3 module_load_include('inc', 'mapboxjs', 'includes/mapboxjs.field');
6 * Implements hook_requirements().
8 * @TODO - Rather than grabbing mapbox.js and mapbox.css directly, checkout project from GitHub
9 * at: https://github.com/mapbox/mapbox.js/tree/v0.6.7. Would then need to update mapboxjs.make.example.
11 function mapboxjs_requirements($phase) {
12 $requirements = array();
14 // Ensure js library is available
15 if ($phase == 'runtime' && !file_exists(libraries_get_path('mapboxjs') .
'/mapbox.js')) {
16 $requirements['mapboxjss'] = array(
17 'title' => t('MapBox.js library not found'),
18 'value' => t('The !mapboxjs javascript library was not found. Please !download it into the mapbox subdirectory of the libraries folder.',
20 '!mapboxjs' => l('MapBox.js', 'http://mapbox.com/mapbox.js'),
21 '!download' => l('download', 'http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.js'),
24 'severity' => REQUIREMENT_ERROR
,
27 // Ensure MapBox css file is available
28 if ($phase == 'runtime' && !file_exists(libraries_get_path('mapboxjs') .
'/mapbox.css')) {
29 $requirements['mapboxjs'] = array(
30 'title' => t('MapBox css not found'),
31 'value' => t('The !mapboxjs css file was not found. Please !download it into the mapbox subdirectory of the libraries folder.',
33 '!mapboxjs' => l('MapBox.js', 'http://mapbox.com/mapbox.js'),
34 '!download' => l('download', 'http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.css'),
37 'severity' => REQUIREMENT_ERROR
,
45 * Implements hook_library().
47 function mapboxjs_library() {
48 $libraries['mapboxjs'] = array(
49 'title' => 'MapBox.js',
50 'website' => 'http://mapbox.com/mapbox.js',
53 // Load the MapBox.js javascript library.
54 libraries_get_path('mapboxjs') .
'/mapbox.js' => array(
57 // Load our javascript implementation of MapBox.js.
58 drupal_get_path('module', 'mapboxjs') .
'/js/mapboxjs.drupal.js' => array(
63 libraries_get_path('mapboxjs') .
'/mapbox.css' => array(
66 'group' => CSS_DEFAULT
,
68 drupal_get_path('module', 'mapboxjs') .
'/css/mapboxjs.drupal.css' => array(
71 'group' => CSS_DEFAULT
,
79 * Implements hook_permission().
81 function mapboxjs_permission() {
83 'administer mapboxjs presets' => array(
84 'title' => t('administer mapboxjs presets'),
85 'description' => t('Administer MapBox.js map presets.'),
86 'restrict access' => TRUE
,
92 * Access callback for managing MapBox.js map presets.
96 function mapboxjs_access() {
97 if (user_access('administer mapboxjs presets')) {
104 * Implements hook_entity_info().
106 * Provides our custom entity type/bundle for MapBox.js map presets.
108 function mapboxjs_entity_info() {
110 'mapboxjs_preset' => array(
111 'label' => t('MapBox.js map preset'),
112 'controller class' => 'EntityAPIControllerExportable',
113 'entity class' => 'MapboxjsPreset',
114 'base table' => 'mapboxjs_preset',
115 'uri callback' => 'entity_class_uri',
117 'exportable' => TRUE
,
118 'module' => 'mapboxjs',
119 'entity keys' => array(
120 'id' => 'mapboxjs_preset_id',
125 'mapboxjs_preset' => array(
126 'label' => 'Mapbox.js map preset',
129 // Enable the entity API's admin UI.
131 'path' => 'admin/structure/mapboxjs/presets',
132 'file' => 'mapboxjs.admin.inc',
133 'file path' => drupal_get_path('module', 'mapboxjs') .
'/includes',
135 'access callback' => 'mapboxjs_access',
136 'label callback' => 'entity_class_label',
144 * Gets an array of all registration types, keyed by the name.
146 * @param string $name
147 * If set, the type with the given name is returned.
149 function mapboxjs_get_presets($name = NULL
) {
150 $presets = entity_load_multiple_by_name('mapboxjs_preset', isset($name) ?
array($name) : FALSE
);
151 return isset($name) ?
reset($presets) : $presets;
155 * Render a preset as a map.
157 * @param MapboxjsPreset $preset
159 * @return MapBox.js map as a renderable array
161 function mapboxjs_render_preset(MapboxjsPreset
$preset) {
162 $map_id = 'map-' .
$preset->name
;
163 // @TODO - Provide unique ID for each MapBox map so that we can act on multiple maps on a single page.
165 // Grab our required base tileset(s).
166 $base_tileset_items = field_get_items('mapboxjs_preset', $preset, 'field_base_tilesets');
167 $base_tilesets = array();
168 foreach ($base_tileset_items as
$item) {
169 $base_tilesets[] = array('title' => $item['title'], 'url' => $item['url']);
171 // Grab our optional tilesets to be laid on top of base tileset(s).
172 $optional_tileset_items = field_get_items('mapboxjs_preset', $preset, 'field_optional_tilesets');
173 if (!empty($optional_tileset_items)) {
174 $optional_tilesets = array();
175 foreach ($optional_tileset_items as
$item) {
176 $optional_tilesets[] = array(
177 'title' => $item['title'],
178 'url' => $item['url']
183 $settings = array('mapID' => $map_id);
184 foreach ($preset->settings as
$key => $value) {
185 $settings['configuration'][$key] = $value;
188 // Pass our settings to Drupal's global js variable.
189 drupal_add_js(array('mapboxjs' => $settings), 'setting');
191 // Optionally, start adding extra controls.
193 // Add a base tile switcher if we have more than one base tileset defined.
194 if (count($base_tilesets) > 1) {
196 // @TODO - Generate markup using renderable array.
197 foreach ($base_tilesets as
$key => $tileset) {
198 $switcher .
= "<li><a href='#' id='base-tile-" .
$key .
"'>" .
$tileset['title'] .
"</a></li>";
200 $switcher = "<div id='map-switcher'><ul>" .
$switcher .
"</ul></div>";
201 $extras .
= $switcher;
203 // Add additional markup layer toggle controls option selected.
204 if ($preset->layer_toggle
== TRUE
) {
205 $extras .
= "<ul id='map-toggle'></ul>";
208 return mapboxjs_render_map($map_id, $preset->height
, $extras);
213 * Implements hook_theme().
215 function mapboxjs_theme($existing, $type, $theme, $path) {
217 'mapboxjs_map' => array(
218 'arguments' => array(
223 'template' => 'mapboxjs_map',
229 * Load all MapBox.js required client files and return markup for a map.
231 * @param string $map_id
232 * @param string $height
233 * @param string $extras
234 * Contains additional markup or controls embedded in the map element.
236 * @return string map markup
238 * @TODO - Provide some additional default map settings that can be overridden.
240 function mapboxjs_render_map($map_id = 'mapboxjs-map', $height = 400, $extras = NULL
) {
241 // Add javascript and css dependencies.
242 drupal_add_library('mapboxjs', 'mapboxjs');
243 return theme('mapboxjs_map', array(