public function GeoCodeAddressForm::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/ GeoCodeAddressForm.php, line 119
Class
- GeoCodeAddressForm
- Form to reverse geocode a address to latitud and Longitude.
Namespace
Drupal\ip_geoloc\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
// Migration comment: Part of ip_geoloc_geocode for submiting ip_geoloc_set_location_form.
if ($this->moduleHandler
->moduleExists('geocoder')) {
// Use Google server-side API to retrieve lat/long from entered address.
$plugins = [
'googlemaps',
];
$address = $form_state
->getValue('street_address');
// Array of (ovverriding) options (@see Note* below)
$options = [
// Array of options.
'googlemaps' => [],
];
$addressCollection = $this->geocoder
->geocode($address, $plugins, $options);
$point = $addressCollection
->get(0);
if (!$point) {
$this->messenger
->addMessage($this
->t('The address you entered could not be geocoded to a location.'), 'warning');
return;
}
$location = [
'provider' => 'user/google',
'ip_address' => \Drupal::request()
->getClientIp(),
'latitude' => $point
->getLatitude(),
'longitude' => $point
->getLongitude(),
'country' => $point
->getCountry(),
'locality' => $point
->getLocality(),
'formatted_address' => $point
->getStreetName() . ' ' . $point
->getStreetNumber(),
];
// print_r($location);die();
// Flatten the point object into a straight location array.
/*foreach ($point->data['geocoder_address_components'] as $component) {
if (!empty($component->long_name)) {
$type = $component->types[0];
$location[$type] = $component->long_name;
if ($type == 'country' && !empty($component->short_name)) {
$location['country_code'] = $component->short_name;
}
}
}*/
$this->ipGeolocSession
->setSessionValue('location', $location);
}
// Form always uses location from session.
$form_state
->setRebuild();
}