Skip to content

Commit

Permalink
Merge pull request #123 from City-of-Helsinki/UHF-6298_daycare_search…
Browse files Browse the repository at this point in the history
…_language_parameter

UHF-6298: Address search language parameter
  • Loading branch information
ltpk-siili authored Sep 2, 2022
2 parents d9d4eb8 + 07a1d20 commit da43497
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Drupal\views\Plugin\views\pager\PagerPluginBase;
use Drupal\views\ViewExecutable;
Expand Down Expand Up @@ -121,26 +122,33 @@ public static function sortByAddress(ViewExecutable $view): ViewExecutable {
* Latitude and longitude coordinates in array, or empty array.
*/
protected static function fetchAddressCoordinates(string $address): array {
$language = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();

$client = new Client([
'base_uri' => 'https://api.hel.fi/servicemap/v2/',
]);

try {
$response = $client->get('search', [
'query' => [
'q' => $address,
'type' => 'address',
'page' => '1',
'page_size' => '1',
'language' => $language,
],
]);
}
catch (RequestException $e) {
return [];
}

$addressSearchResult = Json::decode($response->getBody());

if (empty($addressSearchResult["results"][0]["location"]["coordinates"][1]) ||
empty($addressSearchResult["results"][0]["location"]["coordinates"][0])) {
if (
empty($addressSearchResult["results"][0]["location"]["coordinates"][1]) ||
empty($addressSearchResult["results"][0]["location"]["coordinates"][0])
) {
return [];
}

Expand Down

0 comments on commit da43497

Please sign in to comment.