Skip to content

Commit

Permalink
Merge pull request #884 from City-of-Helsinki/UHF-10406
Browse files Browse the repository at this point in the history
UHF-10406: Catch exceptions from UrlResolverInterface::getProviderByUrl
  • Loading branch information
hyrsky authored Jan 13, 2025
2 parents 444d9da + df07e70 commit fb0ab91
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\helfi_media_remote_video\Entity\RemoteVideo;
use Drupal\media\OEmbed\Resource;
use Drupal\media\OEmbed\ResourceException;
use Drupal\media\OEmbed\UrlResolverInterface;

/**
* Implements hook_media_source_info_alter().
Expand Down Expand Up @@ -186,7 +187,8 @@ function _helfi_media_remote_video_remote_video_validate(array $form, FormStateI
*/
function _helfi_media_remote_video_remote_video_url_handler(string $video_url): false|string {
$converted_url = FALSE;
$url_resolver = \Drupal::service('media.oembed.url_resolver');
/** @var \Drupal\media\OEmbed\UrlResolverInterface $url_resolver */
$url_resolver = \Drupal::service(UrlResolverInterface::class);
$provider = $url_resolver->getProviderByUrl($video_url);
$helsinki_kanava_url_pattern = 'https://www.helsinkikanava.fi/*/web/helsinkikanava/player/vod?assetId=*';

Expand Down
17 changes: 13 additions & 4 deletions modules/helfi_media_remote_video/src/Entity/RemoteVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Drupal\helfi_media\Entity\MediaEntityBundle;
use Drupal\media\MediaInterface;
use Drupal\media\OEmbed\UrlResolverInterface;

/**
* Bundle class for remote_video media.
Expand All @@ -22,10 +23,18 @@ public function getServiceUrl(): ?string {
if (!$this->hasProvider()) {
return NULL;
}
$url_resolver = \Drupal::service('media.oembed.url_resolver');
/** @var \Drupal\media\OEmbed\UrlResolverInterface $url_resolver */
$url_resolver = \Drupal::service(UrlResolverInterface::class);
$video_url = $this->get('field_media_oembed_video')->value;
$provider = $url_resolver->getProviderByUrl($video_url);
return rtrim($provider->getUrl(), '/');
try {
$provider = $url_resolver->getProviderByUrl($video_url);
return rtrim($provider->getUrl(), '/');
}
// UrlResolverInterface::getProviderByUrl makes
// network requests that can fail.
catch (\Exception) {
return NULL;
}
}

/**
Expand All @@ -34,7 +43,7 @@ public function getServiceUrl(): ?string {
* @return mixed
* The video title.
*/
public function getMediaTitle() {
public function getMediaTitle(): mixed {
return $this->get('field_media_oembed_video')
->iframe_title;
}
Expand Down

0 comments on commit fb0ab91

Please sign in to comment.