You are here

trait GeofieldProximityHandlerTrait in Geofield 8

Trait class for Geofield Proximity View Handlers.

Hierarchy

2 files declare their use of GeofieldProximityHandlerTrait
GeofieldProximityField.php in src/Plugin/views/field/GeofieldProximityField.php
GeofieldProximitySort.php in src/Plugin/views/sort/GeofieldProximitySort.php

File

src/Plugin/views/GeofieldProximityHandlerTrait.php, line 8

Namespace

Drupal\geofield\Plugin\views
View source
trait GeofieldProximityHandlerTrait {

  /**
   * Add an Order By declaration to the View Query.
   *
   * @param string $order
   *   The order to be applied (ASC or DESC)
   */
  public function addQueryOrderBy($order) {
    $this
      ->ensureMyTable();
    $lat_alias = $this->realField . '_lat';
    $lon_alias = $this->realField . '_lon';

    /** @var \Drupal\views\Plugin\views\query\Sql $query */
    $query = $this->query;
    try {

      /** @var \Drupal\geofield\Plugin\GeofieldProximitySourceInterface $source_plugin */
      $source_plugin = $this->proximitySourceManager
        ->createInstance($this->options['source'], $this->options['source_configuration']);
      $source_plugin
        ->setViewHandler($this);
      $source_plugin
        ->setUnits($this->options['units']);
      if ($haversine_options = $source_plugin
        ->getHaversineOptions()) {
        $haversine_options['destination_latitude'] = $this->tableAlias . '.' . $lat_alias;
        $haversine_options['destination_longitude'] = $this->tableAlias . '.' . $lon_alias;
        $query
          ->addOrderBy(NULL, geofield_haversine($haversine_options), $order, $this->tableAlias . '_' . $this->field);
      }
    } catch (\Exception $e) {
      watchdog_exception('geofield', $e);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GeofieldProximityHandlerTrait::addQueryOrderBy public function Add an Order By declaration to the View Query.