You are here

public function LocationInputPluginBase::getForm in Search API Location 8

Returns a form to configure settings for the plugin.

Parameters

array $form: The form where the settings form is being included in.

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

array $options: Option array with extra info of the plugin.

Return value

array The form definition for the widget settings.

Overrides LocationInputInterface::getForm

1 method overrides LocationInputPluginBase::getForm()
Map::getForm in src/Plugin/search_api_location/location_input/Map.php
Returns a form to configure settings for the plugin.

File

src/LocationInput/LocationInputPluginBase.php, line 87

Class

LocationInputPluginBase
Defines a base class from which other data type classes may extend.

Namespace

Drupal\search_api_location\LocationInput

Code

public function getForm(array $form, FormStateInterface $form_state, array $options) {
  $plugin_settings = $options['plugin-' . $options['plugin']];
  $is_views_ui_form = $form_state
    ->getBuildInfo()['form_id'] == 'views_ui_config_item_form';
  $operator_prefix = '';
  if ($is_views_ui_form) {
    $states_selector = 'input[name="options[operator]"]';
  }
  else {
    $states_selector = 'select[name="' . $options['expose']['operator_id'] . '"]';
  }
  if (!$is_views_ui_form && !($options['exposed'] && $options['expose']['use_operator'])) {
    $operator_prefix = $options['operator_options'][$options['operator']] . ' ';
  }
  $form['value']['#tree'] = TRUE;
  if ($plugin_settings['radius_type'] == 'select') {
    $distance_options = [];
    $lines = array_filter(array_map('trim', explode("\n", $plugin_settings['radius_options'])));
    foreach ($lines as $line) {
      $pos = strpos($line, ' ');
      $range = substr($line, 0, $pos);
      $distance_options[$range] = trim(substr($line, $pos + 1));
    }
    $form['value']['distance']['from'] = [
      '#type' => 'select',
      '#title' => ' ',
      '#options' => $distance_options,
      '#default_value' => $options['value']['distance']['from'],
      '#field_prefix' => $operator_prefix,
    ];
    $form['value']['distance']['to'] = [
      '#type' => 'select',
      '#title' => ' ',
      '#options' => $distance_options,
      '#default_value' => $options['value']['distance']['to'],
      '#field_prefix' => 'and ',
      '#states' => [
        'visible' => [
          $states_selector => [
            'value' => 'between',
          ],
        ],
      ],
    ];
  }
  elseif ($plugin_settings['radius_type'] == 'textfield') {
    $distance_suffix = $plugin_settings['radius_units'];
    $form['value']['distance']['from'] = [
      '#type' => 'textfield',
      '#title' => ' ',
      '#size' => 5,
      '#default_value' => $options['value']['distance']['from'],
      '#field_prefix' => $operator_prefix,
      '#field_suffix' => $distance_suffix,
    ];
    $form['value']['distance']['to'] = [
      '#title' => ' ',
      '#type' => 'textfield',
      '#size' => 5,
      '#default_value' => $options['value']['distance']['to'],
      '#field_prefix' => 'and ',
      '#field_suffix' => $distance_suffix,
      '#states' => [
        'visible' => [
          $states_selector => [
            'value' => 'between',
          ],
        ],
      ],
    ];
  }
  if (!$is_views_ui_form && !($options['exposed'] && $options['expose']['use_operator']) && $options['operator'] != 'between') {
    unset($form['value']['distance']['to']);
  }
  $form['value']['value'] = [
    '#type' => 'textfield',
    '#title' => ' ',
    '#size' => 20,
    '#default_value' => $options['value']['value'],
    '#field_prefix' => 'from ',
  ];
  return $form;
}