Commit 12c5e9f4 authored by Dieter Holvoet's avatar Dieter Holvoet Committed by Italo Mairo
Browse files

Issue #3176070 by JeroenT, DieterHolvoet: Unable to geocode partial addresses with Mapbox provider

parent f156660d
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -157,7 +157,18 @@ geocoder_provider.configuration.mapbox:
      description: '<a href="https://www.mapbox.com/signup/" target="blank">Get your free API key</a>'
    country:
      type: string
      label: 'Limit results to one or more countries. Permitted values are <a href="https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2" target="blank">ISO 3166 alpha 2</a> country codes separated by commas.'
      label: 'Countries'
      description: 'Limit results to one or more countries. Permitted values are <a href="https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2" target="blank">ISO 3166 alpha 2</a> country codes separated by commas.'
      nullable: true
    location_type:
      type: string
      label: 'Location type'
      description: 'The location type. Check <a href="https://docs.mapbox.com/api/search/geocoding/#data-types" target="_blank">https://docs.mapbox.com/api/search/geocoding/#data-types</a> for all possible location types. Multiple location types can be separated by a comma.'
      nullable: true
    fuzzy_match:
      type: boolean
      label: 'Fuzzy match'
      description: 'Specify whether the Geocoding API should attempt approximate, as well as exact, matching when performing searches (true, default), or whether it should opt out of this behavior and only attempt exact matching (false). For example, the default setting might return Washington, DC for a query of Washington, even though the query was misspelled.'
      nullable: true

geocoder_provider.configuration.mapquest:
+76 −2
Original line number Diff line number Diff line
@@ -2,7 +2,11 @@

namespace Drupal\geocoder\Plugin\Geocoder\Provider;

use Drupal\Core\Form\FormStateInterface;
use Drupal\geocoder\ConfigurableProviderUsingHandlerWithAdapterBase;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Provider\Mapbox\Mapbox as MapboxProvider;

/**
 * Provides a Mapbox geocoder provider plugin.
@@ -13,8 +17,78 @@ use Drupal\geocoder\ConfigurableProviderUsingHandlerWithAdapterBase;
 *   handler = "\Geocoder\Provider\Mapbox\Mapbox",
 *   arguments = {
 *     "accessToken" = "",
 *     "country" = ""
 *     "country" = "",
 *   }
 * )
 */
class Mapbox extends ConfigurableProviderUsingHandlerWithAdapterBase {}
class Mapbox extends ConfigurableProviderUsingHandlerWithAdapterBase {

  /**
   * {@inheritdoc}
   */
  protected function doGeocode($source) {
    $this->throttle->waitForAvailability($this->pluginId, $this->configuration['throttle'] ?? []);

    if (!$this->getHandler() instanceof Provider) {
      return NULL;
    }

    $query = GeocodeQuery::create($source)
      ->withData('fuzzy_match', $this->configuration['fuzzy_match'])
      ->withData('location_type', explode(',', $this->configuration['location_type']));

    return $this->getHandlerWrapper()->geocodeQuery($query);
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration(): array {
    return [
      'location_type' => MapboxProvider::DEFAULT_TYPE,
      'fuzzy_match' => FALSE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
    $form = parent::buildConfigurationForm($form, $form_state);

    $config_schema_definition = $this->getConfigSchemaDefinition();

    $location_type_definition = $config_schema_definition['mapping']['location_type'];
    $form['options']['location_type'] = [
      '#type' => 'textfield',
      '#title' => $location_type_definition['label'] ?? '',
      '#description' => $location_type_definition['description'] ?? '',
      '#default_value' => $this->configuration['location_type'],
      '#required' => empty($location_type_definition['nullable']),
      '#weight' => 5,
    ];

    $fuzzy_match_definition = $config_schema_definition['mapping']['fuzzy_match'];
    $form['options']['fuzzy_match'] = [
      '#type' => 'checkbox',
      '#title' => $fuzzy_match_definition['label'] ?? '',
      '#description' => $fuzzy_match_definition['description'] ?? '',
      '#default_value' => $this->configuration['fuzzy_match'],
      '#required' => empty($fuzzy_match_definition['nullable']),
      '#weight' => 5,
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
    parent::submitConfigurationForm($form, $form_state);

    $this->configuration['location_type'] = $form_state->getValue('location_type');
    $this->configuration['fuzzy_match'] = $form_state->getValue('fuzzy_match');
  }

}
+1 −0
Original line number Diff line number Diff line
@@ -100,6 +100,7 @@ trait ConfigurableProviderTrait {
      Be aware that if you bulk geocode with a hard throttle, it may take a long time or even reach the maximum execution time."),
      '#open' => FALSE,
      '#tree' => TRUE,
      '#weight' => 10,
    ];
    foreach ($this->getThrottleOptions() as $option => $option_definition) {
      $form['options']['throttle'][$option] = [