styled_google_map.module in Styled Google Map 8
Same filename and directory in other branches
Contains all hooks and functions for the Styled Google Map module.
File
styled_google_map.moduleView source
<?php
/**
* @file
* Contains all hooks and functions for the Styled Google Map module.
*/
use Drupal\Component\Utility\UrlHelper;
define('STYLED_GOOGLE_MAP_DEFAULT_WIDTH', '450px');
define('STYLED_GOOGLE_MAP_DEFAULT_HEIGHT', '400px');
define('STYLED_GOOGLE_MAP_DEFAULT_STYLE', '[]');
define('STYLED_GOOGLE_MAP_DEFAULT_ZOOM', 15);
define('STYLED_GOOGLE_MAP_DEFAULT_MAX_ZOOM', 17);
define('STYLED_GOOGLE_MAP_DEFAULT_MIN_ZOOM', 5);
define('STYLED_GOOGLE_MAP_DEFAULT_PIN', 'module://styled_google_map/pin.png');
define('STYLED_GOOGLE_MAP_DEFAULT_MAP_TYPE', 'ROADMAP');
define('STYLED_GOOGLE_MAP_DEFAULT_LABEL', FALSE);
define('STYLED_GOOGLE_MAP_DEFAULT_MAP_TYPE_CONTROL', TRUE);
define('STYLED_GOOGLE_MAP_DEFAULT_SCALE_CONTROL', TRUE);
define('STYLED_GOOGLE_MAP_DEFAULT_ROTATE_CONTROL', TRUE);
define('STYLED_GOOGLE_MAP_DEFAULT_DRAGGABLE', TRUE);
define('STYLED_GOOGLE_MAP_DEFAULT_MOBILE_DRAGGABLE', TRUE);
define('STYLED_GOOGLE_MAP_DEFAULT_ZOOM_CONTROL', TRUE);
define('STYLED_GOOGLE_MAP_DEFAULT_STREET_VIEW_CONTROL', TRUE);
define('STYLED_GOOGLE_MAP_DEFAULT_SCROLLWHEEL', TRUE);
define('STYLED_GOOGLE_MAP_DEFAULT_SHADOW_STYLE', 0);
define('STYLED_GOOGLE_MAP_DEFAULT_PADDING', 13);
define('STYLED_GOOGLE_MAP_DEFAULT_BORDER_RADIUS', 7);
define('STYLED_GOOGLE_MAP_DEFAULT_BORDER_WIDTH', 1);
define('STYLED_GOOGLE_MAP_DEFAULT_BORDER_COLOR', '#cccccc');
define('STYLED_GOOGLE_MAP_DEFAULT_BACKGROUND_COLOR', '#ffffff');
define('STYLED_GOOGLE_MAP_DEFAULT_MIN_WIDTH', 'auto');
define('STYLED_GOOGLE_MAP_DEFAULT_MAX_WIDTH', 'auto');
define('STYLED_GOOGLE_MAP_DEFAULT_MIN_HEIGHT', 'auto');
define('STYLED_GOOGLE_MAP_DEFAULT_MAX_HEIGHT', 'auto');
define('STYLED_GOOGLE_MAP_DEFAULT_AUTO_CLOSE', TRUE);
define('STYLED_GOOGLE_MAP_DEFAULT_ARROW_SIZE', 10);
define('STYLED_GOOGLE_MAP_DEFAULT_ARROW_POSITION', 50);
define('STYLED_GOOGLE_MAP_DEFAULT_ARROW_STYLE', 0);
define('STYLED_GOOGLE_MAP_DEFAULT_DISABLE_AUTO_PAN', 0);
define('STYLED_GOOGLE_MAP_DEFAULT_HIDE_CLOSE_BUTTON', 0);
define('STYLED_GOOGLE_MAP_DEFAULT_DISABLE_ANIMATION', 1);
define('STYLED_GOOGLE_MAP_DEFAULT_BACKGROUND_CLASS', 'sgmpopup-content');
define('STYLED_GOOGLE_MAP_DEFAULT_CONTENT_CONTAINER_CLASS', 'sgmpopup-content-wrapper');
define('STYLED_GOOGLE_MAP_DEFAULT_ARROW_CLASS', 'sgmpopup-arrow');
define('STYLED_GOOGLE_MAP_DEFAULT_ARROW_OUTER_CLASS', 'sgmpopup-arrow-outer');
define('STYLED_GOOGLE_MAP_DEFAULT_ARROW_INNER_CLASS', 'sgmpopup-arrow-inner');
// Enumerate options for type of API authentication.
define('STYLED_GOOGLE_MAP_GOOGLE_AUTH_KEY', 1);
define('STYLED_GOOGLE_MAP_GOOGLE_AUTH_WORK', 2);
define('STYLED_GOOGLE_MAP_DEFAULT_GESTURE', 'cooperative');
use Drupal\Component\Utility\Xss;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
/**
* Implements hook_theme().
*/
function styled_google_map_theme() {
$themes['styled_google_map'] = array(
'variables' => array(
'location' => NULL,
),
);
return $themes;
}
/**
* Returns HTML for the styled google map.
*
* @param array $variables
* An associative array containing:
* - location: The location object including longitude and latitude.
* - display: Display array including formatter settings.
*
* @ingroup themeable
*
* @return mixed|null
* Rendered map.
*/
function theme_styled_google_map(array $variables) {
$language = Drupal::languageManager()
->getCurrentLanguage();
$location = $variables['location']
->getValue();
$field_list = $variables['location']
->getParent();
$entity = $field_list
->getEntity();
$settings = $variables['settings'];
$output = array();
// TODO: Split this function so logic is out of the theming function.
if (!empty($location) && $location['geohash']) {
// Get the pin file url.
if (isset($settings['style']['pin']) && !empty($settings['style']['pin'])) {
$settings['style']['pin'] = file_create_url($settings['style']['pin']);
}
// Sanitize the output of the style settings.
foreach ($settings['style'] as $id => $setting) {
if ($id != 'style') {
$location[$id] = Xss::filter($settings['style'][$id]);
}
}
// Get the label settings.
if (isset($entity) && !empty($entity)) {
switch ($settings['popup']['choice']) {
// Create popup from label.
case 1:
$settings['popup']['label'] = $settings['popup']['label'] ? 'inline' : 'hidden';
$popup_field = $entity->{$settings['popup']['text']}
->view(array(
'label' => $settings['popup']['label'],
), $language
->getId());
break;
// Create popup from view mode.
case 2:
$display = EntityViewDisplay::collectRenderDisplay($entity, $settings['popup']['view_mode']);
$popup_field = $display
->build($entity);
break;
// Default to empty.
default:
$popup_field = array();
}
$location['popup'] = render($popup_field);
}
else {
// Not an entity object.
$location['popup'] = array();
}
if ($settings['map_center']['center_coordinates']) {
$map_center = $entity
->getTranslation($language
->getId())
->get($settings['map_center']['center_coordinates'])
->getValue();
if ($map_center && isset($map_center[0]['lat']) && isset($map_center[0]['lon'])) {
$settings['map_center']['center_coordinates'] = $map_center[0];
}
else {
$settings['map_center']['center_coordinates'] = FALSE;
}
}
$gid = uniqid();
// Include the Google Maps API.
// Include the map location settings.
$map_settings['locations'] = array(
$location,
);
// Include the custom map settings.
$map_settings['settings'] = $settings;
// Include the unique div id.
$map_settings['id'] = 'styled-google-map-' . $gid;
$output['#attached']['drupalSettings']['styled_google_map'] = array(
$gid => $gid,
);
$output['#attached']['drupalSettings']['maps'] = array(
'id' . $gid => $map_settings,
);
// Output a div placeholder for the Styled Google Map.
$output['styled_google_map']['#markup'] = '<div class="styled_map" id="styled-google-map-' . $gid . '"></div>';
// Attach the Styled Google Map javascript file.
$output['#attached']['library'][] = 'styled_google_map/styled-google-map';
}
return render($output);
}
/**
* Implements hook_library_info_alter().
*/
function styled_google_map_library_info_alter(&$libraries, $extension) {
if ($extension == 'styled_google_map' && isset($libraries['styled-google-map'])) {
if (!empty($libraries['styled-google-map']['js']['//maps.google.com/maps/api/js'])) {
$options = $libraries['styled-google-map']['js']['//maps.google.com/maps/api/js'];
unset($libraries['styled-google-map']['js']['//maps.google.com/maps/api/js']);
$api_url = styled_google_map_build_api_url();
$libraries['styled-google-map']['js']['//maps.google.com/maps/api/js' . $api_url] = $options;
}
}
}
/**
* Create url addition for Google Maps library.
*
* @return string
* Url for the google maps library with API key.
*/
function styled_google_map_build_api_url() {
$api_url = '';
$config = \Drupal::config('styled_google_map.settings');
$query = array(
'v' => '3',
);
switch ($config
->get('styled_google_map_google_auth_method')) {
case STYLED_GOOGLE_MAP_GOOGLE_AUTH_KEY:
$key = $config
->get('styled_google_map_google_apikey', FALSE);
if ($key) {
$query['key'] = $key;
}
break;
case STYLED_GOOGLE_MAP_GOOGLE_AUTH_WORK:
$client_id = $config
->get('styled_google_map_google_client_id', FALSE);
if ($client_id) {
$query['client'] = $client_id;
}
break;
}
// Add query params to API url.
if (!empty($query)) {
$api_url .= '?' . UrlHelper::buildQuery($query);
}
return $api_url;
}
Functions
Name | Description |
---|---|
styled_google_map_build_api_url | Create url addition for Google Maps library. |
styled_google_map_library_info_alter | Implements hook_library_info_alter(). |
styled_google_map_theme | Implements hook_theme(). |
theme_styled_google_map | Returns HTML for the styled google map. |