From 85736b5989c2bf66dd5b2f17141a160e3004b6f1 Mon Sep 17 00:00:00 2001 From: itamair <itamair@me.com> Date: Thu, 30 Nov 2023 23:12:16 +0100 Subject: [PATCH] Implementation of Geocoder additional option for Locale code/id settings, that is being used in the Geocoder Query withLocale() method. It is falling back into the Current Interface Language code/id if left empty (Issue #3403048 by tanc, itamair, ekes: Photon provider is only compatible with certain lang codes.) --- config/schema/geocoder.schema.yml | 7 +++++++ src/ProviderUsingHandlerBase.php | 5 ++++- src/Traits/ConfigurableProviderTrait.php | 23 +++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/config/schema/geocoder.schema.yml b/config/schema/geocoder.schema.yml index 70efdb84..464c14c9 100644 --- a/config/schema/geocoder.schema.yml +++ b/config/schema/geocoder.schema.yml @@ -41,6 +41,13 @@ geocoder_provider_configuration: type: geocoder_throttle_configuration label: 'Throttle' nullable: true + geocoder: + type: mapping + label: 'Geocoder Additional Options' + mapping: + locale: + type: string + label: 'Locale' geocoder_throttle_configuration: type: mapping diff --git a/src/ProviderUsingHandlerBase.php b/src/ProviderUsingHandlerBase.php index de8e3ade..98bd825d 100644 --- a/src/ProviderUsingHandlerBase.php +++ b/src/ProviderUsingHandlerBase.php @@ -98,10 +98,13 @@ abstract class ProviderUsingHandlerBase extends ProviderBase { * @throws \ReflectionException */ protected function getHandlerWrapper(): StatefulGeocoder { + // Define the locale on the basis of the geocoder additional option, + // or falling back to the current Interface language code/id. + $locale = $this->configuration['geocoder']['locale'] ?? $this->languageManager->getCurrentLanguage()->getId(); if ($this->handlerWrapper === NULL) { $this->handlerWrapper = new StatefulGeocoder( $this->getHandler(), - $this->languageManager->getCurrentLanguage()->getId() + $locale ); } diff --git a/src/Traits/ConfigurableProviderTrait.php b/src/Traits/ConfigurableProviderTrait.php index 0d384961..feec6405 100644 --- a/src/Traits/ConfigurableProviderTrait.php +++ b/src/Traits/ConfigurableProviderTrait.php @@ -7,7 +7,9 @@ namespace Drupal\geocoder\Traits; use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Core\Config\Schema\SchemaIncompleteException; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Link; use Drupal\Core\Logger\LoggerChannelTrait; +use Drupal\Core\Url; /** * Trait containing reusable code for configuring Geocoder provider plugins. @@ -121,6 +123,24 @@ trait ConfigurableProviderTrait { $form['options']['throttle']['#open'] = TRUE; } } + + $form['options']['geocoder'] = [ + '#type' => 'details', + '#title' => $this->t("Geocoder Additional Options"), + '#weight' => 15, + ]; + + $form['options']['geocoder']['locale'] = [ + '#type' => 'textfield', + '#title' => $this->t('Locale'), + '#maxlength' => 4, + '#placeholder' => $this->languageManager->getCurrentLanguage()->getId(), + '#default_value' => $this->configuration['geocoder']['locale'] ?? '', + '#description' => $this->t('Define your specific language code (en, fr, de, it, es ... etc.) that should be used / forced in the @geocoder_query_link.<br>Alter this only if specifically aware of its functionality.<br><u>If left empty the Current Interface Language code/id will be used.</u>', [ + '@geocoder_query_link' => Link::fromTextAndUrl('Geocoder Query withLocale() method', Url::fromUri('https://github.com/geocoder-php/php-common/blob/master/Query/GeocodeQuery.php#L81'))->toString(), + ]), + ]; + return $form; } @@ -149,6 +169,9 @@ trait ConfigurableProviderTrait { */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void { try { + + $this->configuration['geocoder']['locale'] = $form_state->getValue('locale'); + foreach (array_keys($this->getPluginArguments()) as $argument) { $this->configuration[$argument] = $form_state->getValue($argument); } -- GitLab