Skip to content

Commit

Permalink
Merge pull request #8 from KalleVuorjoki/fix/HUB-1131-fix-wsod-during…
Browse files Browse the repository at this point in the history
…-maintenance

HUB-1131: Fix WSOD during Unitube maintenance breaks
  • Loading branch information
KalleVuorjoki authored Aug 3, 2023
2 parents a2a7ac8 + fc27e66 commit 35f15fa
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/Plugin/video_embed_field/Provider/UniTube.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ class UniTube extends ProviderPluginBase {
* {@inheritdoc}
*/
public function renderEmbedCode($width, $height, $autoplay) {
$name = $this->getName();
if (!$name) {
return [];
}
return [
'#prefix' => '<div>',
'#suffix' => '</div>',
'#type' => 'video_embed_iframe',
'#provider' => 'unitube',
'#url' => sprintf('https://unitube.it.helsinki.fi/unitube/embed.html?id=%s&play=false', $this->getVideoId()),
'#attributes' => [
'title' => $this->getName(),
'title' => $name,
'width' => $width,
'height' => $height,
'scrolling' => 'no',
Expand All @@ -45,20 +49,25 @@ public function renderEmbedCode($width, $height, $autoplay) {
* @return array|mixed
*/
protected function getMetadata() {
try {
// Perform an request to metadata and ensure we get valid response code
$response = $this->httpClient->request('GET', 'https://webcast.helsinki.fi/search/episode.json?id=' . $this->getVideoId());
if ($response->getStatusCode() != 200) {
return [];
}

// Perform an request to metadata and ensure we get valid response code
$response = $this->httpClient->request('GET', 'https://webcast.helsinki.fi/search/episode.json?id=' . $this->getVideoId());
if ($response->getStatusCode() != 200) {
return [];
}
// Parse JSON and get attachments
$parsed_response = json_decode((string) $response->getBody(), TRUE);
if (json_last_error()) {
return [];
}

// Parse JSON and get attachments
$parsed_response = json_decode((string) $response->getBody(), TRUE);
if (json_last_error()) {
return [];
return $parsed_response;
}

return $parsed_response;
catch (\Exception $e) {
\Drupal::logger('video_embed_unitube')->error('There was an error downloading metadata. Message: @message.', ['@message' => $e->getMessage()]);
}
return NULL;
}

/**
Expand Down Expand Up @@ -104,6 +113,10 @@ public static function getIdFromInput($input) {
*/
public function getName() {
$metadata = $this->getMetadata();
if (!$metadata) {
// In case of maintenance break.
return NULL;
}
if (isset($metadata['search-results']['result']['dcTitle'])) {
$title = $metadata['search-results']['result']['dcTitle'];
return $this->t('@provider Video (@id)', ['@provider' => $this->getPluginDefinition()['title'], '@id' => $title]);
Expand Down

0 comments on commit 35f15fa

Please sign in to comment.