t('MapBox.js library not found'), 'value' => t('The !mapboxjs javascript library was not found. Please !download it into the mapbox subdirectory of the libraries folder.', array( '!mapboxjs' => l('MapBox.js', 'http://mapbox.com/mapbox.js'), '!download' => l('download', 'http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.js'), ) ), 'severity' => REQUIREMENT_ERROR, ); } // Ensure MapBox css file is available if ($phase == 'runtime' && !file_exists(libraries_get_path('mapboxjs') . '/mapbox.css')) { $requirements['mapboxjs'] = array( 'title' => t('MapBox css not found'), 'value' => t('The !mapboxjs css file was not found. Please !download it into the mapbox subdirectory of the libraries folder.', array( '!mapboxjs' => l('MapBox.js', 'http://mapbox.com/mapbox.js'), '!download' => l('download', 'http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.css'), ) ), 'severity' => REQUIREMENT_ERROR, ); } return $requirements; } /** * Implements hook_library(). */ function mapboxjs_library() { $libraries['mapboxjs'] = array( 'title' => 'MapBox.js', 'website' => 'http://mapbox.com/mapbox.js', 'version' => '0.6.6', 'js' => array( // Load the MapBox.js javascript library. libraries_get_path('mapboxjs') . '/mapbox.js' => array( 'type' => 'file', ), // Load our javascript implementation of MapBox.js. drupal_get_path('module', 'mapboxjs') . '/js/mapboxjs.drupal.js' => array( 'type' => 'file', ), ), 'css' => array( libraries_get_path('mapboxjs') . '/mapbox.css' => array( 'type' => 'file', 'media' => 'screen', 'group' => CSS_DEFAULT, ), drupal_get_path('module', 'mapboxjs') . '/css/mapboxjs.drupal.css' => array( 'type' => 'file', 'media' => 'screen', 'group' => CSS_DEFAULT, ) ), ); return $libraries; } /** * Implements hook_permission(). */ function mapboxjs_permission() { return array( 'administer mapboxjs presets' => array( 'title' => t('administer mapboxjs presets'), 'description' => t('Administer MapBox.js map presets.'), 'restrict access' => TRUE, ), ); } /** * Access callback for managing MapBox.js map presets. * * @return bool */ function mapboxjs_access() { if (user_access('administer mapboxjs presets')) { return TRUE; } return FALSE; } /** * Implements hook_entity_info(). * * Provides our custom entity type/bundle for MapBox.js map presets. */ function mapboxjs_entity_info() { $return = array( 'mapboxjs_preset' => array( 'label' => t('MapBox.js map preset'), 'controller class' => 'EntityAPIControllerExportable', 'entity class' => 'MapboxjsPreset', 'base table' => 'mapboxjs_preset', 'uri callback' => 'entity_class_uri', 'fieldable' => TRUE, 'exportable' => TRUE, 'module' => 'mapboxjs', 'entity keys' => array( 'id' => 'mapboxjs_preset_id', 'name' => 'name', 'label' => 'label' ), 'bundles' => array( 'mapboxjs_preset' => array( 'label' => 'Mapbox.js map preset', ), ), // Enable the entity API's admin UI. 'admin ui' => array( 'path' => 'admin/structure/mapboxjs/presets', 'file' => 'mapboxjs.admin.inc', 'file path' => drupal_get_path('module', 'mapboxjs') . '/includes', ), 'access callback' => 'mapboxjs_access', 'label callback' => 'entity_class_label', ), ); return $return; } /** * Gets an array of all registration types, keyed by the name. * * @param string $name * If set, the type with the given name is returned. */ function mapboxjs_get_presets($name = NULL) { $presets = entity_load_multiple_by_name('mapboxjs_preset', isset($name) ? array($name) : FALSE); return isset($name) ? reset($presets) : $presets; } /** * Render a preset as a map. * * @param MapboxjsPreset $preset * * @return MapBox.js map as a renderable array */ function mapboxjs_render_preset(MapboxjsPreset $preset) { $map_id = 'map-' . $preset->name; // @TODO - Provide unique ID for each MapBox map so that we can act on multiple maps on a single page. // Grab our required base tileset(s). $base_tileset_items = field_get_items('mapboxjs_preset', $preset, 'field_base_tilesets'); $base_tilesets = array(); foreach ($base_tileset_items as $item) { $base_tilesets[] = array('title' => $item['title'], 'url' => $item['url']); } // Grab our optional tilesets to be laid on top of base tileset(s). $optional_tileset_items = field_get_items('mapboxjs_preset', $preset, 'field_optional_tilesets'); if (!empty($optional_tileset_items)) { $optional_tilesets = array(); foreach ($optional_tileset_items as $item) { $optional_tilesets[] = array( 'title' => $item['title'], 'url' => $item['url'] ); } } $settings = array('mapID' => $map_id); foreach ($preset->settings as $key => $value) { $settings['configuration'][$key] = $value; } // Pass our settings to Drupal's global js variable. drupal_add_js(array('mapboxjs' => $settings), 'setting'); // Optionally, start adding extra controls. $extras = NULL; // Add a base tile switcher if we have more than one base tileset defined. if (count($base_tilesets) > 1) { $switcher = ''; // @TODO - Generate markup using renderable array. foreach ($base_tilesets as $key => $tileset) { $switcher .= "
  • " . $tileset['title'] . "
  • "; } $switcher = "
    "; $extras .= $switcher; } // Add additional markup layer toggle controls option selected. if ($preset->layer_toggle == TRUE) { $extras .= ""; } return mapboxjs_render_map($map_id, $preset->height, $extras); } /** * Implements hook_theme(). */ function mapboxjs_theme($existing, $type, $theme, $path) { return array( 'mapboxjs_map' => array( 'arguments' => array( 'map_id' => NULL, 'height' => 400, 'extras' => '' ), 'template' => 'mapboxjs_map', ), ); } /** * Load all MapBox.js required client files and return markup for a map. * * @param string $map_id * @param string $height * @param string $extras * Contains additional markup or controls embedded in the map element. * * @return string map markup * * @TODO - Provide some additional default map settings that can be overridden. */ function mapboxjs_render_map($map_id = 'mapboxjs-map', $height = 400, $extras = NULL) { // Add javascript and css dependencies. drupal_add_library('mapboxjs', 'mapboxjs'); return theme('mapboxjs_map', array( 'map_id' => $map_id, 'height' => $height, 'extras' => $extras )); }