View source
<?php
namespace Drupal\ip_geoloc\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\ip_geoloc\Services\IpGeoLocSession;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\ip_geoloc\Services\IpGeoLocAPI;
use Drupal\geocoder\Geocoder;
class GeoCodeAddressForm extends FormBase {
protected $messenger;
protected $moduleHandler;
protected $ipGeolocSession;
protected $configFactory;
protected $api;
protected $geocoder;
public function __construct(ConfigFactoryInterface $configFactory, MessengerInterface $messenger, ModuleHandler $moduleHandler, IpGeoLocSession $ipGeolocSession, IpGeoLocAPI $api, Geocoder $geocoder) {
$this->configFactory = $configFactory;
$this->messenger = $messenger;
$this->moduleHandler = $moduleHandler;
$this->ipGeolocSession = $ipGeolocSession;
$this->api = $api;
$this->geocoder = $geocoder;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('messenger'), $container
->get('moduleHandler'), $container
->get('ip_geoloc.session'), $container
->get('ip_geoloc.api'), $container
->get('geocoder'));
}
public function getFormId() {
return 'ip_geoloc_geocode_address';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->configFactory
->get('ip_geoloc.settings');
$location = $this->api
->getVisitorLocation();
$is_address_editable = $config
->get('ip_geoloc_visitor_address_editable') ? $config
->get('ip_geoloc_visitor_address_editable') : TRUE;
$form['street_address'] = [
'#type' => 'textfield',
'#title' => t('Current approximate address'),
'#default_value' => isset($location['formatted_address']) ? $location['formatted_address'] : '',
'#disabled' => !$is_address_editable,
];
if ($is_address_editable) {
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Refine'),
'#button_type' => 'primary',
];
}
$form['#attributes']['class'][] = 'ip-geoloc-address';
$form['#attached']['library'][] = 'ip_geoloc/client_css';
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this->moduleHandler
->moduleExists('geocoder')) {
$plugins = [
'googlemaps',
];
$address = $form_state
->getValue('street_address');
$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(),
];
$this->ipGeolocSession
->setSessionValue('location', $location);
}
$form_state
->setRebuild();
}
}