class Photon in Geolocation Field 8.3
Same name and namespace in other branches
- 8.2 modules/geolocation_leaflet/src/Plugin/geolocation/Geocoder/Photon.php \Drupal\geolocation_leaflet\Plugin\geolocation\Geocoder\Photon
Provides the Photon.
Plugin annotation
@Geocoder(
id = "photon",
name = @Translation("Photon"),
description = @Translation("See https://photon.komoot.io for details."),
locationCapable = true,
boundaryCapable = true,
frontendCapable = true,
reverseCapable = true,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\geolocation\GeocoderBase implements ContainerFactoryPluginInterface, GeocoderInterface
- class \Drupal\geolocation_leaflet\Plugin\geolocation\Geocoder\Photon implements GeocoderInterface
- class \Drupal\geolocation\GeocoderBase implements ContainerFactoryPluginInterface, GeocoderInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Photon
File
- modules/
geolocation_leaflet/ src/ Plugin/ geolocation/ Geocoder/ Photon.php, line 25
Namespace
Drupal\geolocation_leaflet\Plugin\geolocation\GeocoderView source
class Photon extends GeocoderBase implements GeocoderInterface {
/**
* Base URL.
*
* @var string
* Photon URL.
*/
public $requestBaseUrl = 'https://photon.komoot.io';
/**
* {@inheritdoc}
*/
protected function getDefaultSettings() {
$default_settings = parent::getDefaultSettings();
$default_settings['autocomplete_min_length'] = 1;
$default_settings['location_priority'] = [
'lat' => '',
'lng' => '',
];
$default_settings['remove_duplicates'] = FALSE;
return $default_settings;
}
/**
* {@inheritdoc}
*/
public function getOptionsForm() {
$settings = $this
->getSettings();
$form = parent::getOptionsForm();
$form['autocomplete_min_length'] = [
'#title' => $this
->t('Autocomplete minimal input length'),
'#type' => 'number',
'#min' => 1,
'#step' => 1,
'#default_value' => $settings['autocomplete_min_length'],
];
$form['location_priority'] = [
'#type' => 'geolocation_input',
'#title' => $this
->t('Location Priority'),
'#default_value' => [
'lat' => $settings['location_priority']['lat'],
'lng' => $settings['location_priority']['lng'],
],
];
$form['remove_duplicates'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Remove duplicates from the Photon API'),
'#default_value' => $settings['remove_duplicates'],
'#description' => $this
->t('The Photon API can generate duplicates for some locations (i.e. cities that are states for example), this option will remove them.'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function formAttachGeocoder(array &$render_array, $element_name) {
parent::formAttachGeocoder($render_array, $element_name);
$settings = $this
->getSettings();
$render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
'library' => [
'geolocation_leaflet/geocoder.photon',
],
'drupalSettings' => [
'geolocation' => [
'geocoder' => [
$this
->getPluginId() => [
'autocompleteMinLength' => empty($this->configuration['autocomplete_min_length']) ? 1 : (int) $this->configuration['autocomplete_min_length'],
'locationPriority' => [
'lat' => $settings['location_priority']['lat'],
'lon' => $settings['location_priority']['lng'],
],
'removeDuplicates' => $settings['remove_duplicates'],
],
],
],
],
]);
}
/**
* {@inheritdoc}
*/
public function geocode($address) {
if (empty($address)) {
return FALSE;
}
$options = [
'q' => $address,
'limit' => 1,
];
$lang = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
if (in_array($lang, [
'de',
'en',
'it',
'fr',
])) {
$options['lang'] = $lang;
}
$url = Url::fromUri($this->requestBaseUrl . '/api/', [
'query' => $options,
]);
try {
$result = Json::decode(\Drupal::httpClient()
->get($url
->toString())
->getBody());
} catch (RequestException $e) {
watchdog_exception('geolocation', $e);
return FALSE;
}
$location = [];
if (empty($result['features'][0])) {
return FALSE;
}
else {
$location['location'] = [
'lat' => $result['features'][0]['geometry']['coordinates'][1],
'lng' => $result['features'][0]['geometry']['coordinates'][0],
];
}
if (!empty($result[0]['boundingbox'])) {
$location['boundary'] = [
'lat_north_east' => $result[0]['boundingbox'][1],
'lng_north_east' => $result[0]['boundingbox'][3],
'lat_south_west' => $result[0]['boundingbox'][0],
'lng_south_west' => $result[0]['boundingbox'][2],
];
}
if (!empty($result[0]['display_name'])) {
$location['address'] = $result[0]['display_name'];
}
return $location;
}
/**
* {@inheritdoc}
*/
public function reverseGeocode($latitude, $longitude) {
$url = Url::fromUri($this->requestBaseUrl . '/reverse', [
'query' => [
'lat' => $latitude,
'lon' => $longitude,
'limit' => 20,
],
]);
try {
$result = Json::decode(\Drupal::httpClient()
->get($url
->toString())
->getBody());
} catch (RequestException $e) {
watchdog_exception('geolocation', $e);
return FALSE;
}
if (empty($result['features'][0]['properties'])) {
return FALSE;
}
$countries = \Drupal::service('address.country_repository')
->getList();
$address_atomics = [];
foreach ($result['features'] as $id => $entry) {
if (empty($entry['properties']['osm_type'])) {
continue;
}
switch ($entry['properties']['osm_type']) {
case 'N':
$address_atomics = [
'houseNumber' => !empty($entry['properties']['housenumber']) ? $entry['properties']['housenumber'] : '',
'road' => $entry['properties']['street'],
'city' => $entry['properties']['city'],
'postcode' => $entry['properties']['postcode'],
'state' => $entry['properties']['state'],
'country' => $entry['properties']['country'],
'countryCode' => array_search($entry['properties']['country'], $countries),
];
break 2;
}
}
if (empty($address_atomics)) {
return FALSE;
}
return [
'atomics' => $address_atomics,
'elements' => $this
->addressElements($address_atomics),
'formatted_address' => empty($result['display_name']) ? '' : $result['display_name'],
];
}
}
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 | |
GeocoderBase:: |
protected | property | Country formatter manager. | |
GeocoderBase:: |
protected | function | Get formatted address elements from atomics. | |
GeocoderBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
GeocoderBase:: |
public | function | Return plugin settings. | |
GeocoderBase:: |
public | function |
Process the form built above. Overrides GeocoderInterface:: |
|
GeocoderBase:: |
public | function |
GoogleGeocoderBase constructor. Overrides PluginBase:: |
1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
Photon:: |
public | property | Base URL. | |
Photon:: |
public | function |
Attach geocoding logic to input element. Overrides GeocoderBase:: |
|
Photon:: |
public | function |
Geocode an address. Overrides GeocoderBase:: |
|
Photon:: |
protected | function |
Return plugin default settings. Overrides GeocoderBase:: |
|
Photon:: |
public | function |
Return additional options form. Overrides GeocoderBase:: |
|
Photon:: |
public | function |
Reverse geocode an address. Overrides GeocoderBase:: |
|
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. |