Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-10887 Hearings #866

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,56 +134,30 @@ public function query(

foreach ($results as $hearing) {
$existingTranslations = $this->getTranslationLanguages($hearing);
$selectedLangcode = $this->resolveLanguage($hearing, $langcode);
if (!in_array($langcode, $existingTranslations)) {
continue;
}

$item = $hearing;
$item += [
'main_image_url' => $hearing['main_image']['url'],
'main_image' => Url::fromUri($hearing['main_image']['url']),
'count' => $json['count'],
'url' => sprintf('%s%s', self::HEARING_URL, $hearing['slug']),
'langcode' => $selectedLangcode,
'langcode' => $langcode,
'existing_translations' => implode(',', $existingTranslations),
];

$item['title'] = $hearing['title'][$selectedLangcode] ?? $hearing['title']['fi'] ?? '';
$item['abstract'] = $hearing['abstract'][$selectedLangcode] ?? $hearing['abstract']['fi'] ?? '';
$item['main_image_caption'] = $hearing['main_image']['caption'][$selectedLangcode]
?? $hearing['main_image']['caption']['fi']
?? '';
$item['title'] = $hearing['title'][$langcode] ?? '';
$item['abstract'] = $hearing['abstract'][$langcode] ?? '';
$item['main_image_caption'] = $hearing['main_image']['caption'][$langcode] ?? '';

$data[] = $item;
}

return $data;
}

/**
* Get language that exists on a hearing, preferably current language.
*
* @param array $hearing
* The hearing.
* @param string $currentLangCode
* Requested language code.
*
* @return string
* Language code that can be used to show the hearing.
*/
private function resolveLanguage(array $hearing, string $currentLangCode): string {
$existingTranslations = $this->getTranslationLanguages($hearing);
if (in_array($currentLangCode, $existingTranslations)) {
return $currentLangCode;
}

$possibleLanguages = ['fi', 'en', 'sv'];
foreach ($possibleLanguages as $langcode) {
if (in_array($langcode, $existingTranslations)) {
return $langcode;
}
}
throw new \InvalidArgumentException('Failed to resolve language.');
}

/**
* Get all translations for hearing.
*
Expand Down
Loading