You are here

public function ProximityField::query in Geolocation Field 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/views/field/ProximityField.php \Drupal\geolocation\Plugin\views\field\ProximityField::query()
  2. 8.2 src/Plugin/views/field/ProximityField.php \Drupal\geolocation\Plugin\views\field\ProximityField::query()

Called to add the field to a query.

Overrides FieldPluginBase::query

File

src/Plugin/views/field/ProximityField.php, line 371

Class

ProximityField
Field handler for geolocaiton field.

Namespace

Drupal\geolocation\Plugin\views\field

Code

public function query() {

  /** @var \Drupal\views\Plugin\views\query\Sql $query */
  $query = $this->query;
  switch ($this->options['proximity_source']) {
    case 'user_input':
      $latitude = $this->view
        ->getRequest()
        ->get('proximity_lat', '');
      $longitude = $this->view
        ->getRequest()
        ->get('proximity_lng', '');
      $units = $this->options['proximity_units'];
      break;
    case 'filter':

      /** @var \Drupal\geolocation\Plugin\views\filter\ProximityFilter $filter */
      $filter = $this->view->filter[$this->options['proximity_filter']];
      $latitude = $filter
        ->getLatitudeValue();
      $longitude = $filter
        ->getLongitudeValue();
      $units = $filter
        ->getProximityUnit();
      break;
    case 'boundary_filter':
      $filter = $this->view->filter[$this->options['boundary_filter']];

      // See documentation at
      // http://tubalmartin.github.io/spherical-geometry-php/#LatLngBounds
      $latitude = ($filter->value['lat_south_west'] + $filter->value['lat_north_east']) / 2;
      $longitude = ($filter->value['lng_south_west'] + $filter->value['lng_north_east']) / 2;
      if ($filter->value['lng_south_west'] > $filter->value['lng_north_east']) {
        $longitude = $longitude == 0 ? 180 : fmod(fmod($longitude + 180 - -180, 360) + 360, 360) + -180;
      }
      $units = $this->options['proximity_units'];
      break;
    case 'argument':

      /** @var \Drupal\geolocation\Plugin\views\argument\ProximityArgument $argument */
      $argument = $this->view->argument[$this->options['proximity_argument']];
      $values = $argument
        ->getParsedReferenceLocation();
      $latitude = $values['lat'];
      $longitude = $values['lng'];
      $units = $values['units'];
      break;
    case 'entity_id_argument':
      $argument = $this->view->argument[$this->options['entity_id_argument']];
      if (empty($argument)) {
        return;
      }
      $entity_id = $argument
        ->getValue();
      if (!ctype_digit($entity_id)) {
        return;
      }

      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $entity = \Drupal::entityTypeManager()
        ->getStorage($this
        ->getEntityType())
        ->load($entity_id);
      if (!$entity || !$entity
        ->hasField($this->realField)) {
        return;
      }
      $field = $entity
        ->get($this->realField);
      if (empty($field)) {
        return;
      }
      $values = $field
        ->getValue();
      if (empty($values)) {
        return;
      }
      $values = reset($values);
      $latitude = $values['lat'];
      $longitude = $values['lng'];
      $units = $this->options['proximity_units'];
      break;
    default:
      $latitude = $this->options['proximity_lat'];
      $longitude = $this->options['proximity_lng'];
      $units = $this->options['proximity_units'];
  }
  if (!is_numeric($latitude) || !is_numeric($longitude)) {
    return;
  }

  // Get the earth radius from the units.
  $earth_radius = $units === 'mile' ? GeolocationCore::EARTH_RADIUS_MILE : GeolocationCore::EARTH_RADIUS_KM;

  // Build the query expression.
  $expression = $this->geolocationCore
    ->getProximityQueryFragment($this
    ->ensureMyTable(), $this->realField, $latitude, $longitude, $earth_radius);

  // Get a placeholder for this query and save the field_alias for it.
  // Remove the initial ':' from the placeholder and avoid collision with
  // original field name.
  $this->field_alias = $query
    ->addField(NULL, $expression, substr($this
    ->placeholder(), 1));
}