You are here

class SmartIpEventSubscriber in Smart IP 8.4

Same name in this branch
  1. 8.4 modules/device_geolocation/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\device_geolocation\EventSubscriber\SmartIpEventSubscriber
  2. 8.4 modules/smart_ip_maxmind_geoip2_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_maxmind_geoip2_bin_db\EventSubscriber\SmartIpEventSubscriber
  3. 8.4 modules/smart_ip_ipinfodb_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ipinfodb_web_service\EventSubscriber\SmartIpEventSubscriber
  4. 8.4 modules/smart_ip_ip2location_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ip2location_bin_db\EventSubscriber\SmartIpEventSubscriber
  5. 8.4 modules/smart_ip_abstract_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_abstract_web_service\EventSubscriber\SmartIpEventSubscriber
  6. 8.4 modules/smart_ip_maxmind_geoip2_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_maxmind_geoip2_web_service\EventSubscriber\SmartIpEventSubscriber
Same name and namespace in other branches
  1. 8.3 modules/device_geolocation/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\device_geolocation\EventSubscriber\SmartIpEventSubscriber

Core functionality of this Smart IP data source module. Listens to Smart IP override events.

@package Drupal\device_geolocation\EventSubscriber

Hierarchy

Expanded class hierarchy of SmartIpEventSubscriber

2 files declare their use of SmartIpEventSubscriber
DeviceGeolocation.php in modules/device_geolocation/src/DeviceGeolocation.php
Contains \Drupal\device_geolocation\DeviceGeolocation.
device_geolocation.module in modules/device_geolocation/device_geolocation.module
Provides visitor's geographical location using client device location source that implements W3C Geolocation API and Google Geocoding service.
1 string reference to 'SmartIpEventSubscriber'
device_geolocation.services.yml in modules/device_geolocation/device_geolocation.services.yml
modules/device_geolocation/device_geolocation.services.yml
1 service uses SmartIpEventSubscriber
device_geolocation.smart_ip_event_subscriber in modules/device_geolocation/device_geolocation.services.yml
Drupal\device_geolocation\EventSubscriber\SmartIpEventSubscriber

File

modules/device_geolocation/src/EventSubscriber/SmartIpEventSubscriber.php, line 23
Contains \Drupal\device_geolocation\EventSubscriber\SmartIpEventSubscriber.

Namespace

Drupal\device_geolocation\EventSubscriber
View source
class SmartIpEventSubscriber extends SmartIpEventSubscriberBase {

  /**
   * {@inheritdoc}
   */
  public static function sourceId() {
    return 'device_geolocation';
  }

  /**
   * {@inheritdoc}
   */
  public static function configName() {
    return 'device_geolocation.settings';
  }

  /**
   * {@inheritdoc}
   */
  public function formSettings(AdminSettingsEvent $event) {
    $config = \Drupal::config(self::configName());
    $form = $event
      ->getForm();
    $form['device_geolocation_preferences'] = [
      '#type' => 'fieldset',
      '#title' => t('Device Geolocation settings'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    ];
    $form['device_geolocation_preferences']['device_geolocation_use_ajax_check'] = [
      '#type' => 'checkbox',
      '#title' => t("Use AJAX in user's geolocation checking (useful if the site or pages \n        listed above are cached)"),
      '#default_value' => $config
        ->get('use_ajax_check'),
    ];
    $frequencyCheck = $config
      ->get('frequency_check');
    $form['device_geolocation_preferences']['device_geolocation_frequency_check'] = [
      '#title' => t("Frequency of user's geolocation checking"),
      '#type' => 'textfield',
      '#size' => 10,
      '#description' => t('Specify number of hours will prompt the user for geolocation. Leave it
        empty to disable.'),
      '#default_value' => $frequencyCheck === NULL ? '' : $frequencyCheck / 3600,
      '#field_suffix' => t('hours'),
    ];
    $form['device_geolocation_preferences']['device_geolocation_google_map_api_key'] = array(
      '#title' => t('Google map API key'),
      '#type' => 'textfield',
      '#description' => t('The use of Google map service requires API key. Get your API key @here.', [
        '@here' => Link::fromTextAndUrl(t('here'), Url::fromUri('https://developers.google.com/maps/documentation/javascript/get-api-key'))
          ->toString(),
      ]),
      '#default_value' => $config
        ->get('google_map_api_key'),
    );
    $form['device_geolocation_preferences']['device_geolocation_google_map_region'] = [
      '#type' => 'textfield',
      '#title' => t('Google map region'),
      '#default_value' => $config
        ->get('google_map_region'),
      '#description' => t("Specify a region code, which alters the Google map service's behavior based \n        on a given country or territory. See @google_localization_region", [
        '@google_localization_region' => Link::fromTextAndUrl(t('Google Maps API - Localizing the Map (Region localization)'), Url::fromUri('https://developers.google.com/maps/documentation/javascript/localization#Region'))
          ->toString(),
      ]),
    ];
    $form['device_geolocation_preferences']['device_geolocation_google_map_language'] = [
      '#type' => 'textfield',
      '#title' => t('Google map localization'),
      '#default_value' => $config
        ->get('google_map_language'),
      '#description' => t('Change the Google map service default language settings.
        See @google_localization_language', [
        '@google_localization_language' => Link::fromTextAndUrl(t('Google Maps API - Localizing the Map (Language localization)'), Url::fromUri('https://developers.google.com/maps/documentation/javascript/localization#Language'))
          ->toString(),
      ]),
    ];
    $event
      ->setForm($form);
  }

  /**
   * {@inheritdoc}
   */
  public function validateFormSettings(AdminSettingsEvent $event) {

    /** @var \Drupal\Core\Form\FormStateInterface $formState */
    $formState = $event
      ->getFormState();
    $frequencyCheck = $formState
      ->getValue('device_geolocation_frequency_check');
    if ($frequencyCheck != '' && !is_numeric($frequencyCheck) || $frequencyCheck < 0) {
      $formState
        ->setErrorByName('device_geolocation_frequency_check', t("Frequency of user's geolocation checking must be a positive number."));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitFormSettings(AdminSettingsEvent $event) {
    $config = \Drupal::configFactory()
      ->getEditable(self::configName());

    /** @var \Drupal\Core\Form\FormStateInterface $formState */
    $formState = $event
      ->getFormState();
    $frequencyCheck = $formState
      ->getValue('device_geolocation_frequency_check');
    if ($frequencyCheck == '') {
      $frequencyCheck = NULL;
    }
    else {

      // Convert from hours to seconds.
      $frequencyCheck = $frequencyCheck * 3600;
    }
    $config
      ->set('use_ajax_check', $formState
      ->getValue('device_geolocation_use_ajax_check'))
      ->set('frequency_check', $frequencyCheck)
      ->set('google_map_api_key', $formState
      ->getValue('device_geolocation_google_map_api_key'))
      ->set('google_map_region', $formState
      ->getValue('device_geolocation_google_map_region'))
      ->set('google_map_language', $formState
      ->getValue('device_geolocation_google_map_language'))
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function processQuery(GetLocationEvent $event) {
  }

  /**
   * {@inheritdoc}
   */
  public function manualUpdate(DatabaseFileEvent $event) {
  }

  /**
   * {@inheritdoc}
   */
  public function cronRun(DatabaseFileEvent $event) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SmartIpEventSubscriber::configName public static function Get the config name of this Smart IP data source module. Overrides SmartIpDataSourceInterface::configName
SmartIpEventSubscriber::cronRun public function Act on Drupal cron run. Overrides SmartIpDataSourceInterface::cronRun
SmartIpEventSubscriber::formSettings public function Add the form elements of this Smart IP data source to main admin settings page of Smart IP. Overrides SmartIpDataSourceInterface::formSettings
SmartIpEventSubscriber::manualUpdate public function Act on manual database update. Overrides SmartIpDataSourceInterface::manualUpdate
SmartIpEventSubscriber::processQuery public function Act on \Drupal\smart_ip\SmartIp::query() when executed and if selected as Smart IP data source, query the IP address against its database. Overrides SmartIpDataSourceInterface::processQuery
SmartIpEventSubscriber::sourceId public static function Smart IP data source module's source ID. Overrides SmartIpDataSourceInterface::sourceId
SmartIpEventSubscriber::submitFormSettings public function Act on submission of main Smart IP admin settings form. Overrides SmartIpDataSourceInterface::submitFormSettings
SmartIpEventSubscriber::validateFormSettings public function Act on validation of main Smart IP admin settings form. Overrides SmartIpDataSourceInterface::validateFormSettings
SmartIpEventSubscriberBase::getSubscribedEvents static function Returns an array of event names this subscriber wants to listen to.
SmartIpEventSubscriberBase::includeEditableConfigNames public function Add Smart IP source module's config name. Overrides SmartIpDataSourceInterface::includeEditableConfigNames