Drupal

Influence the Drupal site's suggestion list of search autocomplete module with Smart IP views country filter

This is an example application of Smart IP where it influence the suggestion shown in the list of search autocomplete based on the country location of the visitor in a Drupal 7 site with the use of Smart IP, Search Autocomplete and Location contributed modules. Basically, the search autocomplete will display list of suggestions but the list of nodes will be filtered based on country location of the visitor and nodes that have an empty value in Location country field will not be filtered.

Smart IP Drupal 7 settings explained

The following is the explanation of each Smart IP settings in Drupal 7.

Database Update Status

This Smart IP section setting will only appear if your Data Source is either MaxMind GeoIP Legacy binary database or MaxMind GeoIP2 binary database or Smart IP database (Maxmind's CSV). It display the status of database update process from manually executed or automatically during cron run. If the database update failed, you can manually force the database update by clicking the "Update database now" button.

Display specific Drupal contents based on your visitor's country location in Views (using Smart IP and Location modules) and display all contents if there's no result

This tutorial will show how to setup a View that shows list of contents in a Drupal 7 site that are filtered based on your visitor's country location with the use of Smart IP and Location contributed modules and if there is no result, the fall back is to display all contents. Smart IP module is used to identify Drupal site visitor's geographical location based on his IP address and Location module is used to provide the field and expose views location filters.

Filter Drupal contents based on your visitor's city location in Views using Smart IP and Location modules

This guide will show how to setup a View that shows list of contents in a Drupal 7 site that are filtered based on your visitor's city location with the use of Smart IP and Location contributed modules. Smart IP module is used to identify Drupal site visitor's geographical location based on his IP address and Location module is used to provide the field and expose views location filters.

Steps:

The following steps will list all the nodes with city location field stored content same as the value of my city detected by Smart IP.

Adding external javascript library available to all pages with query string value came from PHP in Drupal 8

In Drupal 7, adding external javascript library available to all pages with query string value came either from PHP computed value or from database this can be done by:

  
/**
 * Implements hook_init()
 */
function mymodule_init() {
  drupal_add_js('//maps.googleapis.com/maps/api/js?key=' . variable_get('mymodule_google_map_api_key', NULL), 'external');
}
  

For Drupal 8 this is possible by:

Installing geoip2 required library for Smart IP's data source MaxMind GeoIP2 binary database

The MaxMind GeoIP2 binary database is a custom binary format and there are two available APIs that read this binary database: MaxMind DB Reader PHP API which includes an optional C extension that you may install to improve binary database lookup performance and the default is GeoIP2 PHP API. We will discuss how to install these two APIs in your system.

Passing data from PHP to javascript variable available to all pages in Drupal 8

Most of us pass data from PHP to javascript variable available to all pages in Drupal 7 is by:

  
/**
 * Implements hook_init()
 */
function mymodule_init() {
  $computed_data = mymodule_get_data();
  drupal_add_js(array('mymodule_computed_data' => $computed_data), 'setting');
}
  

We can access this data in javascript by:

Setup Drupal 8 module using Composer

Drupal uses an externally-maintained repository the https://packagist.drupal-composer.org but this repo is scheduled to be deprecated in January 2017 by then we will be using https://packages.drupal.org/8 repo URL.

It is assumed that composer is already installed in your system. Make sure your composer version is 1.0.0 or higher, to check execute:

  
composer -V
  

If it is lower than version 1.0.0, update it by:

  
composer self-update
  

Change directory to your Drupal root path.

Installing ip2location-php required library for Smart IP's data source IP2Location binary database

Using Smart IP's data source IP2Location binary database requires ip2location/ip2location-php library which contains API that reads its database file. This article will show how to setup this API using composer.

The following procedures are tested on my Linode server running Centos 7 64-bit Linux distribution.

  1. First we will install composer: