class SmartIpAdminSettingsForm in Smart IP 8.2
Same name and namespace in other branches
- 8.4 src/Form/SmartIpAdminSettingsForm.php \Drupal\smart_ip\Form\SmartIpAdminSettingsForm
- 8.3 src/Form/SmartIpAdminSettingsForm.php \Drupal\smart_ip\Form\SmartIpAdminSettingsForm
Smart IP main admin settings page.
@package Drupal\smart_ip\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\smart_ip\Form\SmartIpAdminSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SmartIpAdminSettingsForm
1 string reference to 'SmartIpAdminSettingsForm'
File
- src/
Form/ SmartIpAdminSettingsForm.php, line 25 - Contains \Drupal\smart_ip\Form\SmartIpAdminSettingsForm.
Namespace
Drupal\smart_ip\FormView source
class SmartIpAdminSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'smart_ip_admin_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
$configNames = [
'smart_ip.settings',
];
/** @var \Drupal\smart_ip\AdminSettingsEvent $event */
$event = \Drupal::service('smart_ip.admin_settings_event');
// Allow Smart IP source module to add their config names
$event
->setEditableConfigNames($configNames);
\Drupal::service('event_dispatcher')
->dispatch(SmartIpEvents::GET_CONFIG_NAME, $event);
$configNames = $event
->getEditableConfigNames();
return $configNames;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('smart_ip.settings');
$dataSource = $config
->get('data_source');
if (!empty($dataSource)) {
$errorSourceId = \Drupal::state()
->get('smart_ip.request_db_error_source_id') ?: '';
if (!empty($errorSourceId)) {
// Container for update status and manual update
$form['smart_ip_bin_database_update'] = array(
'#type' => 'fieldset',
'#title' => t('Database Update Status'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#states' => array(
'visible' => array(
':input[name="smart_ip_source"]' => array(
'value' => $errorSourceId,
),
),
),
);
$message = \Drupal::state()
->get('smart_ip.request_db_error_message') ?: '';
if (!empty($message)) {
$message = "<div class='messages messages--error'>{$message}</div>";
}
$form['smart_ip_bin_database_update']['smart_ip_bin_update_database'] = array(
'#type' => 'submit',
'#value' => t('Update database now'),
'#submit' => array(
array(
get_class($this),
'manualUpdate',
),
),
'#prefix' => $message,
);
}
// Container for manual lookup
$form['smart_ip_manual_lookup'] = array(
'#type' => 'fieldset',
'#title' => $this
->t('Manual lookup'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['smart_ip_manual_lookup']['smart_ip_lookup'] = array(
'#type' => 'textfield',
'#title' => $this
->t('IP address'),
'#description' => $this
->t('An IP address may be looked up by entering the address above then pressing the %lookup button below.', array(
'%lookup' => t('Lookup'),
)),
);
$storage = $form_state
->getStorage();
$lookupResponse = isset($storage['smart_ip_message']) ? $storage['smart_ip_message'] : '';
$form['smart_ip_manual_lookup']['smart_ip_lookup_button'] = array(
'#type' => 'submit',
'#value' => $this
->t('Lookup'),
'#submit' => array(
array(
get_class($this),
'manualLookup',
),
),
'#ajax' => array(
'callback' => array(
get_class($this),
'manualLookupAjax',
),
'effect' => 'fade',
),
'#suffix' => '<div id="smart-ip-location-manual-lookup">' . $lookupResponse . '</div>',
);
}
// Container for Smart IP source
$form['smart_ip_data_source_selection'] = array(
'#type' => 'fieldset',
'#title' => $this
->t('Smart IP source'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
// Smart IP source selection
$form['smart_ip_data_source_selection']['smart_ip_data_source'] = array(
'#type' => 'radios',
'#title' => $this
->t('Select Smart IP data source'),
'#options' => array(),
'#default_value' => $dataSource,
);
/** @var \Drupal\smart_ip\AdminSettingsEvent $event */
$event = \Drupal::service('smart_ip.admin_settings_event');
// Allow Smart IP source module to add their form elements
$event
->setForm($form);
$event
->setFormState($form_state);
\Drupal::service('event_dispatcher')
->dispatch(SmartIpEvents::DISPLAY_SETTINGS, $event);
$form = $event
->getForm();
$form_state = $event
->getFormState();
if (empty($form['smart_ip_data_source_selection']['smart_ip_data_source']['#options'])) {
// No Smart IP data source module enabled
$form['smart_ip_data_source_selection']['smart_ip_data_source'] = array(
'#markup' => $this
->t('You do not have any Smart IP data source module enabled. Please
enable at least one @here.', array(
'@here' => Link::fromTextAndUrl($this
->t('here'), Url::fromRoute('system.modules_list', [], [
'fragment' => 'edit-modules-smart-ip-data-source',
]))
->toString(),
)),
);
}
// Container for Smart IP preference
$form['smart_ip_preferences'] = array(
'#type' => 'fieldset',
'#title' => $this
->t('Smart IP settings'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$userRoles = user_roles();
$roles = array();
foreach ($userRoles as $role_id => $role) {
$roles[$role_id] = $role
->label();
}
$form['smart_ip_preferences']['smart_ip_roles_to_geolocate'] = array(
'#type' => 'checkboxes',
'#title' => $this
->t('Roles To Geolocate'),
'#default_value' => $config
->get('roles_to_geolocate'),
'#options' => $roles,
'#description' => $this
->t('Select the roles you wish to geolocate. Note that selecting the
anonymous role will add substantial overhead.'),
);
$form['smart_ip_preferences']['smart_ip_save_user_location_creation'] = array(
'#type' => 'checkbox',
'#title' => $this
->t("Save user's location details upon creation"),
'#default_value' => $config
->get('save_user_location_creation'),
'#description' => $this
->t("One time storing of user's location details upon registration."),
);
$form['smart_ip_preferences']['smart_ip_allowed_pages'] = array(
'#title' => $this
->t("Acquire/update user's geolocation on specific Drupal native pages"),
'#type' => 'textarea',
'#rows' => 5,
'#description' => $this
->t("Specify pages by using their paths. Enter one path per line. The '*' \n character is a wildcard. Example paths are %user for the current user's \n page and %user-wildcard for every user page. %front is the front page. \n Leave blank if all pages.", array(
'%user' => '/user',
'%user-wildcard' => '/user/*',
'%front' => '<front>',
)),
'#default_value' => $config
->get('allowed_pages'),
);
// Container for Smart IP debug tool
$form['smart_ip_debug_tool'] = array(
'#type' => 'fieldset',
'#title' => $this
->t('Smart IP debug tool'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['smart_ip_debug_tool']['smart_ip_debug'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Admin debug'),
'#default_value' => $config
->get('debug_mode'),
'#description' => $this
->t('Enables administrator to spoof an IP Address for debugging purposes.'),
);
$form['smart_ip_debug_tool']['smart_ip_test_ip_address'] = array(
'#type' => 'textfield',
'#title' => $this
->t('IP address to use for testing'),
'#default_value' => $config
->get('debug_mode_ip'),
);
return parent::buildForm($form, $form_state);
}
/**
* Triggers manual database update event to Smart IP data source module
* listeners.
*/
public static function manualUpdate() {
/** @var \Drupal\smart_ip\DatabaseFileEvent $event */
$event = \Drupal::service('smart_ip.database_file_event');
// Allow Smart IP source module to act on manual database update
\Drupal::service('event_dispatcher')
->dispatch(SmartIpEvents::MANUAL_UPDATE, $event);
}
/**
* Submit handler to lookup an IP address in the database.
*
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @see \Drupal\smart_ip\Form\SmartIpAdminSettingsForm::manualLookupAjax
*/
public static function manualLookup(array $form, FormStateInterface $form_state) {
$ip = $form_state
->getValue('smart_ip_lookup');
$location = SmartIp::query($ip);
if (isset($location['latitude']) && isset($location['longitude'])) {
$message = '<p>' . t('IP Address @ip is assigned to the following location details:', array(
'@ip' => $ip,
)) . '</p>' . '<dl>' . '<dt>' . t('Country:') . '</dt>' . '<dd>' . t('%country', array(
'%country' => $location['country'],
)) . '</dd>' . '<dt>' . t('Country code:') . '</dt>' . '<dd>' . t('%country_code', array(
'%country_code' => $location['countryCode'],
)) . '</dd>' . '<dt>' . t('Region:') . '</dt>' . '<dd>' . t('%region', array(
'%region' => $location['region'],
)) . '</dd>' . '<dt>' . t('City:') . '</dt>' . '<dd>' . t('%city', array(
'%city' => $location['city'],
)) . '</dd>' . '<dt>' . t('Postal code:') . '</dt>' . '<dd>' . t('%zip', array(
'%zip' => $location['zip'],
)) . '</dd>' . '<dt>' . t('Latitude:') . '</dt>' . '<dd>' . t('%latitude', array(
'%latitude' => $location['latitude'],
)) . '</dd>' . '<dt>' . t('Longitude:') . '</dt>' . '<dd>' . t('%longitude', array(
'%longitude' => $location['longitude'],
)) . '</dd>' . '<dt>' . t('Time zone:') . '</dt>' . '<dd>' . t('%time_zone', array(
'%time_zone' => $location['timeZone'],
)) . '</dd>' . '</dl>';
}
else {
$message = t('IP Address @ip is not assigned to any location.', array(
'@ip' => $ip,
));
}
$storage['smart_ip_message'] = $message;
$form_state
->setStorage($storage);
$form_state
->setRebuild();
}
/**
* Submit handler to lookup an IP address in the database.
*
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @return \Drupal\Core\Ajax\AjaxResponse
* @see \Drupal\smart_ip\Form\SmartIpAdminSettingsForm::manualLookup
*/
public static function manualLookupAjax(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$storage = $form_state
->getStorage();
$value = isset($storage['smart_ip_message']) ? $storage['smart_ip_message'] : '';
$response
->addCommand(new HtmlCommand('#smart-ip-location-manual-lookup', $value));
return $response;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('smart_ip_debug') == TRUE && $form_state
->isValueEmpty('smart_ip_test_ip_address')) {
$form_state
->setErrorByName('smart_ip_test_ip_address', $this
->t('Please enter the IP address to use for testing.'));
}
if (!empty($form['smart_ip_data_source_selection']['smart_ip_data_source']['#options']) && empty($form_state
->getValue('smart_ip_data_source'))) {
$form_state
->setErrorByName('smart_ip_data_source', $this
->t('Please select a Smart IP data source.'));
}
/** @var \Drupal\smart_ip\AdminSettingsEvent $event */
$event = \Drupal::service('smart_ip.admin_settings_event');
// Allow Smart IP source module to add validation on their form elements
$event
->setForm($form);
$event
->setFormState($form_state);
\Drupal::service('event_dispatcher')
->dispatch(SmartIpEvents::VALIDATE_SETTINGS, $event);
$form = $event
->getForm();
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$debugMode = $form_state
->getValue('smart_ip_debug');
if ($debugMode) {
$ip = $form_state
->getValue('smart_ip_test_ip_address');
$type = $this
->t('debug');
}
else {
$ip = \Drupal::request()
->getClientIp();
$type = $this
->t('actual');
}
$location = \Drupal\smart_ip\SmartIp::query($ip);
if (isset($location['latitude']) && isset($location['longitude'])) {
drupal_set_message($this
->t('Using @type IP: %ip / Country: %country / Region: %region / City: %city / Postal code: %zip / Longitude: %long / Latitude: %lat / Time zone: %time_zone', array(
'@type' => $type,
'%ip' => $location['ipAddress'],
'%country' => $location['country'],
'%region' => $location['region'],
'%city' => $location['city'],
'%zip' => $location['zip'],
'%long' => $location['longitude'],
'%lat' => $location['latitude'],
'%time_zone' => $location['timeZone'],
)));
}
\Drupal::service('smart_ip.smart_ip_location')
->save();
$this
->config('smart_ip.settings')
->set('data_source', $form_state
->getValue('smart_ip_data_source'))
->set('roles_to_geolocate', $form_state
->getValue('smart_ip_roles_to_geolocate'))
->set('save_user_location_creation', $form_state
->getValue('smart_ip_save_user_location_creation'))
->set('debug_mode', $debugMode)
->set('debug_mode_ip', $form_state
->getValue('smart_ip_test_ip_address'))
->set('allowed_pages', $form_state
->getValue('smart_ip_allowed_pages'))
->save();
/** @var \Drupal\smart_ip\AdminSettingsEvent $event */
$event = \Drupal::service('smart_ip.admin_settings_event');
// Allow Smart IP source module to add validation on their form elements
$event
->setForm($form);
$event
->setFormState($form_state);
\Drupal::service('event_dispatcher')
->dispatch(SmartIpEvents::SUBMIT_SETTINGS, $event);
$form = $event
->getForm();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SmartIpAdminSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SmartIpAdminSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SmartIpAdminSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SmartIpAdminSettingsForm:: |
public static | function | Submit handler to lookup an IP address in the database. | |
SmartIpAdminSettingsForm:: |
public static | function | Submit handler to lookup an IP address in the database. | |
SmartIpAdminSettingsForm:: |
public static | function | Triggers manual database update event to Smart IP data source module listeners. | |
SmartIpAdminSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SmartIpAdminSettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |