You are here

public function ProximityField::viewsForm in Geolocation Field 8

Form constructor for the user input form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

File

src/Plugin/views/field/ProximityField.php, line 475

Class

ProximityField
Field handler for geolocaiton field.

Namespace

Drupal\geolocation\Plugin\views\field

Code

public function viewsForm(array &$form, FormStateInterface $form_state) {
  if ($this->options['proximity_source'] != 'user_input') {
    unset($form['actions']);
    return;
  }
  $form['#cache']['max-age'] = 0;
  $form['#method'] = 'GET';
  $form['#attributes']['class'][] = 'geolocation-views-proximity-field';
  $form['proximity_lat'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Latitude'),
    '#empty_value' => '',
    '#default_value' => $this->view
      ->getRequest()
      ->get('proximity_lat', ''),
    '#maxlength' => 255,
    '#weight' => -1,
  ];
  $form['proximity_lng'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Longitude'),
    '#empty_value' => '',
    '#default_value' => $this->view
      ->getRequest()
      ->get('proximity_lng', ''),
    '#maxlength' => 255,
    '#weight' => -1,
  ];
  if ($this->options['proximity_geocoder'] && !empty($this->options['proximity_geocoder_plugin_settings'])) {
    $geocoder_configuration = $this->options['proximity_geocoder_plugin_settings']['settings'];
    $geocoder_configuration['label'] = $this
      ->t('Address');

    /** @var \Drupal\geolocation\GeocoderInterface $geocoder_plugin */
    $geocoder_plugin = $this->geolocationCore
      ->getGeocoderManager()
      ->getGeocoder($this->options['proximity_geocoder_plugin_settings']['plugin_id'], $geocoder_configuration);
    if (empty($geocoder_plugin)) {
      return;
    }
    $form['proximity_lat']['#type'] = 'hidden';
    $form['proximity_lng']['#type'] = 'hidden';
    $geocoder_plugin
      ->formAttachGeocoder($form, 'views_field_geocoder');
    $form = array_merge_recursive($form, [
      '#attached' => [
        'library' => [
          'geolocation/geolocation.views.field.geocoder',
        ],
      ],
    ]);
  }
  $form['actions']['submit']['#value'] = $this
    ->t('Calculate proximity');

  // #weight will be stripped from 'output' in preRender callback.
  // Offset negatively to compensate.
  foreach (Element::children($form) as $key) {
    if (isset($form[$key]['#weight'])) {
      $form[$key]['#weight'] = $form[$key]['#weight'] - 2;
    }
    else {
      $form[$key]['#weight'] = -2;
    }
  }
  $form['actions']['#weight'] = -1;
}