Ashoka Tree

Inspiring to share!

Drupal 8: Show content based on website visitor’s country or content Localization – Personalization.

Sometimes we need localization of contents i.e if a website visitor is browsing site from USA will be shown different content then someone is browsing from India or Germany.
So our aim is to show Content based on Visitor’s Country, Geo IP, Geo Location.

To do so we’ll extend the required content type with a filed say Country. While creating a new content there it will appear a Select Box to choose country as an Option. If this content is set to a country then it’ll be available for that country only otherwise it’ll be available for all countries.

Here is the technical guidance towards the solution.
Drupal Modules required

  • Country
  • Composer Manager
  •  Address
  • GeoIP

Regarding enabling those modules within Drupal please refer my another blog as “Drupal 8, Modules, Composer JSON, Platform.sh”.
For example i’ve created one content type as “Country Based Content” with fields Title, Body, Country.
Where Country is field type Country.
Also created some contents of the above content type.

Created a view as “country” to show this content type in the Front-end of the website through view Module.
Its very easy but still i am attaching a screen-shot for beginners.

Usage of GeoIP Module :
GeoIP Module tells Drupal about the Country location of the visitor of the site based on Visitor’s
IP Address.
To explain it more technically,
GeoIP Module register a service as “geoip.geolocation” via its Constructor by following Drupal
Service registration standard.
We need to trace the website visitor’s IP v4 address through our PHP code and have to pass
this IP Address to GeoIP Module.
We use this code to trace the IP Address.
$visitorIp = \Drupal::request()->getClientIp();
As per my previous state we need to call the service:
\Drupal::service(‘geoip.geolocation’)->geolocate(‘14.140.162.242’);
So here is the output below:
CommerceGuys\Intl\Country\Country Object
(
[countryCode:protected] => IN
[name:protected] => India
[threeLetterCode:protected] => IND
[numericCode:protected] => 356
[currencyCode:protected] => INR
[locale:protected] => en
)
Now we’ll access property of Country Object from its Data Model.
We need to get Country Code value from countryCode” property. So this code below will return
“IN” as my country code.
\Drupal::service(‘geoip.geolocation’)->geolocate($visitorIp)->getCountryCode();

localization of Content for Website Visitors:
As i am browsing this site from from India so it’ll display only contents belong to India.
Let’s explain a little bit in coding level.
To alter the view we’ve used hook_views_query_alter()
Where we’ve traced the view id to alter.
function geoip_content_views_query_alter(Drupal\views\ViewExecutable $view,
Drupal\views\Plugin\views\query\Sql $query) {
switch ($view->id()) {
case ‘country’:
$data = ‘IN’;
$query->where[2][‘conditions’][1][‘value’] = [$data];
break;
}
}

Powered by WordPress