device_geolocation.module in Smart IP 8.4
Same filename and directory in other branches
Provides visitor's geographical location using client device location source that implements W3C Geolocation API and Google Geocoding service.
@author Roland Michael dela Peña.
File
modules/device_geolocation/device_geolocation.moduleView source
<?php
/**
* @file
* Provides visitor's geographical location using client device location source
* that implements W3C Geolocation API and Google Geocoding service.
*
* @author Roland Michael dela Peña.
*/
use Drupal\device_geolocation\DeviceGeolocation;
use Drupal\device_geolocation\EventSubscriber\SmartIpEventSubscriber;
use Drupal\smart_ip\SmartIpLocationInterface;
use Drupal\smart_ip\SmartIp;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Implements hook_entity_insert().
*/
function device_geolocation_entity_insert(EntityInterface $entity) {
// Reset the timer for frequency of user's geolocation checking
SmartIp::setSession('device_geolocation_last_attempt', NULL);
}
/**
* Implements hook_user_login().
*/
function device_geolocation_user_login(AccountInterface $account) {
// Reset the timer for frequency of user's geolocation checking
SmartIp::setSession('device_geolocation_last_attempt', NULL);
}
/**
* Implements hook_theme().
*/
function device_geolocation_theme() {
return [
'device_geolocation_visitor_info' => [
'variables' => [
'location' => [],
],
],
];
}
/**
* Prepares variables for visitor's geolocation block templates.
*
* Default template: device-geolocation-visitor-info.html.twig.
*
* @param array $variables
*/
function template_preprocess_device_geolocation_visitor_info(&$variables) {
$blockData = [];
foreach ($variables['location'] as $label => $value) {
if ($label == 'isEuCountry' && !$value || $label == 'isGdprCountry' && !$value || !empty($value) && $label != 'regionCode' && $label != 'originalData' && $label != 'ipVersion') {
if ($label == 'source') {
switch ($value) {
case SmartIpLocationInterface::GEOCODED_SMART_IP:
$value = t('Google Map Geocoded Smart IP coordinates');
break;
case SmartIpLocationInterface::W3C:
$value = t('Geocoded W3C coordinates');
break;
default:
$value = t('Smart IP geolocation');
break;
}
}
elseif ($label == 'isEuCountry' || $label == 'isGdprCountry') {
if ($value) {
$value = t('Yes');
}
else {
$value = t('No');
}
}
elseif ($label == 'timestamp') {
$value = \Drupal::service('date.formatter')
->format($value, 'long');
}
$label = preg_replace('/(?<=\\w)(?=[A-Z])/', " \$1", $label);
$label = str_replace('_', ' ', $label);
$label = ucwords(mb_strtolower($label));
$blockData[$label] = $value;
}
}
$variables['location'] = $blockData;
}
/**
* Implements hook_page_attachments().
*/
function device_geolocation_page_attachments(array &$page) {
if (!\Drupal\smart_ip\SmartIp::checkAllowedPage()) {
// This page is not on the list to access user's geolocation
return;
}
$config = \Drupal::config(SmartIpEventSubscriber::configName());
$useAjaxCheck = $config
->get('use_ajax_check');
if ($useAjaxCheck || DeviceGeolocation::isNeedUpdate()) {
/** @var \Drupal\smart_ip\SmartIpLocation $location */
$location = \Drupal::service('smart_ip.smart_ip_location');
// Send current user's geolocation to javascript.
$page['#attached']['drupalSettings']['device_geolocation'] = $location
->getData(FALSE);
$debugMode = SmartIp::isUserDebugMode();
//$debugMode = FALSE;
$page['#attached']['drupalSettings']['device_geolocation']['debugMode'] = $debugMode;
if ($useAjaxCheck) {
// Add javascript app that checks if device geolocation needs refresh via
// ajax call.
$page['#attached']['library'][] = 'device_geolocation/drupal.device_geolocation.check';
$page['#attached']['drupalSettings']['device_geolocation']['askGeolocate'] = FALSE;
}
else {
$page['#attached']['drupalSettings']['device_geolocation']['askGeolocate'] = TRUE;
}
$param = '';
$apiKey = $config
->get('google_map_api_key');
$languageRegion = $config
->get('google_map_region');
$languageVal = $config
->get('google_map_language');
if ($apiKey) {
$param .= "key={$apiKey}";
}
if ($languageVal) {
$param .= "&language={$languageVal}";
}
if ($languageRegion) {
$param .= "®ion={$languageRegion}";
}
$page['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#attributes' => [
'src' => "//maps.googleapis.com/maps/api/js?{$param}",
'async' => TRUE,
'defer' => TRUE,
],
],
'device_geolocation_google_map_lib',
];
$page['#attached']['library'][] = 'device_geolocation/drupal.device_geolocation.core';
}
}
Functions
Name![]() |
Description |
---|---|
device_geolocation_entity_insert | Implements hook_entity_insert(). |
device_geolocation_page_attachments | Implements hook_page_attachments(). |
device_geolocation_theme | Implements hook_theme(). |
device_geolocation_user_login | Implements hook_user_login(). |
template_preprocess_device_geolocation_visitor_info | Prepares variables for visitor's geolocation block templates. |