public function ProximityField::buildOptionsForm in Geolocation Field 8
Same name and namespace in other branches
- 8.3 src/Plugin/views/field/ProximityField.php \Drupal\geolocation\Plugin\views\field\ProximityField::buildOptionsForm()
- 8.2 src/Plugin/views/field/ProximityField.php \Drupal\geolocation\Plugin\views\field\ProximityField::buildOptionsForm()
Default options form that provides the label widget that all fields should have.
Overrides NumericField::buildOptionsForm
File
- src/
Plugin/ views/ field/ ProximityField.php, line 89
Class
- ProximityField
- Field handler for geolocaiton field.
Namespace
Drupal\geolocation\Plugin\views\fieldCode
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
// Add the proximity field group.
$form['proximity_group'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Proximity Settings'),
];
$form['proximity_source'] = [
'#type' => 'select',
'#title' => $this
->t('Select the source type.'),
'#description' => $this
->t('To calculate proximity we need a starting point to compare the field value to. Select where to get the start location.'),
'#default_value' => $this->options['proximity_source'],
'#fieldset' => 'proximity_group',
'#options' => [
'direct_input' => $this
->t('Static Values'),
'user_input' => $this
->t('User input'),
],
];
/*
* Direct input form elements.
*/
$form['proximity_lat'] = [
'#type' => 'textfield',
'#title' => $this
->t('Latitude'),
'#empty_value' => '',
'#default_value' => $this->options['proximity_lat'],
'#maxlength' => 255,
'#fieldset' => 'proximity_group',
'#states' => [
'visible' => [
'select[name="options[proximity_source]"]' => [
'value' => 'direct_input',
],
],
],
];
$form['proximity_lng'] = [
'#type' => 'textfield',
'#title' => $this
->t('Longitude'),
'#empty_value' => '',
'#default_value' => $this->options['proximity_lng'],
'#maxlength' => 255,
'#fieldset' => 'proximity_group',
'#states' => [
'visible' => [
'select[name="options[proximity_source]"]' => [
'value' => 'direct_input',
],
],
],
];
$form['proximity_units'] = [
'#type' => 'select',
'#title' => $this
->t('Units'),
'#default_value' => !empty($this->options['proximity_units']) ? $this->options['proximity_units'] : '',
'#weight' => 40,
'#fieldset' => 'proximity_group',
'#options' => [
'mile' => $this
->t('Miles'),
'km' => $this
->t('Kilometers'),
],
'#states' => [
'visible' => [
[
[
'select[name="options[proximity_source]"]' => [
'value' => 'direct_input',
],
],
'or',
[
'select[name="options[proximity_source]"]' => [
'value' => 'boundary_filter',
],
],
'or',
[
'select[name="options[proximity_source]"]' => [
'value' => 'entity_id_argument',
],
],
'or',
[
'select[name="options[proximity_source]"]' => [
'value' => 'user_input',
],
],
],
],
],
];
$geocoder_definitions = $this->geolocationCore
->getGeocoderManager()
->getLocationCapableGeocoders();
if ($geocoder_definitions) {
$form['proximity_geocoder'] = [
'#type' => 'checkbox',
'#default_value' => $this->options['proximity_geocoder'],
'#title' => $this
->t('Use Geocoder for latitude/longitude input'),
'#fieldset' => 'proximity_group',
'#states' => [
'visible' => [
'select[name="options[proximity_source]"]' => [
'value' => 'user_input',
],
],
],
];
$geocoder_options = [];
foreach ($geocoder_definitions as $id => $definition) {
$geocoder_options[$id] = $definition['name'];
}
$form['proximity_geocoder_plugin_settings'] = [
'#type' => 'container',
'#fieldset' => 'proximity_group',
'#states' => [
'visible' => [
'input[name="options[proximity_geocoder]"]' => [
'checked' => TRUE,
],
'select[name="options[proximity_source]"]' => [
'value' => 'user_input',
],
],
],
];
$geocoder_container =& $form['proximity_geocoder_plugin_settings'];
$geocoder_container['plugin_id'] = [
'#type' => 'select',
'#options' => $geocoder_options,
'#title' => $this
->t('Geocoder plugin'),
'#default_value' => $this->options['proximity_geocoder_plugin_settings']['plugin_id'],
'#ajax' => [
'callback' => [
get_class($this->geolocationCore
->getGeocoderManager()),
'addGeocoderSettingsFormAjax',
],
'wrapper' => 'geocoder-plugin-settings',
'effect' => 'fade',
],
];
if (!empty($this->options['proximity_geocoder_plugin_settings']['plugin_id'])) {
$geocoder_plugin = $this->geolocationCore
->getGeocoderManager()
->getGeocoder($this->options['proximity_geocoder_plugin_settings']['plugin_id'], $this->options['proximity_geocoder_plugin_settings']['settings']);
}
elseif (current(array_keys($geocoder_options))) {
$geocoder_plugin = $this->geolocationCore
->getGeocoderManager()
->getGeocoder(current(array_keys($geocoder_options)));
}
if (!empty($geocoder_plugin)) {
$geocoder_settings_form = $geocoder_plugin
->getOptionsForm();
if ($geocoder_settings_form) {
$geocoder_container['settings'] = $geocoder_settings_form;
}
}
if (empty($geocoder_container['settings'])) {
$geocoder_container['settings'] = [
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => $this
->t("No settings available."),
];
}
$geocoder_container['settings'] = array_replace_recursive($geocoder_container['settings'], [
'#flatten' => TRUE,
'#prefix' => '<div id="geocoder-plugin-settings">',
'#suffix' => '</div>',
]);
}
/*
* Available proximity filters form elements.
*/
$proximity_filters = [];
/** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */
foreach ($this->displayHandler
->getHandlers('filter') as $delta => $filter) {
if (is_a($filter, '\\Drupal\\geolocation\\Plugin\\views\\filter\\ProximityFilter')) {
$proximity_filters[$delta] = $filter
->adminLabel();
}
}
if (!empty($proximity_filters)) {
$form['proximity_filter'] = [
'#type' => 'select',
'#title' => $this
->t('Select filter.'),
'#description' => $this
->t('Select the filter to use as the starting point for calculating proximity.'),
'#options' => $proximity_filters,
'#default_value' => $this->options['proximity_filter'],
'#fieldset' => 'proximity_group',
'#states' => [
'visible' => [
'select[name="options[proximity_source]"]' => [
'value' => 'filter',
],
],
],
];
$form['proximity_source']['#options']['filter'] = $this
->t('Proximity Filter');
}
/*
* Proximity contextual filter form elements.
*/
$proximity_arguments = [];
/** @var \Drupal\views\Plugin\views\argument\ArgumentPluginBase $argument */
foreach ($this->displayHandler
->getHandlers('argument') as $delta => $argument) {
if ($argument
->getPluginId() === 'geolocation_argument_proximity') {
$proximity_arguments[$delta] = $argument
->adminLabel();
}
}
if (!empty($proximity_arguments)) {
$form['proximity_argument'] = [
'#type' => 'select',
'#title' => $this
->t('Select contextual filter (argument).'),
'#description' => $this
->t('Select the contextual filter (argument) to use as the starting point for calculating proximity.'),
'#options' => $proximity_arguments,
'#default_value' => $this->options['proximity_argument'],
'#fieldset' => 'proximity_group',
'#states' => [
'visible' => [
'select[name="options[proximity_source]"]' => [
'value' => 'argument',
],
],
],
];
$form['proximity_source']['#options']['argument'] = $this
->t('Proximity Contextual Filter');
}
/*
* Available boundary filters form elements.
*/
$boundary_filters = [];
/** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */
foreach ($this->displayHandler
->getHandlers('filter') as $delta => $filter) {
if (is_a($filter, '\\Drupal\\geolocation\\Plugin\\views\\filter\\BoundaryFilter')) {
$boundary_filters[$delta] = $filter
->adminLabel();
}
}
if (!empty($boundary_filters)) {
$form['boundary_filter'] = [
'#type' => 'select',
'#title' => $this
->t('Select filter.'),
'#description' => $this
->t('Select the boundary filter to use as the starting point for calculating proximity.'),
'#options' => $boundary_filters,
'#default_value' => $this->options['boundary_filter'],
'#fieldset' => 'proximity_group',
'#states' => [
'visible' => [
'select[name="options[proximity_source]"]' => [
'value' => 'boundary_filter',
],
],
],
];
$form['proximity_source']['#options']['boundary_filter'] = $this
->t('Boundary Filter');
}
/*
* Entity ID contextual filter form elements.
*/
$entity_id_arguments = [];
/** @var \Drupal\views\Plugin\views\argument\ArgumentPluginBase $argument */
foreach ($this->displayHandler
->getHandlers('argument') as $delta => $argument) {
$entity_id_arguments[$delta] = $argument
->adminLabel();
}
$entity_type_label = \Drupal::entityTypeManager()
->getDefinition($this
->getEntityType())
->getLabel();
if (!empty($entity_id_arguments)) {
$form['entity_id_argument'] = [
'#type' => 'select',
'#title' => $this
->t('Select a contextual filter returning the @entity_type ID to base proximity on.', [
'@entity_type' => $entity_type_label,
]),
'#description' => $this
->t('The value of the @field_name field of this @entity_type will be used as center for distance values.', [
'@entity_type' => $entity_type_label,
'@field_name' => $this->field,
]),
'#options' => $entity_id_arguments,
'#default_value' => $this->options['entity_id_argument'],
'#fieldset' => 'proximity_group',
'#states' => [
'visible' => [
'select[name="options[proximity_source]"]' => [
'value' => 'entity_id_argument',
],
],
],
];
$form['proximity_source']['#options']['entity_id_argument'] = $this
->t('Entity ID Contextual Filter');
}
parent::buildOptionsForm($form, $form_state);
}