abstract class GeofieldProximitySourceBase in Geofield 8
Base class for Geofield Proximity Source plugins.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\geofield\Plugin\GeofieldProximitySourceBase implements GeofieldProximitySourceInterface uses StringTranslationTrait
Expanded class hierarchy of GeofieldProximitySourceBase
3 files declare their use of GeofieldProximitySourceBase
- ContextProximityFilter.php in src/
Plugin/ GeofieldProximitySource/ ContextProximityFilter.php - ManualOriginDefault.php in src/
Plugin/ GeofieldProximitySource/ ManualOriginDefault.php - OriginFromProximityFilter.php in src/
Plugin/ GeofieldProximitySource/ OriginFromProximityFilter.php
File
- src/
Plugin/ GeofieldProximitySourceBase.php, line 16
Namespace
Drupal\geofield\PluginView source
abstract class GeofieldProximitySourceBase extends PluginBase implements GeofieldProximitySourceInterface {
use StringTranslationTrait;
/**
* The name of the constant defining the measurement unit.
*
* @var string
*/
protected $units;
/**
* The view handler which uses this proximity plugin.
*
* @var \Drupal\views\Plugin\views\HandlerBase
*/
protected $viewHandler;
/**
* The origin point to measure proximity from.
*
* @var array
*/
protected $origin;
/**
* {@inheritdoc}
*/
public function isValidLocation($lat, $lon) {
return is_numeric($lat) && is_numeric($lon);
}
/**
* {@inheritdoc}
*/
public function isEmptyLocation($lat, $lon) {
return empty($lat) && empty($lon);
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents, $is_exposed = FALSE) {
}
/**
* {@inheritdoc}
*/
public function validateOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents) {
}
/**
* {@inheritdoc}
*/
public function getOrigin() {
return $this->origin;
}
/**
* {@inheritdoc}
*/
public function setOrigin(array $origin) {
return $this->origin = $origin;
}
/**
* {@inheritdoc}
*/
public function setUnits($units) {
// If the given value is not a valid option, throw an error.
if (!in_array($units, $this
->getUnitsOptions())) {
$message = $this
->t('Invalid units supplied.');
\Drupal::logger('geofield')
->error($message);
return FALSE;
}
else {
$this->units = $units;
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function getUnits() {
return $this->units;
}
/**
* Get the list of valid options for units.
*
* @return array
* The list of available unit types.
*/
public function getUnitsOptions() {
return array_keys(geofield_radius_options());
}
/**
* {@inheritdoc}
*/
public function setViewHandler(HandlerBase $view_handler) {
$this->viewHandler = $view_handler;
}
/**
* {@inheritdoc}
*/
public function getProximity($lat, $lon) {
if (!$this
->isValidLocation($lat, $lon)) {
throw new InvalidPointException($this
->t('@proximity_handler reports Invalid Point coordinates', [
'@proximity_handler' => get_class($this),
]));
}
// Fetch the value of the units that have been set for this class. The
// constants are defined in the module file.
$radius = constant($this->units);
$origin = $this
->getOrigin();
if (!isset($origin['lat']) || !isset($origin['lon']) || $this
->isEmptyLocation($origin['lat'], $origin['lon'])) {
return NULL;
}
// Convert degrees to radians.
$origin_latitude = deg2rad($origin['lat']);
$origin_longitude = deg2rad($origin['lon']);
$destination_latitude = deg2rad($lat);
$destination_longitude = deg2rad($lon);
// Calculate proximity.
$proximity = $radius * acos(cos($origin_latitude) * cos($destination_latitude) * cos($destination_longitude - $origin_longitude) + sin($origin_latitude) * sin($destination_latitude));
if (!is_numeric($proximity)) {
throw new ProximityUnavailableException($this
->t('@proximity_handler not able to calculate valid Proximity value', [
'@proximity_handler' => get_class($this),
]));
}
return $proximity;
}
/**
* {@inheritdoc}
*/
public function getHaversineOptions() {
$origin = $this
->getOrigin();
if (!$origin || !isset($origin['lat']) || !isset($origin['lon'])) {
throw new HaversineUnavailableException('Not able to calculate Haversine Options due to invalid Proximity Origin definition.');
}
if ($this
->isEmptyLocation($origin['lat'], $origin['lon']) || !$this
->isValidLocation($origin['lat'], $origin['lon'])) {
return NULL;
}
return [
'origin_latitude' => $origin['lat'],
'origin_longitude' => $origin['lon'],
'earth_radius' => constant($this->units),
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GeofieldProximitySourceBase:: |
protected | property | The origin point to measure proximity from. | |
GeofieldProximitySourceBase:: |
protected | property | The name of the constant defining the measurement unit. | |
GeofieldProximitySourceBase:: |
protected | property | The view handler which uses this proximity plugin. | |
GeofieldProximitySourceBase:: |
public | function |
Builds the specific form elements for the geofield proximity plugin. Overrides GeofieldProximitySourceInterface:: |
2 |
GeofieldProximitySourceBase:: |
public | function |
Gets the haversine options. Overrides GeofieldProximitySourceInterface:: |
|
GeofieldProximitySourceBase:: |
public | function |
Gets the proximity distance origin. Overrides GeofieldProximitySourceInterface:: |
2 |
GeofieldProximitySourceBase:: |
public | function |
Get the calculated proximity. Overrides GeofieldProximitySourceInterface:: |
|
GeofieldProximitySourceBase:: |
public | function |
Get the current units. Overrides GeofieldProximitySourceInterface:: |
|
GeofieldProximitySourceBase:: |
public | function | Get the list of valid options for units. | |
GeofieldProximitySourceBase:: |
public | function |
Check if Location is empty. Overrides GeofieldProximitySourceInterface:: |
|
GeofieldProximitySourceBase:: |
public | function |
Check for a valid couple of latitude and longitude. Overrides GeofieldProximitySourceInterface:: |
|
GeofieldProximitySourceBase:: |
public | function |
Sets the proximity distance origin. Overrides GeofieldProximitySourceInterface:: |
|
GeofieldProximitySourceBase:: |
public | function |
Set the units to perform the calculation in. Overrides GeofieldProximitySourceInterface:: |
|
GeofieldProximitySourceBase:: |
public | function |
Sets view handler which uses this proximity plugin. Overrides GeofieldProximitySourceInterface:: |
|
GeofieldProximitySourceBase:: |
public | function |
Validates the options form for the geofield proximity plugin. Overrides GeofieldProximitySourceInterface:: |
1 |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |