You are here

public function GeofieldProximityFilter::query in Geofield 8

Add this filter to the query.

Due to the nature of fapi, the value and the operator have an unintended level of indirection. You will find them in $this->operator and $this->value respectively.

Overrides NumericFilter::query

File

src/Plugin/views/filter/GeofieldProximityFilter.php, line 192

Class

GeofieldProximityFilter
Field handler to filter Geofields by proximity.

Namespace

Drupal\geofield\Plugin\views\filter

Code

public function query() {
  $this
    ->ensureMyTable();
  $lat_alias = $this->realField . '_lat';
  $lon_alias = $this->realField . '_lon';
  try {

    /** @var \Drupal\geofield\Plugin\GeofieldProximitySourceInterface $source_plugin */
    $this->sourcePlugin = $this->proximitySourceManager
      ->createInstance($this->options['source'], $this->options['source_configuration']);
    $this->sourcePlugin
      ->setViewHandler($this);
    $this->sourcePlugin
      ->setUnits($this->options['units']);
    $info = $this
      ->operators();

    // Add query condition in case of valid proximity filter options.
    if ($haversine_options = $this->sourcePlugin
      ->getHaversineOptions()) {
      $haversine_options['destination_latitude'] = $this->tableAlias . '.' . $lat_alias;
      $haversine_options['destination_longitude'] = $this->tableAlias . '.' . $lon_alias;
      $this
        ->{$info[$this->operator]['method']}($haversine_options);

      // Ensure that destination is valid.
      $condition = (new Condition('AND'))
        ->isNotNull($haversine_options['destination_latitude'])
        ->isNotNull($haversine_options['destination_longitude']);
      $this->query
        ->addWhere(0, $condition);
    }
    elseif (!$this
      ->isExposed()) {

      // Origin is not valid so return no results (if not exposed filter).
      $this->query
        ->addWhereExpression($this->options['group'], '1=0');
    }
  } catch (\Exception $e) {
    watchdog_exception('geofield', $e);
  }
}