class SetLocationForm in IP Geolocation Views & Maps 8
Peding doc.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\ip_geoloc\Form\SetLocationForm implements ContainerFactoryPluginInterface
Expanded class hierarchy of SetLocationForm
1 file declares its use of SetLocationForm
- GeoCodeAddressBlock.php in src/
Plugin/ Block/ GeoCodeAddressBlock.php
File
- src/
Form/ SetLocationForm.php, line 16
Namespace
Drupal\ip_geoloc\FormView source
class SetLocationForm extends FormBase implements ContainerFactoryPluginInterface {
/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
protected $api;
protected $session;
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'ip_geoloc_set_location_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Migration comment: Part of ip_geoloc_set_location_form definition.
$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;
}
/**
* {@inheritdoc}
*/
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();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SetLocationForm:: |
protected | property | ||
SetLocationForm:: |
protected | property |
The Messenger service. Overrides MessengerTrait:: |
|
SetLocationForm:: |
protected | property | ||
SetLocationForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
SetLocationForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
SetLocationForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SetLocationForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
SetLocationForm:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |