You are here

function smart_ip_page_attachments in Smart IP 8.2

Same name and namespace in other branches
  1. 8.4 smart_ip.module \smart_ip_page_attachments()
  2. 8.3 smart_ip.module \smart_ip_page_attachments()

Implements hook_page_attachments().

File

./smart_ip.module, line 124
Determines country, geo location (longitude/latitude), region, city and postal code of the user, based on IP address

Code

function smart_ip_page_attachments(array &$page) {
  if (!\Drupal\smart_ip\SmartIp::checkAllowedPage()) {

    // This page is not on the list to access user's geolocation
    return;
  }

  /** @var \Drupal\smart_ip\SmartIpLocation $location */
  $location = \Drupal::service('smart_ip.smart_ip_location');
  $data['location'] = $location
    ->getData();
  $value = \Drupal\Component\Serialization\Json::encode($data);

  // Make user geolocation available to javascript
  $page['#attached']['html_head'][] = [
    [
      '#tag' => 'script',
      '#value' => "var smartIp = {$value}",
    ],
    'smart_ip_location_javascript_access',
  ];
}