You are here

function smart_ip_page_attachments in Smart IP 8.3

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

Implements hook_page_attachments().

File

./smart_ip.module, line 94
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(FALSE);
  $value = Json::encode($data);

  // Make user geolocation available to javascript via "drupalSettings.smartIp".
  $page['#attached']['drupalSettings']['smartIp'] = $value;
  $page['#attached']['drupalSettings']['smartIpSrc'] = [
    'smartIp' => SmartIpLocationInterface::SMART_IP,
    'geocodedSmartIp' => SmartIpLocationInterface::GEOCODED_SMART_IP,
    'w3c' => SmartIpLocationInterface::W3C,
  ];
}