Mapbox module: add Mapbox API key to farm map settings form and add the Mapbox Satellite layer to maps.

This commit is contained in:
Michael Stenta 2019-09-23 15:02:30 -04:00
parent 0d3a71e7f9
commit e9e48590b5
7 changed files with 138 additions and 0 deletions

View File

@ -136,6 +136,14 @@ function farm_install_configure_form($form, &$form_state) {
'#default_value' => variable_get('farm_map_google_api_key', ''),
);
// Allow the user to enter a Mapbox API key.
$form['farm_map_mapbox_api_key'] = array(
'#type' => 'textfield',
'#title' => t('Mapbox API Key'),
'#description' => t('Enter your Mapbox API key.') . ' ' . t('This can also be done after installation.'),
'#default_value' => variable_get('farm_map_mapbox_api_key', ''),
);
// Form actions.
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
@ -169,6 +177,14 @@ function farm_install_configure_form_submit($form, &$form_state) {
module_enable(array('farm_map_google'));
}
}
// If a Mapbox API key was provided, save it and enable the module.
if (!empty($form_state['values']['farm_map_mapbox_api_key'])) {
variable_set('farm_map_mapbox_api_key', $form_state['values']['farm_map_mapbox_api_key']);
if (!module_exists('farm_map_mapbox')) {
module_enable(array('farm_map_mapbox'));
}
}
}
/**

View File

@ -44,3 +44,13 @@ function farm_map_update_7002(&$sandbox) {
module_enable(array($module));
}
}
/**
* Install new Mapbox module if an API key is saved.
*/
function farm_map_update_7003(&$sandbox) {
$module = 'farm_map_mapbox';
if (variable_get('farm_map_mapbox_api_key', FALSE) && !module_exists($module)) {
module_enable(array($module));
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
* @file
* Farm Map hooks implemented by the Farm Map Mapbox module.
*/
/**
* Implements hook_farm_map_behaviors().
*/
function farm_map_mapbox_farm_map_behaviors() {
return array(
'mapbox' => array(
'js' => 'js/farmOS.map.behaviors.mapbox.js',
),
);
}
/**
* Implements hook_farm_map_behavior_settings().
*/
function farm_map_mapbox_farm_map_behavior_settings($behavior) {
$settings = array();
if ($behavior == 'mapbox') {
$settings['api_key'] = variable_get('farm_map_mapbox_api_key', FALSE);
}
return $settings;
}
/**
* Implements hook_farm_map_view().
*/
function farm_map_mapbox_farm_map_view($name, $element) {
// If a Mapbox API key is set, enable Mapbox layers in all farm maps.
if (!empty(variable_get('farm_map_mapbox_api_key', FALSE))) {
farm_map_add_behavior('mapbox');
}
}

View File

@ -0,0 +1,5 @@
name = Farm Map MapBox
description = Adds MapBox layers to farm maps.
core = 7.x
package = farmOS
dependencies[] = farm_map

View File

@ -0,0 +1,15 @@
<?php
/**
* @file
* Farm map Mapbox install.
*/
/**
* Implements hook_uninstall().
*/
function farm_map_mapbox_uninstall() {
// Delete variable.
variable_del('farm_map_mapbox_api_key');
}

View File

@ -0,0 +1,35 @@
<?php
/**
* @file
* Farm map Mapbox.
*/
/**
* Implements hook_farm_info().
*/
function farm_map_mapbox_farm_info() {
$info = array();
// Add the Mapbox API key to /farm.json.
$api_key = variable_get('farm_map_mapbox_api_key', '');
if (!empty($api_key)) {
$info['mapbox_api_key'] = $api_key;
}
return $info;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function farm_map_mapbox_form_farm_map_settings_form_alter(&$form, &$form_state, $form_id) {
// Add a field for setting the Mapbox API key in the farm_map settings form.
$form['farm_map_mapbox_api_key'] = array(
'#type' => 'textfield',
'#title' => t('Mapbox API Key'),
'#description' => t('Enter your Mapbox API key.'),
'#default_value' => variable_get('farm_map_mapbox_api_key', ''),
);
}

View File

@ -0,0 +1,19 @@
(function () {
farmOS.map.behaviors.mapbox = {
attach: function (instance) {
var key = Drupal.settings.farm_map.behaviors.mapbox.api_key;
this.addMapboxLayer(instance, 'Mapbox Satellite', 'mapbox.satellite', key);
this.addMapboxLayer(instance, 'Mapbox Outdoors', 'mapbox.outdoors', key);
},
addMapboxLayer: function (instance, title, tileset, key) {
var opts = {
title: title,
url: 'https://api.mapbox.com/v4/' + tileset + '/{z}/{x}/{y}.png?access_token=' + key,
group: 'Base layers',
base: true,
visible: false,
};
instance.addLayer('xyz', opts);
}
};
}());