class OriginFromProximityFilter in Geofield 8
Defines 'Geofield Custom Origin' plugin.
@package Drupal\geofield\Plugin
Plugin annotation
@GeofieldProximitySource(
id = "geofield_origin_from_proximity_filter",
label = @Translation("Origin from Proximity Filter"),
description = @Translation("A sort and field plugin that points the Origin from an existing Geofield Proximity Filter."),
exposedDescription = @Translation("The origin is fixed from an existing Geofield Proximity Filter."),
context = {
"sort",
"field",
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\geofield\Plugin\GeofieldProximitySourceBase implements GeofieldProximitySourceInterface uses StringTranslationTrait
- class \Drupal\geofield\Plugin\GeofieldProximitySource\OriginFromProximityFilter implements ContainerFactoryPluginInterface
- class \Drupal\geofield\Plugin\GeofieldProximitySourceBase implements GeofieldProximitySourceInterface uses StringTranslationTrait
Expanded class hierarchy of OriginFromProximityFilter
File
- src/
Plugin/ GeofieldProximitySource/ OriginFromProximityFilter.php, line 29
Namespace
Drupal\geofield\Plugin\GeofieldProximitySourceView source
class OriginFromProximityFilter extends GeofieldProximitySourceBase implements ContainerFactoryPluginInterface {
/**
* The geofield proximity manager.
*
* @var \Drupal\geofield\Plugin\GeofieldProximitySourceManager
*/
protected $proximitySourceManager;
/**
* {@inheritdoc}
*/
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'));
}
/**
* Constructs a GeocodeOrigin object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\geofield\Plugin\GeofieldProximitySourceManager $proximitySourceManager
* The Geofield Proximity Source manager service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, GeofieldProximitySourceManager $proximitySourceManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->proximitySourceManager = $proximitySourceManager;
}
/**
* Returns the list of available proximity filters.
*
* @return array
* The list of available proximity filters
*/
protected function getAvailableProximityFilters() {
$proximity_filters = [];
/** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */
foreach ($this->viewHandler->displayHandler
->getHandlers('filter') as $delta => $filter) {
if ($filter->pluginId === 'geofield_proximity_filter') {
$proximity_filters[$delta] = $filter
->adminLabel();
}
}
return $proximity_filters;
}
/**
* {@inheritdoc}
*/
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.'));
}
}
/**
* {@inheritdoc}
*/
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.'));
}
}
/**
* Ajax callback triggered on Proximity Filter Selection.
*
* @param array $form
* The build form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* Ajax response with updated form element.
*/
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;
}
/**
* {@inheritdoc}
*/
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'])) {
/** @var \Drupal\geofield\Plugin\views\filter\GeofieldProximityFilter $geofield_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 {
/** @var \Drupal\geofield\Plugin\GeofieldProximitySourceInterface $source_plugin */
$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;
}
}
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 |
Gets the haversine options. Overrides GeofieldProximitySourceInterface:: |
|
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:: |
|
OriginFromProximityFilter:: |
protected | property | The geofield proximity manager. | |
OriginFromProximityFilter:: |
public | function |
Builds the specific form elements for the geofield proximity plugin. Overrides GeofieldProximitySourceBase:: |
|
OriginFromProximityFilter:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
OriginFromProximityFilter:: |
protected | function | Returns the list of available proximity filters. | |
OriginFromProximityFilter:: |
public | function |
Gets the proximity distance origin. Overrides GeofieldProximitySourceBase:: |
|
OriginFromProximityFilter:: |
public static | function | Ajax callback triggered on Proximity Filter Selection. | |
OriginFromProximityFilter:: |
public | function |
Validates the options form for the geofield proximity plugin. Overrides GeofieldProximitySourceBase:: |
|
OriginFromProximityFilter:: |
public | function |
Constructs a GeocodeOrigin object. Overrides PluginBase:: |
|
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. | |
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. |