You are here

public function ProximityFilter::buildExposedForm in Geolocation Field 8

Render our chunk of the exposed filter form when selecting

You can override this if it doesn't do what you expect.

Overrides FilterPluginBase::buildExposedForm

File

src/Plugin/views/filter/ProximityFilter.php, line 189

Class

ProximityFilter
Filter handler for search keywords.

Namespace

Drupal\geolocation\Plugin\views\filter

Code

public function buildExposedForm(&$form, FormStateInterface $form_state) {
  parent::buildExposedForm($form, $form_state);
  $identifier = $this->options['expose']['identifier'];

  // Get the value element.
  if (isset($form['value']['#tree'])) {
    $value_element =& $form['value'];
  }
  else {
    $value_element =& $form;
  }
  $value_element[$identifier]['#weight'] = 30;
  if ($this->options['proximity_units'] == 'exposed') {
    $value_element[$this->options['expose']['identifier'] . '-units'] = [
      '#type' => 'select',
      '#default_value' => !empty($this->value['units']) ? $this->value['units'] : '',
      '#weight' => 40,
      '#options' => [
        'mile' => $this
          ->t('Miles'),
        'km' => $this
          ->t('Kilometers'),
      ],
    ];
  }
  if ($this->options['proximity_source'] == 'exposed') {
    $value_element[$this->options['expose']['identifier'] . '-lat'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Latitude'),
      '#weight' => 10,
    ];
    $value_element[$this->options['expose']['identifier'] . '-lng'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Longitude'),
      '#weight' => 20,
    ];
    if ($this->options['expose']['input_by_geocoding_widget'] && !empty($form[$identifier]) && !empty($this->options['expose']['geocoder_plugin_settings'])) {
      $geocoder_configuration = $this->options['expose']['geocoder_plugin_settings']['settings'];
      $geocoder_configuration['label'] = $this->options['expose']['label'];

      /** @var \Drupal\geolocation\GeocoderInterface $geocoder_plugin */
      $geocoder_plugin = $this->geolocationCore
        ->getGeocoderManager()
        ->getGeocoder($this->options['expose']['geocoder_plugin_settings']['plugin_id'], $geocoder_configuration);
      if (empty($geocoder_plugin)) {
        return;
      }
      $form[$identifier . '-lat']['#type'] = 'hidden';
      $form[$identifier . '-lng']['#type'] = 'hidden';
      $geocoder_plugin
        ->formAttachGeocoder($form, $identifier);
      $form = array_merge_recursive($form, [
        '#attached' => [
          'library' => [
            'geolocation/geolocation.views.filter.geocoder',
          ],
          'drupalSettings' => [
            'geolocation' => [
              'geocoder' => [
                'viewsFilterGeocoder' => [
                  $identifier => [
                    'type' => 'proximity',
                  ],
                ],
              ],
            ],
          ],
        ],
      ]);
    }
  }
}