View source
<?php
namespace Drupal\ip_geoloc\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Component\Utility\Html;
use Drupal\ip_geoloc\Services\IpGeoLocAPI;
use Drupal\ip_geoloc\Services\IpGeoLocSession;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
class SetLocationForm extends FormBase implements ContainerFactoryPluginInterface {
protected $messenger;
protected $api;
protected $session;
public function __construct(MessengerInterface $messenger, ConfigFactoryInterface $config_factory, IpGeoLocAPI $api, IpGeoLocSession $session) {
parent::__construct($config_factory);
$this->messenger = $messenger;
$this->api = $api;
$this->session = $session;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('messenger'), $container
->get('config.factory'), $container
->get('ip_geoloc.api'), $container
->get('ip_geoloc.session'));
}
public function getFormId() {
return 'ip_geoloc_set_location_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$ip_geoloc_config = $this
->config('ip_geoloc.settings');
$has_find_visitor = $ip_geoloc_config
->get('ip_geoloc_visitor_find') ? $ip_geoloc_config
->get('ip_geoloc_visitor_find') : TRUE;
$is_address_editable = $ip_geoloc_config
->get('ip_geoloc_visitor_address_editable') ? $ip_geoloc_config
->get('ip_geoloc_visitor_address_editable') : TRUE;
$geo_vocabulary_id = $ip_geoloc_config
->get('ip_geoloc_geo_vocabulary_id') ? $ip_geoloc_config
->get('ip_geoloc_geo_vocabulary_id') : 0;
if (!$has_find_visitor && !$is_address_editable && !$geo_vocabulary_id) {
$this->messenger
->addError($this
->t('You should select at least one of the three widgets available for the "Set my location" block.'));
return $form;
}
$location = $this->api
->getVisitorLocation();
$form['#attributes']['id'] = $ajax_wrapper_id = drupal_html_id('set-location-form');
if ($has_find_visitor) {
_ip_geoloc_set_my_location_add_find_me($form, $ajax_wrapper_id);
}
$options = _ip_geoloc_set_my_location_add_selector($form, $location, $is_address_editable, $geo_vocabulary_id);
$is_reverse_geocode = $has_find_visitor && $ip_geoloc_config
->get('ip_geoloc_visitor_reverse_geocode') ? $ip_geoloc_config
->get('ip_geoloc_visitor_reverse_geocode') : TRUE;
if ($is_reverse_geocode || $is_address_editable) {
_ip_geoloc_set_my_location_add_address($form, $location);
}
if ($geo_vocabulary_id) {
_ip_geoloc_set_my_location_add_region($form, $location, $geo_vocabulary_id);
}
_ip_geoloc_set_my_location_add_logic($form, $location, $options, $is_address_editable, $geo_vocabulary_id);
if ($is_address_editable || $geo_vocabulary_id) {
$form['submit_address'] = [
'#type' => 'submit',
'#value' => t('Go'),
'#submit' => [
'_ip_geoloc_process_go_to_submit',
],
'#weight' => 15,
];
}
$form['#attributes']['class'][] = 'ip-geoloc-address';
$form['#attached']['library'][] = 'ip_geoloc/ip_geoloc.client';
return $form;
}
public function submitForm(array $form, FormStateInterface $form_state) {
$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')) {
$form_state
->setValue('fixed_address', $geo_vocabulary_id ? '2' : '1');
}
if ($form_state
->getValue('fixed_address') == '1') {
$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) {
$location = ip_geoloc_set_location_from_region($form_state
->getValue('region'));
}
$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);
}
$form_state
->setRebuild();
}
}