Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev to main #429

Merged
merged 10 commits into from
Mar 14, 2024
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
Loading