You are here

public function IpGeoLocGlobal::useGeoipApiIfEnabled in IP Geolocation Views & Maps 8

Module GeoIP API does not expose a hook, but it does expose an API.

Parameters

array $location: Ff $location['ip_address'] isn't filled out the current user's IP address will be used.

Return value

bool TRUE upon success, FALSE otherwise.

1 call to IpGeoLocGlobal::useGeoipApiIfEnabled()
IpGeoLocGlobal::reinitLocation in src/Services/IpGeoLocGlobal.php
Reinitialises the supplied location array.

File

src/Services/IpGeoLocGlobal.php, line 137

Class

IpGeoLocGlobal
Class IpGeoLocGlobal.

Namespace

Drupal\ip_geoloc\Services

Code

public function useGeoipApiIfEnabled(array &$location) {
  if (!function_exists('geoip_city')) {
    return FALSE;
  }
  $location['provider'] = 'geoip';
  if (empty($location['ip_address'])) {
    $location['ip_address'] = ip_address();
  }
  $geoip_location = (array) geoip_city($location['ip_address']);
  if (reset($geoip_location)) {

    // Where different, convert GeoIP names to our equivalents.
    $geoip_location['country'] = isset($geoip_location['country_name']) ? $geoip_location['country_name'] : '';
    unset($geoip_location['country_name']);
    $location = array_merge($geoip_location, $location);
    ip_geoloc_format_address($location);
  }
  $this
    ->debug($this->stringTranslation
    ->translate('IPGV&M: GeoIP API retrieved: !location', [
    '!location' => ip_geoloc_pretty_print($location),
  ]));
  return TRUE;
}