You are here

function varbase_media_update_8705 in Varbase Media 8.7

Same name and namespace in other branches
  1. 9.0.x varbase_media.install \varbase_media_update_8705()

Follow up #3115391: Update all remote videos, set field_provider value.

1 call to varbase_media_update_8705()
varbase_media_update_8707 in ./varbase_media.install
Issue #3125946: Fix missing varbase media entity presave hook.

File

./varbase_media.install, line 335
Contains install and update for Varbase Media module.

Code

function varbase_media_update_8705() {

  // When the Remote Video media type is active.
  $media_bundle_ids = \Drupal::service('entity_type.bundle.info')
    ->getBundleInfo('media');
  if (isset($media_bundle_ids) && is_array($media_bundle_ids) && count($media_bundle_ids) > 0 && isset($media_bundle_ids['remote_video'])) {
    $url_resolver = \Drupal::service('media.oembed.url_resolver');
    $resource_fetcher = \Drupal::service('media.oembed.resource_fetcher');
    $entities = \Drupal::entityTypeManager()
      ->getStorage('media')
      ->loadByProperties([
      'bundle' => 'remote_video',
    ]);

    // When we do have remote video media entities.
    if (isset($entities) && is_array($entities) && count($entities) > 0) {

      // And no syncing for update process.
      if (!\Drupal::isConfigSyncing()) {

        // Fetch the provider name and cach it in the field_provider
        // Not to be changed or fetched again.
        foreach ($entities as $entity) {
          try {
            $resource_url = $url_resolver
              ->getResourceUrl($entity->field_media_oembed_video->value);
            $resource = $resource_fetcher
              ->fetchResource($resource_url);
            $provider = strtolower($resource
              ->getProvider()
              ->getName());
            if ($entity->field_provider->value != $provider) {
              $entity
                ->set('field_provider', $provider);
              $entity
                ->save();
            }
          } catch (Exception $e) {
          }
        }
      }
    }
  }
}