You are here

function ip_geoloc_page_attachments in IP Geolocation Views & Maps 8

Pending doc.

File

./ip_geoloc.module, line 208
IPGV&M is a mapping engine for Views that contain locations of entities and/or visitors. Google Maps, Leaflet and OpenLayers2 maps are all supported and available through this module. Using a number of optional sources IPGV&M also retrieves…

Code

function ip_geoloc_page_attachments(array &$attachments) {
  $api = \Drupal::service('ip_geoloc.api');
  $geoloc_global = \Drupal::service('ip_geoloc.global');
  $location = $api
    ->getVisitorLocation();
  $scheduled_reverse_geocode = $geoloc_global
    ->checkLocation($location);

  // 2nd condition is to avoid HTTP 503 error.
  if ($scheduled_reverse_geocode && !\Drupal::state()
    ->get('system.maintenance_mode')) {

    // Insert some javascript to first retrieve the user's lat/lon coords,
    // HTML5 style (requiring the user to accept a browser prompt) and then
    // optionally (default) use Google Maps API to reverse-geocode the lat/lon
    // into a street address.
    // This is all done via client-side calls, so the Drupal server will not
    // rake up any calls against its Google-imposed quotum, i.e. the
    // OVER_QUERY_LIMIT.
    // When done, the javascript calls us back on the default menu callback,
    // '/js/ip_geoloc/current_location', which receives the geolocation data
    // from the Google Maps call via the $_POST variable and stores it on the
    // session.
    // Naturally all of this will only work if the browser is connected to
    // the internet and has javascript enabled.
    // See also: _ip_geoloc_process_find_me_ajax().
    $geoloc_session = \Drupal::service('ip_geoloc.session');
    $settings = $api
      ->getCurrentLocation();
    $attachments['#attached']['library'][] = 'ip_geoloc/current_location_js';
    $attachments['#attached']['drupalSettings'] = $settings;
    $geoloc_session
      ->setSessionValue('last_position_check', time());
  }
}