View source
<?php
namespace Drupal\geolocation;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class GeocoderBase extends PluginBase implements GeocoderInterface, ContainerFactoryPluginInterface {
protected $countryFormatterManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, GeocoderCountryFormattingManager $geocoder_country_formatter_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->countryFormatterManager = $geocoder_country_formatter_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.geocoder_country_formatting'));
}
protected function getDefaultSettings() {
return [
'label' => $this
->t('Address'),
'description' => $this
->t('Enter an address to be localized.'),
];
}
public function getSettings() {
return array_replace_recursive($this
->getDefaultSettings(), $this->configuration);
}
public function getOptionsForm() {
$settings = $this
->getSettings();
return [
'label' => [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#default_value' => $settings['label'],
'#size' => 15,
],
'description' => [
'#type' => 'textfield',
'#title' => $this
->t('Description'),
'#default_value' => $settings['description'],
'#size' => 25,
],
];
}
public function processOptionsForm(array $form_element) {
return NULL;
}
public function formAttachGeocoder(array &$render_array, $element_name) {
$settings = $this
->getSettings();
$render_array['geolocation_geocoder_address'] = [
'#type' => 'search',
'#title' => $settings['label'] ?: $this
->t('Address'),
'#placeholder' => $settings['label'] ?: $this
->t('Address'),
'#description' => $settings['description'] ?: $this
->t('Enter an address to retrieve location.'),
'#description_display' => 'after',
'#maxlength' => 256,
'#size' => 25,
'#attributes' => [
'class' => [
'geolocation-geocoder-address',
'form-autocomplete',
],
'data-source-identifier' => $element_name,
],
'#attached' => [
'drupalSettings' => [
'geolocation' => [
'geocoder' => [
$this
->getPluginId() => [
'inputIds' => [
$element_name,
],
],
],
],
],
],
];
}
protected function addressElements(array $address_atomics) {
$formatter = $this->countryFormatterManager
->getCountry($address_atomics['countryCode'], $this
->getPluginId());
if (empty($formatter)) {
return $address_atomics;
}
return $formatter
->format($address_atomics);
}
public function geocode($address) {
return NULL;
}
public function reverseGeocode($latitude, $longitude) {
return NULL;
}
}