You are here

function smart_ip_entity_insert in Smart IP 8.2

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

Implements hook_entity_insert().

Save new user's geolocation upon registration.

File

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

Code

function smart_ip_entity_insert(EntityInterface $entity) {
  $is_new = $entity
    ->getCreatedTime() == REQUEST_TIME;
  if ($entity
    ->getEntityTypeId() == 'user' && $is_new) {
    $save_user_location = \Drupal::config('smart_ip.settings')
      ->get('save_user_location_creation');
    if ($save_user_location) {
      $data['location'] = SmartIp::query();

      /** @var \Drupal\user\UserData $userData */
      $userData = \Drupal::service('user.data');
      $userData
        ->set('smart_ip', $entity
        ->id(), 'geoip_location_original', $data);
    }
  }
}