You are here

public function BoundaryFilter::buildExposeForm in Geolocation Field 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/views/filter/BoundaryFilter.php \Drupal\geolocation\Plugin\views\filter\BoundaryFilter::buildExposeForm()
  2. 8.2 src/Plugin/views/filter/BoundaryFilter.php \Drupal\geolocation\Plugin\views\filter\BoundaryFilter::buildExposeForm()

Options form subform for exposed filter options.

Overrides FilterPluginBase::buildExposeForm

See also

buildOptionsForm()

File

src/Plugin/views/filter/BoundaryFilter.php, line 98

Class

BoundaryFilter
Filter handler for search keywords.

Namespace

Drupal\geolocation\Plugin\views\filter

Code

public function buildExposeForm(&$form, FormStateInterface $form_state) {
  $geocoder_definitions = $this->geolocationCore
    ->getGeocoderManager()
    ->getBoundaryCapableGeocoders();
  if ($geocoder_definitions) {
    $form['expose']['input_by_geocoding_widget'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use geocoding widget to retrieve boundary values'),
      '#default_value' => $this->options['expose']['input_by_geocoding_widget'],
    ];
    $geocoder_options = [];
    foreach ($geocoder_definitions as $id => $definition) {
      $geocoder_options[$id] = $definition['name'];
    }
    $form['expose']['geocoder_plugin_settings'] = [
      '#type' => 'container',
      '#states' => [
        'visible' => [
          'input[name="options[expose][input_by_geocoding_widget]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $geocoder_container =& $form['expose']['geocoder_plugin_settings'];
    $geocoder_container['plugin_id'] = [
      '#type' => 'select',
      '#options' => $geocoder_options,
      '#title' => $this
        ->t('Geocoder plugin'),
      '#default_value' => $this->options['expose']['geocoder_plugin_settings']['plugin_id'],
      '#ajax' => [
        'callback' => [
          get_class($this->geolocationCore
            ->getGeocoderManager()),
          'addGeocoderSettingsFormAjax',
        ],
        'wrapper' => 'geocoder-plugin-settings',
        'effect' => 'fade',
      ],
    ];
    if (!empty($this->options['expose']['geocoder_plugin_settings']['plugin_id'])) {
      $geocoder_plugin = $this->geolocationCore
        ->getGeocoderManager()
        ->getGeocoder($this->options['expose']['geocoder_plugin_settings']['plugin_id'], $this->options['expose']['geocoder_plugin_settings']['settings']);
    }
    elseif (current(array_keys($geocoder_options))) {
      $geocoder_plugin = $this->geolocationCore
        ->getGeocoderManager()
        ->getGeocoder(current(array_keys($geocoder_options)));
    }
    if (!empty($geocoder_plugin)) {
      $geocoder_settings_form = $geocoder_plugin
        ->getOptionsForm();
      if ($geocoder_settings_form) {
        $geocoder_container['settings'] = $geocoder_settings_form;
      }
    }
    if (empty($geocoder_container['settings'])) {
      $geocoder_container['settings'] = [
        '#type' => 'html_tag',
        '#tag' => 'span',
        '#value' => $this
          ->t("No settings available."),
      ];
    }
    $geocoder_container['settings'] = array_replace_recursive($geocoder_container['settings'], [
      '#flatten' => TRUE,
      '#prefix' => '<div id="geocoder-plugin-settings">',
      '#suffix' => '</div>',
    ]);
  }
  parent::buildExposeForm($form, $form_state);
}