View source
<?php
namespace Drupal\geolocation\Plugin\views\field;
use Drupal\Core\Render\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\geolocation\LocationManager;
use Drupal\geolocation\LocationInputManager;
use Drupal\geolocation\ProximityTrait;
use Drupal\Component\Utility\NestedArray;
class ProximityFormField extends ProximityField implements ContainerFactoryPluginInterface {
use ProximityTrait;
protected $centerValue = [];
protected $locationInputManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LocationManager $location_manager, LocationInputManager $location_input_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $location_manager);
$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.location'), $container
->get('plugin.manager.geolocation.locationinput'));
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$proximity_center_options = NestedArray::getValue($form_state
->getUserInput(), [
'options',
'center',
]);
if (empty($proximity_center_options)) {
$proximity_center_options = $this->options['center'];
}
if (empty($proximity_center_options)) {
$proximity_center_options = [];
}
$form['center'] = $this->locationInputManager
->getOptionsForm($proximity_center_options, $this);
}
public function viewsForm(&$form, FormStateInterface $form_state) {
$form['#tree'] = TRUE;
$form['center'] = $this->locationInputManager
->getForm($this->options['center'], $this, $this
->getCenter());
$form['actions']['submit']['#value'] = $this
->t('Calculate proximity');
foreach (Element::children($form) as $key) {
if (isset($form[$key]['#weight'])) {
$form[$key]['#weight'] = $form[$key]['#weight'] - 2;
}
else {
$form[$key]['#weight'] = -2;
}
}
$form['actions']['#weight'] = -1;
}
public function viewsFormSubmit(array &$form, FormStateInterface $form_state) {
if ($form_state
->get('step') == 'views_form_views_form') {
$form_state
->disableRedirect(TRUE);
}
}
protected function getCenter() {
if (empty($this->centerValue)) {
$this->centerValue = $this->locationInputManager
->getCoordinates((array) $this->view
->getRequest()
->get('center', []), $this->options['center'], $this);
}
return $this->centerValue;
}
}