You are here

class SetLocationForm in IP Geolocation Views & Maps 8

Peding doc.

Hierarchy

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\Form
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SetLocationForm::$api protected property
SetLocationForm::$messenger protected property The Messenger service. Overrides MessengerTrait::$messenger
SetLocationForm::$session protected property
SetLocationForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
SetLocationForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
SetLocationForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SetLocationForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
SetLocationForm::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.