Skip to content
Snippets Groups Projects
Commit 80259639 authored by Bálint Junkuncz's avatar Bálint Junkuncz Committed by Sven Berg Ryen
Browse files

Issue #3068704 by junkuncz: Support GeoIP module in Drupal 8

parent a3ad1362
No related branches found
Tags 8.x-2.0-alpha7
No related merge requests found
......@@ -14,7 +14,6 @@ use Drupal\Core\Url;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\Utility\Html;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\smart_ip\SmartIp;
use Drupal\Core\Database\Database;
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Component\Serialization\Json;
......@@ -582,12 +581,21 @@ function eu_cookie_compliance_user_in_eu() {
$ip_address = Drupal::request()->getClientIp();
// Try to get country_code by php extension.
$country_code = extension_loaded('geoip') ? geoip_country_code_by_name($ip_address) : '';
// Try to get country_code by smart_ip module.
if (Drupal::moduleHandler()->moduleExists('smart_ip')) {
$location_service = \Drupal::service('smart_ip.smart_ip_location');
$smart_ip_session = $location_service->get();
$country_code = isset($smart_ip_session['countryCode']) ? $smart_ip_session['countryCode'] : NULL;
}
// Try to get country_code by geoip module.
elseif (Drupal::moduleHandler()->moduleExists('geoip')) {
$geo_location_service = \Drupal::service('geoip.geolocation');
$geo_ip_session = $geo_location_service->geolocate($ip_address);
$country_code = !empty($geo_ip_session) ? $geo_ip_session : NULL;
}
// If the CloudFlare provided country header is available, use it as a
// fallback. See:
......
......@@ -734,12 +734,12 @@ class EuCookieComplianceConfigForm extends ConfigFormBase {
'#title' => t('EU countries'),
];
if ($this->moduleHandler->moduleExists('smart_ip') || extension_loaded('geoip')) {
if ($this->moduleHandler->moduleExists('smart_ip') || $this->moduleHandler->moduleExists('geoip') || extension_loaded('geoip')) {
$form['eu_only']['eu_only'] = [
'#type' => 'checkbox',
'#title' => $this->t('Only display banner in EU countries.'),
'#default_value' => !empty($config->get('eu_only')) ? $config->get('eu_only') : 0,
'#description' => $this->t('You can limit the number of countries for which the banner is displayed by checking this option. If you want to provide a list of countries other than current EU states, you may place an array in <code>$config[\'eu_cookie_compliance.settings\'][\'eu_countries\']</code> in your <code>settings.php</code> file. Using the <a href="http://drupal.org/project/smart_ip">smart_ip</a> module or the <a href="http://www.php.net/manual/en/function.geoip-country-code-by-name.php">geoip_country_code_by_name()</a> PHP function.'),
'#description' => $this->t('You can limit the number of countries for which the banner is displayed by checking this option. If you want to provide a list of countries other than current EU states, you may place an array in <code>$config[\'eu_cookie_compliance.settings\'][\'eu_countries\']</code> in your <code>settings.php</code> file. Using the <a href="http://drupal.org/project/smart_ip">smart_ip</a> module or using the <a href="http://drupal.org/project/geoip">geoip</a> module or the <a href="http://www.php.net/manual/en/function.geoip-country-code-by-name.php">geoip_country_code_by_name()</a> PHP function.'),
];
$form['eu_only']['eu_only_js'] = [
'#type' => 'checkbox',
......@@ -750,7 +750,7 @@ class EuCookieComplianceConfigForm extends ConfigFormBase {
}
else {
$form['eu_only']['info'] = [
'#markup' => t('You can choose to show the banner only to visitors from EU countries. In order to achieve this, you need to install the <a href="http://drupal.org/project/smart_ip">smart_ip</a> module or enable the <a href="http://www.php.net/manual/en/function.geoip-country-code-by-name.php">geoip_country_code_by_name()</a> PHP function.'),
'#markup' => t('You can choose to show the banner only to visitors from EU countries. In order to achieve this, you need to install the <a href="http://drupal.org/project/smart_ip">smart_ip</a> module or install the <a href="http://drupal.org/project/geoip">geoip</a> module or enable the <a href="http://www.php.net/manual/en/function.geoip-country-code-by-name.php">geoip_country_code_by_name()</a> PHP function.'),
];
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment