You are here

public function SetLocationForm::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/SetLocationForm.php, line 114

Class

SetLocationForm
Peding 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.
  // Clear any pending location retrieval process that may be in process.
  $this->session
    ->setSessionValue('last_position_check', time());
  $this->session
    ->setSessionValue('position_pending_since', NULL);
  $geo_vocabulary_id = \Drupal::state()
    ->get('ip_geoloc_geo_vocabulary_id', 0);
  if ($form_state
    ->isValueEmpty('fixed_address')) {

    // Nothing slected. As 'Go' was pressed we need to choose 1 or 2.
    $form_state
      ->setValue('fixed_address', $geo_vocabulary_id ? '2' : '1');
  }
  if ($form_state
    ->getValue('fixed_address') == '1') {

    // Using new 'input' rather than current 'values'.
    $input = $form_state
      ->get('input');
    if (!empty($input['street_address'])) {
      $entered_address = Html::escape($input['street_address']);
    }
    else {
      $entered_address = NULL;
    }
    $location = ip_geoloc_set_location_from_address($entered_address);
  }
  elseif ($geo_vocabulary_id) {

    // "Region" selected.
    $location = ip_geoloc_set_location_from_region($form_state
      ->getValue('region'));
  }

  // Wipe old location before setting the new one (to avoid merging).
  $this->session
    ->setSessionValue('location', NULL);
  $this->session
    ->setSessionValue('location', $location);
  $redirect = \Drupal::state()
    ->get('ip_geoloc_address_redirect');
  if (!empty($redirect)) {
    $form_state
      ->set('redirect', $redirect);
  }

  // No need to remember the form state. It's all kept on the session.
  // Also, if set to TRUE, content is rendered before new location is set
  // and Region selector will be one step behind.
  $form_state
    ->setRebuild();
}