View source
<?php
namespace Drupal\geofield\Plugin\GeofieldProximitySource;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\geofield\Plugin\GeofieldProximitySourceBase;
use Drupal\geofield\Plugin\GeofieldProximitySourceManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OriginFromProximityFilter extends GeofieldProximitySourceBase implements ContainerFactoryPluginInterface {
protected $proximitySourceManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.geofield_proximity_source'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, GeofieldProximitySourceManager $proximitySourceManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->proximitySourceManager = $proximitySourceManager;
}
protected function getAvailableProximityFilters() {
$proximity_filters = [];
foreach ($this->viewHandler->displayHandler
->getHandlers('filter') as $delta => $filter) {
if ($filter->pluginId === 'geofield_proximity_filter') {
$proximity_filters[$delta] = $filter
->adminLabel();
}
}
return $proximity_filters;
}
public function buildOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents, $is_exposed = FALSE) {
$user_input = $form_state
->getUserInput();
$proximity_filters_sources = $this
->getAvailableProximityFilters();
$user_input_proximity_filter = isset($user_input['options']['source_configuration']['source_proximity_filter']) ? $user_input['options']['source_configuration']['source_proximity_filter'] : current(array_keys($proximity_filters_sources));
$source_proximity_filter = isset($this->configuration['source_proximity_filter']) ? $this->configuration['source_proximity_filter'] : $user_input_proximity_filter;
if (!empty($proximity_filters_sources)) {
$form['source_proximity_filter'] = [
'#type' => 'select',
'#title' => $this
->t('Source Proximity Filter'),
'#description' => $this
->t('Select the Geofield Proximity filter to use as the starting point for calculating proximity.'),
'#options' => $this
->getAvailableProximityFilters(),
'#default_value' => $source_proximity_filter,
'#ajax' => [
'callback' => [
static::class,
'sourceProximityFilterUpdate',
],
'effect' => 'fade',
],
];
}
else {
$form['source_proximity_filter_warning'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t('No Geofield Proximity Filter found. At least one should be set for this Proximity Field be able to work.'),
"#attributes" => [
'class' => [
'proximity-filter-warning',
'red',
],
],
];
$form_state
->setError($form['source_proximity_filter_warning'], $this
->t('This Proximity Field cannot work. Dismiss this and add & setup a Geofield Proximity Filter before.'));
}
}
public function validateOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents) {
$values = $form_state
->getValues();
if (!isset($values['options']['source_configuration']['source_proximity_filter'])) {
$form_state
->setError($form['source_proximity_filter_warning'], $this
->t('This Proximity Field cannot work. Dismiss this and add and setup a Proximity Filter before.'));
}
}
public static function sourceProximityFilterUpdate(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new ReplaceCommand('#proximity-source-configuration', $form['options']['source_configuration']));
return $response;
}
public function getOrigin() {
$origin = [];
if (isset($this->viewHandler) && isset($this->viewHandler->view->filter[$this->viewHandler->options['source_configuration']['source_proximity_filter']]) && is_a($this->viewHandler->view->filter[$this->viewHandler->options['source_configuration']['source_proximity_filter']], '\\Drupal\\geofield\\Plugin\\views\\filter\\GeofieldProximityFilter') && ($source_proximity_filter = $this->viewHandler->options['source_configuration']['source_proximity_filter'])) {
$geofield_proximity_filter = $this->viewHandler->view->filter[$source_proximity_filter];
$source_plugin_id = $geofield_proximity_filter->options['source'];
$source_plugin_configuration = $geofield_proximity_filter->options['source_configuration'];
try {
$source_plugin = $this->proximitySourceManager
->createInstance($source_plugin_id, $source_plugin_configuration);
$source_plugin
->setViewHandler($geofield_proximity_filter);
$origin = $source_plugin
->getOrigin();
} catch (\Exception $e) {
watchdog_exception('geofield', $e);
}
}
return $origin;
}
}