Skip to content

Commit

Permalink
Merge pull request #429 from City-of-Helsinki/dev
Browse files Browse the repository at this point in the history
Dev to main
  • Loading branch information
teroelonen authored Mar 14, 2024
2 parents 0d97950 + b26d5de commit 5c0faaf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5c0faaf

Please sign in to comment.