diff --git a/public/modules/custom/helfi_rekry_content/helfi_rekry_content.install b/public/modules/custom/helfi_rekry_content/helfi_rekry_content.install index 73e3f0d0..468a764c 100644 --- a/public/modules/custom/helfi_rekry_content/helfi_rekry_content.install +++ b/public/modules/custom/helfi_rekry_content/helfi_rekry_content.install @@ -5,6 +5,7 @@ * Contains install functions for HELfi Rekry Content module. */ +use Drupal\media\Entity\Media; use Drupal\migrate\MigrateExecutable; use Drupal\migrate\MigrateMessage; use Drupal\search_api\Entity\Index; @@ -275,3 +276,44 @@ function helfi_rekry_content_update_9007() { $dispatcher = \Drupal::getContainer()->get('event_dispatcher'); $dispatcher->dispatch(new ReindexScheduledEvent($jobIndex, TRUE), SearchApiEvents::REINDEX_SCHEDULED); } + +/** + * UHF-9764 Remove all videos with dreambroker provider. + */ +function helfi_rekry_content_update_9008() { + $query = \Drupal::entityQuery('media') + ->accessCheck(FALSE) + ->condition('bundle', 'remote_video'); + + $ids = $query->execute(); + + if (count($ids) === 0) { + return; + } + + $storage_handler = \Drupal::entityTypeManager()->getStorage("media"); + + foreach ($ids as $id) { + $media = Media::load($id); + $url = $media->get('field_media_oembed_video')->value; + + if (str_contains($url, 'dreambroker')) { + $query = \Drupal::entityQuery('node') + ->accessCheck(FALSE) + ->condition('type', 'job_listing') + ->condition('field_video', '', '<>'); + + $job_listing_nodes = $query->execute(); + + foreach ($job_listing_nodes as $job_listing_node) { + $node = \Drupal::entityTypeManager()->getStorage('node')->load($job_listing_node); + $referenced_id = $node->get('field_video')?->first()?->get('entity')?->getValue()->id(); + + if ($referenced_id === $media->id()) { + $node->set('field_video', []); + } + } + $storage_handler->delete([$media]); + } + } +} diff --git a/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module b/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module index 5ba7c0e4..54e77357 100644 --- a/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module +++ b/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module @@ -154,10 +154,16 @@ function _helfi_rekry_content_get_media_image(string|NULL $fid = NULL): ?string */ function _helfi_rekry_content_get_video_url(string|NULL $url = NULL): ?string { try { + /** @var \Drupal\media\OEmbed\UrlResolverInterface $resolver */ $resolver = \Drupal::service('media.oembed.url_resolver'); - $resolver->getProviderByUrl($url); + + $provider = $resolver->getProviderByUrl($url); + + if (!in_array($provider->getName(), ['YouTube', 'Icareus Suite'])) { + throw new MigrateSkipRowException(); + } } - catch (\throwable $e) { + catch (ResourceException | ProviderException $e) { \Drupal::logger('helfi_rekry_content') ->notice('Video embed url "' . $url . '" failed validation with message: ' . $e->getMessage()); @@ -207,20 +213,6 @@ function _helfi_rekry_content_sanitize_video_url(string $url): string { return $url; } -/** - * Get media entity by file id. - * - * @param string|null $url - * The video url. - * - * @throws \Drupal\migrate\MigrateSkipRowException - */ -function _helfi_rekry_content_check_video_existance(string|NULL $url = NULL): void { - if (_helfi_rekry_content_lookup_video_mid($url)) { - throw new MigrateSkipRowException(); - } -} - /** * Get video mid by video url. * diff --git a/public/modules/custom/helfi_rekry_content/migrations/job_listing_videos.yml b/public/modules/custom/helfi_rekry_content/migrations/job_listing_videos.yml index 308432a0..668f5b3f 100644 --- a/public/modules/custom/helfi_rekry_content/migrations/job_listing_videos.yml +++ b/public/modules/custom/helfi_rekry_content/migrations/job_listing_videos.yml @@ -47,13 +47,6 @@ process: plugin: callback callable: _helfi_rekry_content_sanitize_video_url source: video - - - plugin: callback - callable: _helfi_rekry_content_check_video_existance - - - plugin: callback - callable: _helfi_rekry_content_sanitize_video_url - source: video - plugin: callback callable: _helfi_rekry_content_get_video_url