You are here

public function BBoxArgument::query in Views GeoJSON 8

If we've been passed a bounding box, it's parsable, and the view style has a geofield, then we work out which fields to add to the query and add a where clause.

Overrides StringArgument::query

File

src/Plugin/views/argument/BBoxArgument.php, line 64

Class

BBoxArgument
Argument handler for Bounding Boxes.

Namespace

Drupal\views_geojson\Plugin\views\argument

Code

public function query($group_by = FALSE) {
  if ($bbox = $this
    ->getParsedBoundingBox()) {
    $geojson_options = $this->view
      ->getStyle()->options;
    $geofield_type = NULL;
    $geofield_name = NULL;
    if (!empty($geojson_options['data_source']['value'])) {
      $geofield_type = $geojson_options['data_source']['value'];
      if (!empty($geojson_options['data_source'][$geofield_type])) {
        $geofield_name = $geojson_options['data_source'][$geofield_type];
      }
    }
    if (empty($geofield_type) || empty($geofield_name)) {
      return;
    }

    // @todo - get geo_table from field definition?
    $geo_table = "node__{$geofield_name}";
    if ($geofield_type == 'geolocation') {
      $geo_table_entity_id_field = 'entity_id';
      $field_lat = "{$geo_table}.field_geolocation_lat";
      $field_lng = "{$geo_table}.field_geolocation_lng";
    }
    elseif ($geofield_type == 'geofield') {
      $geo_table_entity_id_field = 'entity_id';
      $field_lat = "{$geo_table}.{$geofield_name}_lat";
      $field_lng = "{$geo_table}.{$geofield_name}_lon";
    }
    if (!empty($geo_table)) {
      $this->query
        ->ensureTable($geo_table, NULL, new JoinPluginBase([
        'table' => $geo_table,
        'field' => $geo_table_entity_id_field,
        'left_table' => 'node_field_data',
        'left_field' => 'nid',
      ], NULL, NULL));
    }
    $this->query
      ->addWhere('bbox', $field_lat, $bbox['bottom'], '>=');
    $this->query
      ->addWhere('bbox', $field_lat, $bbox['top'], '<=');
    $this->query
      ->addWhere('bbox', $field_lng, $bbox['left'], '>=');
    $this->query
      ->addWhere('bbox', $field_lng, $bbox['right'], '<=');
  }
}