class Geocoder in Geolocation Field 8.3
Same name in this branch
- 8.3 src/Annotation/Geocoder.php \Drupal\geolocation\Annotation\Geocoder
- 8.3 src/Plugin/geolocation/LocationInput/Geocoder.php \Drupal\geolocation\Plugin\geolocation\LocationInput\Geocoder
Same name and namespace in other branches
- 8.2 src/Plugin/geolocation/LocationInput/Geocoder.php \Drupal\geolocation\Plugin\geolocation\LocationInput\Geocoder
Location based proximity center.
Plugin annotation
@LocationInput(
id = "geocoder",
name = @Translation("Geocoder address input"),
description = @Translation("Enter an address and use the geocoded location."),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\geolocation\LocationInputBase implements ContainerFactoryPluginInterface, LocationInputInterface
- class \Drupal\geolocation\Plugin\geolocation\LocationInput\Geocoder implements ContainerFactoryPluginInterface, LocationInputInterface
- class \Drupal\geolocation\LocationInputBase implements ContainerFactoryPluginInterface, LocationInputInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Geocoder
4 string references to 'Geocoder'
- AddressFieldProvider::getSettingsForm in modules/
geolocation_address/ src/ Plugin/ geolocation/ DataProvider/ AddressFieldProvider.php - Provide data provider settings form array.
- geolocation.location_input.schema.yml in config/
schema/ geolocation.location_input.schema.yml - config/schema/geolocation.location_input.schema.yml
- geolocation_google_maps.map_features.schema.yml in modules/
geolocation_google_maps/ config/ schema/ geolocation_google_maps.map_features.schema.yml - modules/geolocation_google_maps/config/schema/geolocation_google_maps.map_features.schema.yml
- geolocation_leaflet.map_features.schema.yml in modules/
geolocation_leaflet/ config/ schema/ geolocation_leaflet.map_features.schema.yml - modules/geolocation_leaflet/config/schema/geolocation_leaflet.map_features.schema.yml
File
- src/
Plugin/ geolocation/ LocationInput/ Geocoder.php, line 20
Namespace
Drupal\geolocation\Plugin\geolocation\LocationInputView source
class Geocoder extends LocationInputBase implements LocationInputInterface, ContainerFactoryPluginInterface {
/**
* Geocoder Manager.
*
* @var \Drupal\geolocation\GeocoderManager
*/
protected $geocoderManager;
/**
* Geocoder constructor.
*
* @param array $configuration
* Configuration.
* @param string $plugin_id
* Plugin ID.
* @param mixed $plugin_definition
* Plugin definition.
* @param \Drupal\geolocation\GeocoderManager $geocoder_manager
* Geocoder Manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, GeocoderManager $geocoder_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->geocoderManager = $geocoder_manager;
}
/**
* {@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.geolocation.geocoder'));
}
/**
* {@inheritdoc}
*/
public static function getDefaultSettings() {
$settings = parent::getDefaultSettings();
$settings['auto_submit'] = FALSE;
$settings['hide_form'] = FALSE;
$settings['plugin_id'] = '';
$settings['settings'] = [];
return $settings;
}
/**
* {@inheritdoc}
*/
public function getSettingsForm($option_id = NULL, array $settings = [], $context = NULL) {
$form = [];
$settings = $this
->getSettings($settings);
$geocoder_options = [];
foreach ($this->geocoderManager
->getDefinitions() as $geocoder_id => $geocoder_definition) {
if (empty($geocoder_definition['locationCapable'])) {
continue;
}
$geocoder_options[$geocoder_id] = $geocoder_definition['name'];
}
if ($geocoder_options) {
$form['plugin_id'] = [
'#type' => 'select',
'#options' => $geocoder_options,
'#title' => $this
->t('Geocoder plugin'),
'#default_value' => $settings['plugin_id'],
'#ajax' => [
'callback' => [
get_class($this->geocoderManager),
'addGeocoderSettingsFormAjax',
],
'wrapper' => 'location-input-geocoder-plugin-settings',
'effect' => 'fade',
],
];
if (!empty($settings['plugin_id'])) {
$geocoder_plugin = $this->geocoderManager
->getGeocoder($settings['plugin_id'], $settings['settings']);
}
elseif (current(array_keys($geocoder_options))) {
$geocoder_plugin = $this->geocoderManager
->getGeocoder(current(array_keys($geocoder_options)));
}
if (!empty($geocoder_plugin)) {
$geocoder_settings_form = $geocoder_plugin
->getOptionsForm();
if ($geocoder_settings_form) {
$form['settings'] = $geocoder_settings_form;
}
}
if (empty($form['settings'])) {
$form['settings'] = [
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => $this
->t("No settings available."),
];
}
$form['settings'] = array_replace_recursive($form['settings'], [
'#flatten' => TRUE,
'#prefix' => '<div id="location-input-geocoder-plugin-settings">',
'#suffix' => '</div>',
]);
$form['auto_submit'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Auto-submit form'),
'#default_value' => $settings['auto_submit'],
'#description' => $this
->t('Only triggers if location could be set'),
];
$form['hide_form'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Hide coordinates form'),
'#default_value' => $settings['hide_form'],
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function getCoordinates($form_value, $option_id, array $option_settings, $context = NULL) {
$coordinates = parent::getCoordinates($form_value, $option_id, $option_settings, $context);
if ($coordinates) {
return $coordinates;
}
if (empty($form_value['geocoder'])) {
return [];
}
$settings = $this
->getSettings($option_settings);
$location_data = $this->geocoderManager
->getGeocoder($settings['plugin_id'], $settings['settings'])
->geocode($form_value['geocoder']['geolocation_geocoder_address']);
if (!empty($location_data['location'])) {
return $location_data['location'];
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getForm($option_id, array $option_settings, $context = NULL, array $default_value = NULL) {
$form = parent::getForm($option_id, $option_settings, $context, $default_value);
if (empty($form['coordinates'])) {
return $form;
}
$option_settings = $this
->getSettings($option_settings);
$identifier = uniqid($option_id);
$form['coordinates']['#attributes'] = [
'class' => [
$identifier,
'location-input-geocoder',
],
];
$form['geocoder'] = [
'#type' => 'container',
'#attached' => [
'library' => [
'geolocation/location_input.geocoder',
],
'drupalSettings' => [
'geolocation' => [
'locationInput' => [
'geocoder' => [
[
'identifier' => $identifier,
'autoSubmit' => $option_settings['auto_submit'],
'hideForm' => $option_settings['hide_form'],
],
],
],
],
],
],
];
/** @var \Drupal\geolocation\GeocoderInterface $geocoder_plugin */
$geocoder_plugin = $this->geocoderManager
->getGeocoder($option_settings['plugin_id'], $option_settings['settings']);
$geocoder_plugin
->formAttachGeocoder($form['geocoder'], $identifier);
return $form;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
Geocoder:: |
protected | property | Geocoder Manager. | |
Geocoder:: |
public static | function |
Creates an instance of the plugin. Overrides LocationInputBase:: |
|
Geocoder:: |
public | function |
Get center value. Overrides LocationInputBase:: |
|
Geocoder:: |
public static | function |
Provide a populated settings array. Overrides LocationInputBase:: |
|
Geocoder:: |
public | function |
Get center form. Overrides LocationInputBase:: |
|
Geocoder:: |
public | function |
Settings form by ID and context. Overrides LocationInputBase:: |
|
Geocoder:: |
public | function |
Geocoder constructor. Overrides PluginBase:: |
|
LocationInputBase:: |
public | function |
For one LocationInput (i.e. boundary filter), return all options. Overrides LocationInputInterface:: |
1 |
LocationInputBase:: |
public | function |
Provide LocationInput option specific settings. Overrides LocationInputInterface:: |
|
LocationInputBase:: |
public | function | ||
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
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. |