You are here

class Photon in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 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.de for details."),
  locationCapable = true,
  boundaryCapable = true,
  frontendCapable = true,
  reverseCapable = false,
)

Hierarchy

Expanded class hierarchy of Photon

File

modules/geolocation_leaflet/src/Plugin/geolocation/Geocoder/Photon.php, line 25

Namespace

Drupal\geolocation_leaflet\Plugin\geolocation\Geocoder
View source
class Photon extends GeocoderBase implements GeocoderInterface {

  /**
   * {@inheritdoc}
   */
  protected function getDefaultSettings() {
    $default_settings = parent::getDefaultSettings();
    $default_settings['location_priority'] = [
      'lat' => '',
      'lng' => '',
    ];
    return $default_settings;
  }

  /**
   * {@inheritdoc}
   */
  public function getOptionsForm() {
    $settings = $this
      ->getSettings();
    $form = parent::getOptionsForm();
    $form['location_priority'] = [
      '#type' => 'geolocation_input',
      '#title' => $this
        ->t('Location Priority'),
      '#default_value' => [
        'lat' => $settings['location_priority']['lat'],
        'lng' => $settings['location_priority']['lng'],
      ],
    ];
    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() => [
              'locationPriority' => [
                'lat' => $settings['location_priority']['lat'],
                'lon' => $settings['location_priority']['lng'],
              ],
            ],
          ],
        ],
      ],
    ]);
  }

  /**
   * {@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('https://photon.komoot.de/api/' . $address, [
      '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[0])) {
      return FALSE;
    }
    else {
      $location['location'] = [
        'lat' => $result[0]['lat'],
        'lng' => $result[0]['lon'],
      ];
    }
    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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
GeocoderBase::$countryFormatterManager protected property Country formatter manager.
GeocoderBase::addressElements protected function Get formatted address elements from atomics.
GeocoderBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
GeocoderBase::getSettings public function Return plugin settings.
GeocoderBase::processOptionsForm public function Process the form built above. Overrides GeocoderInterface::processOptionsForm
GeocoderBase::reverseGeocode public function Reverse geocode an address. Overrides GeocoderInterface::reverseGeocode 2
GeocoderBase::__construct public function GoogleGeocoderBase constructor. Overrides PluginBase::__construct 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
Photon::formAttachGeocoder public function Attach geocoding logic to input element. Overrides GeocoderBase::formAttachGeocoder
Photon::geocode public function Geocode an address. Overrides GeocoderBase::geocode
Photon::getDefaultSettings protected function Return plugin default settings. Overrides GeocoderBase::getDefaultSettings
Photon::getOptionsForm public function Return additional options form. Overrides GeocoderBase::getOptionsForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.