From d83e72cdf759c41775d0c909290d5eb80fe5f457 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Wed, 13 Mar 2024 12:25:00 +0200 Subject: [PATCH 1/8] UHF-9764: Add check for dream broker videos not to be rendered because they dont work --- .../custom/helfi_rekry_content/helfi_rekry_content.module | 2 ++ public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme | 7 +++++++ .../templates/layout/node--job-listing.html.twig | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) 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..22a1394e 100644 --- a/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module +++ b/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module @@ -153,6 +153,8 @@ function _helfi_rekry_content_get_media_image(string|NULL $fid = NULL): ?string * @throws \Drupal\migrate\MigrateSkipRowException */ function _helfi_rekry_content_get_video_url(string|NULL $url = NULL): ?string { + + try { $resolver = \Drupal::service('media.oembed.url_resolver'); $resolver->getProviderByUrl($url); diff --git a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme index f5603805..d12d8dcd 100644 --- a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme +++ b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme @@ -209,6 +209,13 @@ function hdbt_subtheme_preprocess_node(&$variables): void { $variables['task_area_rekry_search_url'] = Url::fromRoute('entity.node.canonical', ['node' => $search_page_nid], $options); } } + + if ($video = $node->get('field_video')?->first()?->get('entity')?->getValue()) { + $url = $video->get('field_media_oembed_video')->value; + if (str_contains($url, 'dreambroker')) { + $variables['unsupported_video'] = TRUE; + } + } } /** diff --git a/public/themes/custom/hdbt_subtheme/templates/layout/node--job-listing.html.twig b/public/themes/custom/hdbt_subtheme/templates/layout/node--job-listing.html.twig index 316d6d46..fbf15117 100644 --- a/public/themes/custom/hdbt_subtheme/templates/layout/node--job-listing.html.twig +++ b/public/themes/custom/hdbt_subtheme/templates/layout/node--job-listing.html.twig @@ -186,7 +186,7 @@ {% endif %} - {% if content.field_video|render %} + {% if content.field_video|render and not unsupported_video %}
{% include '@hdbt/component/remote-video.twig' with { video: content.field_video, From fd641723674a5c46963811f5bbbb1dbd4c6019cd Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Wed, 13 Mar 2024 12:26:39 +0200 Subject: [PATCH 2/8] UHF-9764: Add provider name check to helfi_rekry_videos migration --- .../helfi_rekry_content.module | 22 ++++++------------- .../migrations/job_listing_videos.yml | 7 ------ 2 files changed, 7 insertions(+), 22 deletions(-) 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 22a1394e..151e06ee 100644 --- a/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module +++ b/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module @@ -156,8 +156,14 @@ 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) { \Drupal::logger('helfi_rekry_content') @@ -209,20 +215,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 From 109b10ba0c81dbd1f2cb77ff519d29c6dc8a103f Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Wed, 13 Mar 2024 12:29:05 +0200 Subject: [PATCH 3/8] UHF-9764: Add comment on the url check for the field video --- public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme | 1 + 1 file changed, 1 insertion(+) diff --git a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme index d12d8dcd..318c9f04 100644 --- a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme +++ b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme @@ -210,6 +210,7 @@ function hdbt_subtheme_preprocess_node(&$variables): void { } } + // Check if there is dreambroker urls on the media referred on the field video. if ($video = $node->get('field_video')?->first()?->get('entity')?->getValue()) { $url = $video->get('field_media_oembed_video')->value; if (str_contains($url, 'dreambroker')) { From f3e289abeabe774d5f0f40e07c99e9e94255f406 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Wed, 13 Mar 2024 13:20:52 +0200 Subject: [PATCH 4/8] UHF-9764: Fix PHPCS --- .../custom/helfi_rekry_content/helfi_rekry_content.module | 2 -- 1 file changed, 2 deletions(-) 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 151e06ee..55aa5a65 100644 --- a/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module +++ b/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module @@ -153,8 +153,6 @@ function _helfi_rekry_content_get_media_image(string|NULL $fid = NULL): ?string * @throws \Drupal\migrate\MigrateSkipRowException */ 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'); From 8b80feeecdfbcd606c3ec2b76d4541324a16afa1 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Wed, 13 Mar 2024 13:28:05 +0200 Subject: [PATCH 5/8] UHF-9764: Fix PHPCS --- public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme index 318c9f04..c8e0e281 100644 --- a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme +++ b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme @@ -210,7 +210,7 @@ function hdbt_subtheme_preprocess_node(&$variables): void { } } - // Check if there is dreambroker urls on the media referred on the field video. + // Check if there are dreambroker urls on the referred media. if ($video = $node->get('field_video')?->first()?->get('entity')?->getValue()) { $url = $video->get('field_media_oembed_video')->value; if (str_contains($url, 'dreambroker')) { From f513abf78c3c5835cd48f8282b92631cbb4d9e22 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Wed, 13 Mar 2024 15:58:05 +0200 Subject: [PATCH 6/8] UHF-9764: Add update hook that goes through the remote media entities and removes the ones with dreambroker url --- .../helfi_rekry_content.install | 42 +++++++++++++++++++ .../custom/hdbt_subtheme/hdbt_subtheme.theme | 8 ---- .../layout/node--job-listing.html.twig | 2 +- 3 files changed, 43 insertions(+), 9 deletions(-) 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..af178b43 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; + } + + $medias = Media::loadMultiple($ids); + $storage_handler = \Drupal::entityTypeManager()->getStorage("media"); + + foreach ($medias as $media) { + $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/themes/custom/hdbt_subtheme/hdbt_subtheme.theme b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme index c8e0e281..f5603805 100644 --- a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme +++ b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme @@ -209,14 +209,6 @@ function hdbt_subtheme_preprocess_node(&$variables): void { $variables['task_area_rekry_search_url'] = Url::fromRoute('entity.node.canonical', ['node' => $search_page_nid], $options); } } - - // Check if there are dreambroker urls on the referred media. - if ($video = $node->get('field_video')?->first()?->get('entity')?->getValue()) { - $url = $video->get('field_media_oembed_video')->value; - if (str_contains($url, 'dreambroker')) { - $variables['unsupported_video'] = TRUE; - } - } } /** diff --git a/public/themes/custom/hdbt_subtheme/templates/layout/node--job-listing.html.twig b/public/themes/custom/hdbt_subtheme/templates/layout/node--job-listing.html.twig index fbf15117..316d6d46 100644 --- a/public/themes/custom/hdbt_subtheme/templates/layout/node--job-listing.html.twig +++ b/public/themes/custom/hdbt_subtheme/templates/layout/node--job-listing.html.twig @@ -186,7 +186,7 @@
{% endif %} - {% if content.field_video|render and not unsupported_video %} + {% if content.field_video|render %}
{% include '@hdbt/component/remote-video.twig' with { video: content.field_video, From 40245e29f0a0cf0ef1a48976d65147347ebab9f2 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Wed, 13 Mar 2024 16:08:02 +0200 Subject: [PATCH 7/8] UHF-9764: Change correct exception to _helfi_rekry_content_get_video_url --- .../custom/helfi_rekry_content/helfi_rekry_content.install | 3 ++- .../custom/helfi_rekry_content/helfi_rekry_content.module | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) 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 af178b43..79b81503 100644 --- a/public/modules/custom/helfi_rekry_content/helfi_rekry_content.install +++ b/public/modules/custom/helfi_rekry_content/helfi_rekry_content.install @@ -294,7 +294,8 @@ function helfi_rekry_content_update_9008() { $medias = Media::loadMultiple($ids); $storage_handler = \Drupal::entityTypeManager()->getStorage("media"); - foreach ($medias as $media) { + foreach ($ids as $id) { + $media = Media::load($id); $url = $media->get('field_media_oembed_video')->value; if (str_contains($url, 'dreambroker')) { 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 55aa5a65..54e77357 100644 --- a/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module +++ b/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module @@ -163,7 +163,7 @@ function _helfi_rekry_content_get_video_url(string|NULL $url = NULL): ?string { 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()); From b5b524121d61e30dd182d9f73dfc3a04045866c7 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Wed, 13 Mar 2024 16:27:11 +0200 Subject: [PATCH 8/8] UHF-9764: Fix PHPCS --- .../custom/helfi_rekry_content/helfi_rekry_content.install | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 79b81503..468a764c 100644 --- a/public/modules/custom/helfi_rekry_content/helfi_rekry_content.install +++ b/public/modules/custom/helfi_rekry_content/helfi_rekry_content.install @@ -283,7 +283,7 @@ function helfi_rekry_content_update_9007() { function helfi_rekry_content_update_9008() { $query = \Drupal::entityQuery('media') ->accessCheck(FALSE) - ->condition('bundle','remote_video'); + ->condition('bundle', 'remote_video'); $ids = $query->execute(); @@ -291,7 +291,6 @@ function helfi_rekry_content_update_9008() { return; } - $medias = Media::loadMultiple($ids); $storage_handler = \Drupal::entityTypeManager()->getStorage("media"); foreach ($ids as $id) { @@ -301,7 +300,7 @@ function helfi_rekry_content_update_9008() { if (str_contains($url, 'dreambroker')) { $query = \Drupal::entityQuery('node') ->accessCheck(FALSE) - ->condition('type','job_listing') + ->condition('type', 'job_listing') ->condition('field_video', '', '<>'); $job_listing_nodes = $query->execute();