You are here

public function IpGeoLocAPI::getCurrentLocation in IP Geolocation Views & Maps 8

Uses AJAX to return in $_POST info about the visitor's current location.

Note: this function will result in the browser prompting the visitor to share their location, which they may or may not accept.

This works via an asynchronous javascript call, so the result is not immediately available on return from this function, hence the $menu_callback. Upon page load the included javascript will, when ready, instigate an AJAX call to the $menu_callback, which should invoke a function to pull the lat/long and address values out of the $_POST variable. See ip_geoloc_current_location_ajax_recipient() for an example.

Note: will result in a HTTP error 503 when the site is in maintenance mode, as in maintenance mode menu items are not available.

File

src/Services/IpGeoLocAPI.php, line 379

Class

IpGeoLocAPI
Class IpGeoAPI to interact with other modules.

Namespace

Drupal\ip_geoloc\Services

Code

public function getCurrentLocation($menu_callback = NULL, $reverse_geocode = NULL, $refresh_page = NULL) {
  $config = \Drupal::config('ip_geoloc.settings');
  $throbber_text = $this->config
    ->get('ip_geoloc_throbber_text2');
  if (empty($throbber_text)) {
    $throbber_text = IP_GEOLOC_THROBBER_DEFAULT_TEXT;
  }
  if ($throbber_text != '<none>') {
    $this->messenger
      ->addMessage(Xss::filterAdmin($throbber_text));
  }
  $this->ipGeolocSession
    ->setSessionValue('last_position_check', time());
  $this->ipGeolocSession
    ->setSessionValue('position_pending_since', microtime(TRUE));
  if (!isset($menu_callback)) {
    $base_url = \Drupal::request()
      ->getSchemeAndHttpHost();

    // @TODO review this as the clean url variable was removed
    // $menu_callback = "$base_url/" . (\Drupal::state()->get('clean_url', 0) ? 'js/ip_geoloc/current_location' : '?q=js/ip_geoloc/current_location');
    $menu_callback = "{$base_url}/" . 'js/ip_geoloc/current_location';
  }
  if (!isset($reverse_geocode)) {
    $reverse_geocode = $this->config
      ->get('ip_geoloc_visitor_reverse_geocode') ? $this->config
      ->get('ip_geoloc_visitor_reverse_geocode') : TRUE;
  }
  if (!isset($refresh_page)) {
    $refresh_page = $this->config
      ->get('ip_geoloc_page_refresh') ? $this->config
      ->get('ip_geoloc_visitor_reverse_geocode') : TRUE && !path_is_admin($_GET['q']);
  }
  $settings = [
    'ip_geoloc_menu_callback' => $menu_callback,
    'ip_geoloc_refresh_page' => $refresh_page,
    'ip_geoloc_reverse_geocode' => $reverse_geocode,
  ];
  return $settings;
}