ViewsProximityFilter.php in Geolocation Field 8.3
File
src/Plugin/geolocation/Location/ViewsProximityFilter.php
View source
<?php
namespace Drupal\geolocation\Plugin\geolocation\Location;
use Drupal\geolocation\LocationInterface;
use Drupal\geolocation\LocationBase;
use Drupal\geolocation\LocationInputManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\geolocation\ViewsContextTrait;
class ViewsProximityFilter extends LocationBase implements LocationInterface {
use ViewsContextTrait;
protected $locationInputManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LocationInputManager $location_input_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->locationInputManager = $location_input_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.geolocation.locationinput'));
}
public function getAvailableLocationOptions($context) {
$options = [];
if ($displayHandler = self::getViewsDisplayHandler($context)) {
foreach ($displayHandler
->getHandlers('filter') as $delta => $filter) {
if ($filter
->getPluginId() === 'geolocation_filter_proximity' && $filter !== $context) {
$options[$delta] = $this
->t('Proximity filter') . ' - ' . $filter
->adminLabel();
}
}
}
return $options;
}
public function getCoordinates($location_option_id, array $location_option_settings, $context = NULL) {
if (!($displayHandler = self::getViewsDisplayHandler($context))) {
return parent::getCoordinates($location_option_id, $location_option_settings, $context);
}
$filter = $displayHandler
->getHandler('filter', $location_option_id);
if (empty($filter)) {
return parent::getCoordinates($location_option_id, $location_option_settings, $context);
}
if (array_key_exists('lat', $filter->value) && array_key_exists('lng', $filter->value)) {
return [
'lat' => (double) $filter->value['lat'],
'lng' => (double) $filter->value['lng'],
];
}
return $this->locationInputManager
->getCoordinates((array) $filter->value['center'], $filter->options['location_input'], $filter);
}
}