Skip to content

Commit

Permalink
Merge pull request #761 from City-of-Helsinki/UHF-10059
Browse files Browse the repository at this point in the history
UHF-10059 Remote video via media library
  • Loading branch information
rpnykanen authored May 22, 2024
2 parents 6c7a81b + 98eccf9 commit f242ff9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
23 changes: 17 additions & 6 deletions modules/helfi_media_remote_video/helfi_media_remote_video.module
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function _helfi_media_remote_video_remote_video_validate(array $form, FormStateI

$user_input = $form_state->getUserInput();
$video_url = FALSE;
$skip_url_validation = FALSE;

// Helsinki-kanava video can be added via media library or as a new media
// entity. Handle the URL in both cases.
Expand All @@ -133,21 +134,31 @@ function _helfi_media_remote_video_remote_video_validate(array $form, FormStateI
) {
$oembed_video_field = 'url';
$video_url = &$user_input[$oembed_video_field];
$skip_url_validation = TRUE;
}

// Convert the video url if needed.
if ($video_url) {
try {
$converted_url = _helfi_media_remote_video_remote_video_url_handler($video_url);
}
catch (ResourceException $e) {
catch (Exception $e) {
$form_state->setErrorByName('url', $e->getMessage());
return $form;
}

if ($converted_url) {
$video_url = $converted_url;
$form_state->setUserInput($user_input);
$form_state->setValue($oembed_video_field, $user_input[$oembed_video_field]);

// Do not validate the media library form from this point forward.
// The user inserted URL is cached by media library form and the Oembed
// provider would try to validate the unconverted URL. Set the form
// validation complete to avoid this.
if ($skip_url_validation) {
$form_state->setValidationComplete();
}
}
}

Expand All @@ -167,6 +178,9 @@ function _helfi_media_remote_video_remote_video_validate(array $form, FormStateI
*
* @return false|string
* Returns FALSE or converted video URL.
*
* @throws \Exception
* If the URL is missing asset ID.
*/
function _helfi_media_remote_video_remote_video_url_handler(string $video_url): false|string {
$converted_url = FALSE;
Expand Down Expand Up @@ -197,18 +211,15 @@ function _helfi_media_remote_video_remote_video_url_handler(string $video_url):

// Try to convert the URL if it's of form 'web/helsinkikanava/player/webcast'.
if (str_contains($video_url, '/web/helsinkikanava/player/webcast')) {
$lang_code = '';
preg_match('/assetId=(\d+)/', $video_url, $asset_id_matches);
$asset_id = $asset_id_matches[1];

if (empty($asset_id)) {
return $converted_url;
throw new Exception('URL is missing asset ID parameter.');
}

// @todo figure out if this is needed.
// $lang_code = empty($lang_code) ? 'fi' : $lang_code;
$url_parts = explode('*', $helsinki_kanava_url_pattern);
$converted_url = $url_parts[0] . $lang_code . $url_parts[1] . $asset_id;
$converted_url = $url_parts[0] . 'fi' . $url_parts[1] . $asset_id;
}

// Return the converted URL.
Expand Down
10 changes: 10 additions & 0 deletions translations/override/fi.po
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ msgstr "Käyttäjä"
msgid "Yes"
msgstr "Kyllä"

msgid "Add @type via URL"
msgstr "Lisää @type"

msgid "No matching provider found."
msgstr "Tarkista URL-osoite. Osoitteen perusteella ei voitu määrittää palveluntarjoajaa."

# Content lock
msgid "Break Lock for content @label"
msgstr "Vahvista lukituksen poisto sivulta @label"
Expand Down Expand Up @@ -96,6 +102,10 @@ msgstr "Korvaa alkuperäinen tiedosto (@originalExtension)"
msgid "Replace file"
msgstr "Korvaa tiedosto"

# Media library
msgid "The media item has been created but has not yet been saved. Fill in any required fields and save to add it to the media library."
msgstr "Media on luotu, mutta sitä ei ole vielä tallennettu. Täytä kaikki pakolliset kentät ja tallenna lisätäksesi se mediakirjastoon."

# Menu
msgid "Parent link"
msgstr "Yläsivu"
Expand Down

0 comments on commit f242ff9

Please sign in to comment.