You are here

public function SearchApiViewsHandlerFieldLocation::get_value in Search API Location 7.2

Overridden to use a metadata wrapper.

Overrides entity_views_handler_field_text::get_value

1 call to SearchApiViewsHandlerFieldLocation::get_value()
SearchApiViewsHandlerFieldLocation::render in search_api_location_views/handler_field_location.inc
Render the field.

File

search_api_location_views/handler_field_location.inc, line 124
Contains SearchApiViewsHandlerFieldLocation.

Class

SearchApiViewsHandlerFieldLocation
Specialized handler for latitude/longitude fields.

Code

public function get_value($values, $field = NULL) {
  if ($field || empty($this->options['show_distance_to_point'])) {

    // Return the normal field value.
    return parent::get_value($values, $field);
  }

  // Return the distance.
  $spatials = (array) $this->query
    ->getOption('search_api_location', array());

  // If the spatial information aren't complete to have a distance skip
  // value fetching.
  foreach ($spatials as $spatial) {
    if ($spatial['field'] === $this->real_field) {
      $spatial_filter = $spatial;
      break;
    }
  }
  if (!isset($spatial_filter)) {
    return NULL;
  }
  $id = $values->_entity_properties['search_api_id'];
  $results = $this->query
    ->getSearchApiResults();
  if (isset($results['search_api_location'][$this->real_field][$id]['distance'])) {
    $value = $results['search_api_location'][$this->real_field][$id]['distance'];
  }
  else {

    // If the server wasn't able to calculate the distance do it here.
    $value = NULL;
    $latlon = parent::get_value($values);
    if (is_string($latlon) && strpos($latlon, ',')) {
      $location = array_combine(array(
        'lat',
        'lon',
      ), explode(',', $latlon, 2));
      $value = search_api_location_calculate_distance($location, array(
        'lat' => $spatial_filter['lat'],
        'lon' => $spatial_filter['lon'],
      ));
    }
  }

  // Do the conversion.
  if (is_numeric($value)) {
    return $value / $this->options['conversion_factor'];
  }
  return $value;
}