Refactor Google Maps integration to use a farmOS-map behavior.

This commit is contained in:
Michael Stenta 2019-07-24 09:55:51 -04:00
parent 755f076242
commit 0d3a71e7f9
8 changed files with 111 additions and 31 deletions

View File

@ -132,7 +132,7 @@ function farm_install_configure_form($form, &$form_state) {
$form['farm_map_google_api_key'] = array(
'#type' => 'textfield',
'#title' => t('Google Maps API Key'),
'#description' => t('Google Maps layers require that you obtain an API key. Refer to the <a href="@doc">Google Maps API Key</a> documentation on farmOS.org for instructions.', array('@doc' => 'https://farmos.org/hosting/googlemaps')) . ' ' . t('This can also be done after installation. If no key is provided, OpenStreetMaps will be used as the default map base layer.'),
'#description' => t('Google Maps layers require that you obtain an API key. Refer to the <a href="@doc">Google Maps API Key</a> documentation on farmOS.org for instructions.', array('@doc' => 'https://farmos.org/hosting/googlemaps')) . ' ' . t('This can also be done after installation.'),
'#default_value' => variable_get('farm_map_google_api_key', ''),
);
@ -162,14 +162,12 @@ function farm_install_configure_form_submit($form, &$form_state) {
variable_set('farm_quantity_unit_system', $form_state['values']['farm_quantity_unit_system']);
}
// Save the Google Maps API key, and make Google Hybrid the default.
// If a Google Maps API key was not provided, make OpenStreetMap the default.
// If a Google Maps API key was provided, save it and enable the module.
if (!empty($form_state['values']['farm_map_google_api_key'])) {
variable_set('farm_map_google_api_key', $form_state['values']['farm_map_google_api_key']);
variable_set('farm_map_default_base_layer', 'farm_map_layer_google_hybrid');
}
else {
variable_set('farm_map_default_base_layer', 'farm_map_layer_openstreetmap');
if (!module_exists('farm_map_google')) {
module_enable(array('farm_map_google'));
}
}
}

View File

@ -11,7 +11,6 @@ function farm_map_uninstall() {
// Delete variables.
variable_del('farm_map_show');
variable_del('farm_map_google_api_key');
}
/**
@ -35,3 +34,13 @@ function farm_map_update_7000(&$sandbox) {
function farm_map_update_7001(&$sandbox) {
variable_del('farm_map_default_base_layer');
}
/**
* Install new Google Maps module if an API key is saved.
*/
function farm_map_update_7002(&$sandbox) {
$module = 'farm_map_google';
if (variable_get('farm_map_google_api_key', FALSE) && !module_exists($module)) {
module_enable(array($module));
}
}

View File

@ -298,33 +298,10 @@ function farm_map_settings_form($form, &$form_state) {
'#default_value' => variable_get('farm_map_show', TRUE),
);
// Google Maps API key.
$form['farm_map_google_api_key'] = array(
'#type' => 'textfield',
'#title' => t('Google Maps API Key'),
'#description' => t('Google Maps layers require that you obtain an API key. Refer to the <a href="@doc">Google Maps API Key</a> documentation on farmOS.org for instructions.', array('@doc' => 'https://farmos.org/hosting/googlemaps')),
'#default_value' => variable_get('farm_map_google_api_key', ''),
);
// Return it as a system settings form.
return system_settings_form($form);
}
/**
* Implements hook_farm_info().
*/
function farm_map_farm_info() {
$info = array();
// Add the Google Maps API key to /farm.json.
$google_maps_api_key = variable_get('farm_map_google_api_key', '');
if (!empty($google_maps_api_key)) {
$info['google_maps_api_key'] = $google_maps_api_key;
}
return $info;
}
/**
* Implements hook_openlayers_object_preprocess_alter().
*/

View File

@ -0,0 +1,34 @@
<?php
/**
* @file
* Farm Map hooks implemented by the Farm Map Google module.
*/
/**
* Implements hook_farm_map_behaviors().
*/
function farm_map_google_farm_map_behaviors() {
return array(
'google' => array(
'js' => 'js/farmOS.map.behaviors.google.js',
),
);
}
/**
* Implements hook_farm_map_view().
*/
function farm_map_google_farm_map_view($name, $element) {
// If a Google API key is set, enable Google Maps layers in farm maps.
$google_api_key = variable_get('farm_map_google_api_key', FALSE);
if (!empty($google_api_key)) {
// Add remote Google Maps JavaScript library, with the API key.
$url = 'https://maps.googleapis.com/maps/api/js?v=3&key=' . $google_api_key;
drupal_add_js($url, array('type' => 'external', 'weight' => -100));
// Add a farmOS map behavior that will enable Google Maps.
farm_map_add_behavior('google');
}
}

View File

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

View File

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

View File

@ -0,0 +1,35 @@
<?php
/**
* @file
* Farm map google.
*/
/**
* Implements hook_farm_info().
*/
function farm_map_google_farm_info() {
$info = array();
// Add the Google Maps API key to /farm.json.
$api_key = variable_get('farm_map_google_api_key', '');
if (!empty($api_key)) {
$info['google_maps_api_key'] = $api_key;
}
return $info;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function farm_map_google_form_farm_map_settings_form_alter(&$form, &$form_state, $form_id) {
// Add a field for setting the Google API key in the farm_map settings form.
$form['farm_map_google_api_key'] = array(
'#type' => 'textfield',
'#title' => t('Google Maps API Key'),
'#description' => t('Google Maps layers require that you obtain an API key. Refer to the <a href="@doc">Google Maps API Key</a> documentation on farmOS.org for instructions.', array('@doc' => 'https://farmos.org/hosting/googlemaps')),
'#default_value' => variable_get('farm_map_google_api_key', ''),
);
}

View File

@ -0,0 +1,7 @@
(function () {
farmOS.map.behaviors.google = {
attach: function (instance) {
instance.addBehavior('google');
}
};
}());