diff --git a/styled_google_map.admin.inc b/styled_google_map.admin.inc new file mode 100644 index 0000000..34feb71 --- /dev/null +++ b/styled_google_map.admin.inc @@ -0,0 +1,48 @@ + 'select', + '#title' => t('Google API Authentication Method'), + '#description' => t(''), + '#default_value' => variable_get('styled_google_map_google_auth_method', STYLED_GOOGLE_MAP_GOOGLE_AUTH_KEY), + '#options' => array( + STYLED_GOOGLE_MAP_GOOGLE_AUTH_KEY => t('API Key'), + STYLED_GOOGLE_MAP_GOOGLE_AUTH_WORK => t('Google Maps API for Work'), + ), + ); + + $form['styled_google_map_google_apikey'] = array( + '#type' => 'textfield', + '#title' => t('Google Maps API Key'), + '#description' => t('Obtain a Google Maps Javascript API key at @link', array( + '@link' => 'https://developers.google.com/maps/documentation/javascript/get-api-key', + )), + '#default_value' => variable_get('styled_google_map_google_apikey', ''), + '#required' => FALSE, + '#states' => array( + 'visible' => array( + ':input[name="styled_google_map_google_auth_method"]' => array('value' => STYLED_GOOGLE_MAP_GOOGLE_AUTH_KEY), + ), + ), + ); + + $form['styled_google_map_google_client_id'] = array( + '#type' => 'textfield', + '#title' => t('Google Maps API for Work: Client ID'), + '#description' => t('For more information, visit: @link', array( + '@link' => 'https://developers.google.com/maps/documentation/javascript/get-api-key#client-id', + )), + '#default_value' => variable_get('styled_google_map_google_client_id', ''), + '#required' => FALSE, + '#states' => array( + 'visible' => array( + ':input[name="styled_google_map_google_auth_method"]' => array('value' => STYLED_GOOGLE_MAP_GOOGLE_AUTH_WORK), + ), + ), + ); + + return system_settings_form($form); +} \ No newline at end of file diff --git a/styled_google_map.info b/styled_google_map.info index c2248a0..ba68bf6 100644 --- a/styled_google_map.info +++ b/styled_google_map.info @@ -4,6 +4,8 @@ core = 7.x dependencies[] = system_stream_wrapper dependencies[] = geofield +configure = admin/config/content/styled_google_map + ; Information added by Drupal.org packaging script on 2016-05-12 version = "7.x-2.0" core = "7.x" diff --git a/styled_google_map.module b/styled_google_map.module index 5aeb1e8..4a2eb40 100755 --- a/styled_google_map.module +++ b/styled_google_map.module @@ -1,4 +1,4 @@ - t('Styled google map settings'), + 'description' => t('Configuration for API keys.'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('styled_google_map_admin_settings'), + 'file' => 'styled_google_map.admin.inc', + 'access arguments' => array('administer site configuration'), + 'type' => MENU_NORMAL_ITEM, + ); + + return $items; +} /** * Implements hook_theme(). @@ -601,8 +622,11 @@ function theme_styled_google_map(array $variables) { $gid = uniqid(); drupal_add_js(array('styled_google_map' => array($gid => $gid)), 'setting'); + // Build API url. + $api_url = styled_google_map_build_api_url(); + // Include the Google Maps API. - drupal_add_js('//maps.google.com/maps/api/js?sensor=true', array('type' => 'external', 'group' => JS_LIBRARY)); + drupal_add_js($api_url, array('type' => 'external', 'group' => JS_LIBRARY)); // Include the Info Bubble API. drupal_add_js(drupal_get_path('module', 'styled_google_map') . '/lib/infobubble.js'); // Include the map location settings. @@ -627,3 +651,76 @@ function theme_styled_google_map(array $variables) { return render($output); } + +/** + * Builds the javascript maps api url based on authentication method. + */ +function styled_google_map_build_api_url() { + // Google api url. + $api_url = '//maps.googleapis.com/maps/api/js'; + // Array to hold query paramters for the google maps url. + // Including version number as it's required for Premium plans. + // https://developers.google.com/maps/documentation/javascript/versions + $query = array('v' => '3'); + + switch (variable_get('styled_google_map_google_auth_method')) { + case STYLED_GOOGLE_MAP_GOOGLE_AUTH_KEY: + $key = variable_get('styled_google_map_google_apikey', FALSE); + if (!empty($key)) { + $query['key'] = $key; + } + break; + + case STYLED_GOOGLE_MAP_GOOGLE_AUTH_WORK: + $client_id = variable_get('styled_google_map_google_client_id', FALSE); + if (!empty($client_id)) { + $query['client'] = $client_id; + } + break; + } + + // Add query params to API url. + if (!empty($query)) { + $api_url .= '?' . drupal_http_build_query($query); + } + + return $api_url; +} + +/** + * Implementation of hook_requirements(). + */ +function styled_google_map_requirements($phase) { + $requirements = array(); + + // Whether or not an API key or client id is provided. + $key_provided = FALSE; + + switch (variable_get('styled_google_map_google_auth_method')) { + case STYLED_GOOGLE_MAP_GOOGLE_AUTH_KEY: + $key = variable_get('styled_google_map_google_apikey', FALSE); + if (!empty($key)) { + $key_provided = TRUE; + } + break; + + case STYLED_GOOGLE_MAP_GOOGLE_AUTH_WORK: + $client_id = variable_get('styled_google_map_google_client_id', FALSE); + if (!empty($client_id)) { + $key_provided = TRUE; + } + break; + } + + if (!$key_provided) { + $requirements['styled_google_map'] = array( + 'title' => t('Styled Google Map'), + 'severity' => REQUIREMENT_WARNING, + 'value' => t('Google Maps API key or Client ID was not found. As of 2016/06/22, keyless access is no longer supported and it may impact rendering of maps. For more information visit: @link', array( + '@link' => 'http://googlegeodevelopers.blogspot.ca/2016/06/building-for-scale-updates-to-google.html', + )), + ); + } + + return $requirements; +} diff --git a/styled_google_views/styled_google_views.module b/styled_google_views/styled_google_views.module index 7f5eee6..4fd2542 100644 --- a/styled_google_views/styled_google_views.module +++ b/styled_google_views/styled_google_views.module @@ -35,8 +35,12 @@ function styled_google_views_preprocess_styled_google_view(&$vars) { return; } drupal_add_js(drupal_get_path('module', 'styled_google_map') . '/styled-google-map.js'); + + // Build API url. + $api_url = styled_google_map_build_api_url(); + // Include the Google Maps API. - drupal_add_js('//maps.google.com/maps/api/js?sensor=true', array('type' => 'external', 'group' => JS_LIBRARY)); + drupal_add_js($api_url, array('type' => 'external', 'group' => JS_LIBRARY)); drupal_add_js(drupal_get_path('module', 'styled_google_map') . '/lib/markerclusterer.js'); drupal_add_js(drupal_get_path('module', 'styled_google_map') . '/lib/infobubble.js'); // Get all geofield locations.