You are here

SetLocationForm.php in IP Geolocation Views & Maps 8

File

src/Form/SetLocationForm.php
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;

/**
 * Peding doc.
 */
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();
  }

}

Classes

Namesort descending Description
SetLocationForm Peding doc.