diff --git a/config/schema/geocoder.schema.yml b/config/schema/geocoder.schema.yml index 70efdb84158313b0625b36199c337fdd76eaacc5..464c14c97471cb920c4fc997961abb3387ac9a7f 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 de8e3adea6ff38b48c08843ec9e0b6af692878a2..98bd825dd10abc9627bd1c109ced45b7d457abb1 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 0d3849618ffd9d1d037fe7beccbe2a3860ace9c1..feec640524cd347c1ac075a45a44ab5221ecd332 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); }