Skip to content

Commit

Permalink
Merge pull request #166 from City-of-Helsinki/UHF-9234
Browse files Browse the repository at this point in the history
UHF-9234: Multilingual street address search instead of search with current language
  • Loading branch information
rpnykanen committed Jan 26, 2024
2 parents 2847282 + 7940c00 commit c14c863
Showing 1 changed file with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
use Drupal\views\Plugin\views\pager\PagerPluginBase;
use Drupal\views\ViewExecutable;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Promise\Utils;

/**
* Address search for sorting the results to show the nearest units first.
Expand All @@ -30,6 +31,8 @@
*/
class AddressSearch extends FilterPluginBase {

const BASE_URL = 'https://api.hel.fi/servicemap/v2/';

/**
* Provide a simple textfield for street address.
*/
Expand Down Expand Up @@ -131,34 +134,65 @@ 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();
$currentLanguage = \Drupal::languageManager()
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId();

$langcodes = ['fi', 'sv'];
// Reorder langcodes for the result generation, set current first.
if ($langcodes[$currentLanguage]) {
$langcodes = array_unique(array_merge([$currentLanguage], $langcodes));
}

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

try {
$response = $client->get('search', [
foreach ($langcodes as $langcode) {
$queries[$langcode] = [
'query' => [
'q' => $address,
'type' => 'address',
'page' => '1',
'page_size' => '1',
'language' => $language,
'language' => $langcode,
'municipality' => 'helsinki',
],
]);
];
}

try {
$promises = [
'sv' => $client->getAsync('search', $queries['sv']),
'fi' => $client->getAsync('search', $queries['fi']),
];
$responses = Utils::unwrap($promises);
}
catch (RequestException $e) {
catch (ConnectException $e) {
\Drupal::logger('helfi_tpr')
->error(
"After school activity search\'s coordinate search failed,
error code: {$e->getCode()}"
);
return [];
}

$addressSearchResult = Json::decode($response->getBody());
foreach ($langcodes as $langcode) {
$response = $responses[$langcode];
$result = Json::decode($response->getBody());

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

$addressSearchResult = $result;
break;
}

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

Expand Down

0 comments on commit c14c863

Please sign in to comment.