You are here

function _ip_geoloc_get_field_value in IP Geolocation Views & Maps 8

Same name and namespace in other branches
  1. 7 plugins/ip_geoloc.statistics.inc \_ip_geoloc_get_field_value()

Better Statistics field callback for IP Geoloc.

Return a value to be inserted into the accesslog based on a database field name provided, for the currently stored location.

Parameters

string $db_field_name: The name of the database field for which to return data.

Return value

string|int|bool|float The data to be inserted into the accesslog for the provided field.

1 string reference to '_ip_geoloc_get_field_value'
ip_geoloc_better_statistics_fields in plugins/ip_geoloc.statistics.inc
Implements hook_better_statistics_fields().

File

plugins/ip_geoloc.statistics.inc, line 305
Capture IP Geoloc statistics in the access log.

Code

function _ip_geoloc_get_field_value($db_field_name) {

  // See [#1970180].
  static $geoloc;
  if (!isset($geoloc)) {

    // If caching, the module will not be loaded. In such case,
    // ip_geoloc_get_visitor_location() will be undefined, and an alternative
    // method to retrieve data should be used.
    if (function_exists('ip_geoloc_get_visitor_location')) {

      // Uses internal cache.
      $geoloc = ip_geoloc_get_visitor_location();
    }
    else {

      // Include common.inc.
      require_once DRUPAL_ROOT . '/includes/common.inc';
      $list = system_list('module_enabled');

      // Include 'IP Geolocation Views & Maps' module file.
      require_once DRUPAL_ROOT . '/' . $list['ip_geoloc']->filename;

      // If 'Smart IP' module is enabled and set up as backup, include its
      // module file.
      if (\Drupal::state()
        ->get('ip_geoloc_smart_ip_as_backup', TRUE) && isset($list['smart_ip'])) {
        require_once DRUPAL_ROOT . '/' . $list['smart_ip']->filename;
      }

      // If 'GeoIP API' module is enabled, include its module file.
      if (isset($list['geoip'])) {
        require_once DRUPAL_ROOT . '/' . $list['geoip']->filename;
      }

      // Retrieve geolocation.
      // ip_geoloc_get_location_by_ip() will either fetch a stored geolocation
      // from {ip_geoloc}, or manage conversation with backup modules to
      // retrieve the information and store back to {ip_geoloc}.
      $geoloc = Drupal::service('ip_geoloc.api')->ipGeolocApi
        ->getLocationByIp(Drupal::request()
        ->getClientIp(), $resample = FALSE, $store = TRUE, $reverse_geocode = FALSE);
      if (!empty($geoloc) and (!isset($geoloc['provider']) or empty($geoloc['provider']))) {
        $geoloc['provider'] = 'ip_geoloc';
      }
    }
  }

  // No geolocation data available, return NULL.
  if (empty($geoloc)) {
    return NULL;
  }
  return _ip_geoloc_get_location_field_value($db_field_name, $geoloc);
}