You are here

public function IpLookupForm::submitForm in IP Geolocation Views & Maps 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/IpLookupForm.php, line 70

Class

IpLookupForm
Pending doc.

Namespace

Drupal\ip_geoloc\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Migration comment:  Part of _ip_geoloc_process_go_to_submit for submiting ip_geoloc_set_location_form
  // $form_state->set(['storage', 'value'], 'something');.
  $store = !empty($form_state
    ->getStorage());
  $location = $this->ipGeolocApi
    ->getLocationByIp(trim($form_state
    ->get('ip_address'), TRUE, $store));
  $connection = \Drupal::database();

  // @TODO Acesslog table doesn't exist for d8
  if ($connection
    ->schema()
    ->tableExists('accesslog')) {
    $last_visit = $connection
      ->query('SELECT MAX(timestamp) FROM {accesslog} WHERE hostname = :ip_address', [
      ':ip_address' => $location['ip_address'],
    ])
      ->fetchField();
    if ($last_visit) {
      $last_visit = format_date($last_visit, 'short');
      $form_state
        ->set([
        'storage',
        'value',
      ], 'last_visit');
      $form_state
        ->set([
        'storage',
        $this
          ->t('<strong>Last visit:</strong> %date', [
          '%date' => $last_visit,
        ]),
      ], 'last_visit');
    }
  }
  $formatted_address = isset($location['formatted_address']) ? t('%ip: <strong>%address</strong>', [
    '%ip' => $location['ip_address'],
    '%address' => $location['formatted_address'],
  ]) : '';
  $form_state
    ->set([
    'storage',
    $formatted_address,
  ], 'formatted_address');

  // To preserve entered values and storage array.
  $form_state
    ->setRebuild();
}