You are here

public function Geocoder::geocode in Geocoder 8.2

Same name and namespace in other branches
  1. 8.3 src/Geocoder.php \Drupal\geocoder\Geocoder::geocode()
  2. 7.2 src/Geocoder.php \Drupal\geocoder\Geocoder::geocode()

Geocodes a string.

Parameters

string $data: The string to geocoded.

string[] $plugins: A list of plugin identifiers to use.

array $options: (optional) An associative array with plugin options, keyed plugin by the plugin id. Defaults to an empty array. These options would be merged with (and would override) plugins options set in the module configurations.

Return value

\Geocoder\Model\AddressCollection|\Geometry|null An address collection or NULL on geocoding failure.

Overrides GeocoderInterface::geocode

File

src/Geocoder.php, line 44

Class

Geocoder
Provides a geocoder factory class.

Namespace

Drupal\geocoder

Code

public function geocode($data, array $plugins, array $options = []) {

  // Retrieve plugins options from the module configurations.
  $plugins_options = $this->config
    ->get('plugins_options') ?: [];

  // Merge possible options overrides into plugins options.
  $plugins_options = NestedArray::mergeDeep($plugins_options, $options);
  foreach ($plugins as $plugin_id) {

    // Transform in empty array a null value for the plugin id options.
    $plugins_options += [
      $plugin_id => [],
    ];
    try {
      $provider = $this->providerPluginManager
        ->createInstance($plugin_id, $plugins_options[$plugin_id]);
      return $provider
        ->geocode($data);
    } catch (\Exception $e) {
      static::log($e
        ->getMessage());
    }
  }
  return FALSE;
}