You are here

public function ProximityFilter::buildOptionsForm in Geolocation Field 8

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

Provide the basic form which calls through to subforms. If overridden, it is best to call through to the parent, or to at least make sure all of the functions in this form are called.

Overrides FilterPluginBase::buildOptionsForm

File

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

Class

ProximityFilter
Filter handler for search keywords.

Namespace

Drupal\geolocation\Plugin\views\filter

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {

  // Add the proximity field group.
  $form['proximity_group'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Proximity Source Settings'),
  ];
  $form['proximity_source'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Select the source type.'),
    '#description' => $this
      ->t('To calculate proximity we need a starting point to compare the field value to. Select where to get the start location.'),
    '#default_value' => $this->options['proximity_source'],
    '#fieldset' => 'proximity_group',
    '#options' => [
      'direct_input' => $this
        ->t('Static Values'),
    ],
  ];
  if ($this
    ->isExposed()) {
    $form['proximity_source']['#options']['exposed'] = $this
      ->t('Expose in & retrieve from exposed form');
  }

  /*
   * Direct input form elements.
   */
  $form['proximity_lat'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Latitude'),
    '#empty_value' => '',
    '#default_value' => $this->options['proximity_lat'],
    '#maxlength' => 255,
    '#fieldset' => 'proximity_group',
    '#states' => [
      'visible' => [
        'select[name="options[proximity_source]"]' => [
          'value' => 'direct_input',
        ],
      ],
    ],
  ];
  $form['proximity_lng'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Longitude'),
    '#empty_value' => '',
    '#default_value' => $this->options['proximity_lng'],
    '#maxlength' => 255,
    '#fieldset' => 'proximity_group',
    '#states' => [
      'visible' => [
        'select[name="options[proximity_source]"]' => [
          'value' => 'direct_input',
        ],
      ],
    ],
  ];

  /*
   * Proximity contextual filter form elements.
   */
  $proximity_arguments = [];

  /** @var \Drupal\views\Plugin\views\argument\ArgumentPluginBase $argument */
  foreach ($this->displayHandler
    ->getHandlers('argument') as $delta => $argument) {
    if ($argument
      ->getPluginId() === 'geolocation_argument_proximity') {
      $proximity_arguments[$delta] = $argument
        ->adminLabel();
    }
  }
  if (!empty($proximity_arguments)) {
    $form['proximity_argument'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Select contextual filter (argument).'),
      '#description' => $this
        ->t('Select the contextual filter (argument) to use as the starting point for calculating proximity.'),
      '#options' => $proximity_arguments,
      '#default_value' => $this->options['proximity_argument'],
      '#fieldset' => 'proximity_group',
      '#states' => [
        'visible' => [
          'select[name="options[proximity_source]"]' => [
            'value' => 'argument',
          ],
        ],
      ],
    ];
    $form['proximity_source']['#options']['argument'] = $this
      ->t('Proximity Contextual Filter');
  }

  /*
   * Available boundary filters form elements.
   */
  $boundary_filters = [];

  /** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */
  foreach ($this->displayHandler
    ->getHandlers('filter') as $delta => $filter) {
    if (is_a($filter, '\\Drupal\\geolocation\\Plugin\\views\\filter\\BoundaryFilter')) {
      $boundary_filters[$delta] = $filter
        ->adminLabel();
    }
  }
  if (!empty($boundary_filters)) {
    $form['boundary_filter'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Select filter.'),
      '#description' => $this
        ->t('Select the boundary filter to use as the starting point for calculating proximity.'),
      '#options' => $boundary_filters,
      '#default_value' => $this->options['boundary_filter'],
      '#fieldset' => 'proximity_group',
      '#states' => [
        'visible' => [
          'select[name="options[proximity_source]"]' => [
            'value' => 'boundary_filter',
          ],
        ],
      ],
    ];
    $form['proximity_source']['#options']['boundary_filter'] = $this
      ->t('Boundary Filter');
  }

  /*
   * Entity ID contextual filter form elements.
   */
  $entity_id_arguments = [];

  /** @var \Drupal\views\Plugin\views\argument\ArgumentPluginBase $argument */
  foreach ($this->displayHandler
    ->getHandlers('argument') as $delta => $argument) {
    $entity_id_arguments[$delta] = $argument
      ->adminLabel();
  }
  $entity_type_label = (string) \Drupal::entityTypeManager()
    ->getDefinition($this
    ->getEntityType())
    ->getLabel();
  if (!empty($entity_id_arguments)) {
    $form['entity_id_argument'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Select a contextual filter returning the @entity_type ID to base proximity on.', [
        '@entity_type' => $entity_type_label,
      ]),
      '#description' => $this
        ->t('The value of the @field_name field of this @entity_type will be used as center for distance values.', [
        '@entity_type' => $entity_type_label,
        '@field_name' => $this->field,
      ]),
      '#options' => $entity_id_arguments,
      '#default_value' => $this->options['entity_id_argument'],
      '#fieldset' => 'proximity_group',
      '#states' => [
        'visible' => [
          'select[name="options[proximity_source]"]' => [
            'value' => 'entity_id_argument',
          ],
        ],
      ],
    ];
    $form['proximity_source']['#options']['entity_id_argument'] = $this
      ->t('Entity ID Contextual Filter');
  }
  $proximity_units_options = [
    'mile' => $this
      ->t('Miles'),
    'km' => $this
      ->t('Kilometers'),
  ];
  if ($this
    ->isExposed()) {
    $proximity_units_options['exposed'] = $this
      ->t('Expose in & retrieve from exposed form');
  }
  if ($this->options['proximity_source'] == 'argument') {
    $proximity_units_options['argument'] = $this
      ->t('Derive from contextual proximity filter');
  }
  $form['proximity_units'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Units'),
    '#default_value' => !empty($this->options['proximity_units']) ? $this->options['proximity_units'] : '',
    '#weight' => 40,
    '#fieldset' => 'proximity_group',
    '#options' => $proximity_units_options,
    '#states' => [
      'visible' => [
        [
          [
            'select[name="options[proximity_source]"]' => [
              'value' => 'direct_input',
            ],
          ],
          'or',
          [
            'select[name="options[proximity_source]"]' => [
              'value' => 'exposed',
            ],
          ],
          'or',
          [
            'select[name="options[proximity_source]"]' => [
              'value' => 'boundary_filter',
            ],
          ],
          'or',
          [
            'select[name="options[proximity_source]"]' => [
              'value' => 'entity_id_argument',
            ],
          ],
        ],
      ],
    ],
  ];
  parent::buildOptionsForm($form, $form_state);
}