Unverified Commit a0a28bdd authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3438618 by larowlan, longwave, alexpott, martijn de wit, askibinski,...

Issue #3438618 by larowlan, longwave, alexpott, martijn de wit, askibinski, seanb: Allow opting out of oembed discovery
parent eab46c49
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -2,3 +2,4 @@ icon_base_uri: 'public://media-icons/generic'
iframe_domain: ~
oembed_providers_url: 'https://oembed.com/providers.json'
standalone_url: false
oembed_discovery: false
+3 −0
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@ media.settings:
    standalone_url:
      type: boolean
      label: 'Allow media items to be viewed standalone at /media/{id}'
    oembed_discovery:
      type: boolean
      label: 'Allow oEmbed discovery'

media.type.*:
  type: config_entity
+7 −0
Original line number Diff line number Diff line
@@ -27,3 +27,10 @@ function media_removed_post_updates(): array {
function media_post_update_media_author_views_filter_update(): void {
  // Empty update function to clear the Views data cache.
}

/**
 * Set the oembed_discovery setting.
 */
function media_post_update_set_oembed_discovery(): void {
  \Drupal::configFactory()->getEditable('media.settings')->set('oembed_discovery', TRUE)->save(TRUE);
}
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ services:
    parent: default_plugin_manager
  media.oembed.url_resolver:
    class: Drupal\media\OEmbed\UrlResolver
    arguments: ['@media.oembed.provider_repository', '@media.oembed.resource_fetcher', '@http_client', '@module_handler', '@cache.default']
    arguments: ['@media.oembed.provider_repository', '@media.oembed.resource_fetcher', '@http_client', '@module_handler', '@cache.default', '@config.factory']
  Drupal\media\OEmbed\UrlResolverInterface: '@media.oembed.url_resolver'
  media.oembed.provider_repository:
    class: Drupal\media\OEmbed\ProviderRepository
+19 −15
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use GuzzleHttp\ClientInterface;
use Psr\Http\Client\ClientExceptionInterface;
@@ -63,24 +64,24 @@ class UrlResolver implements UrlResolverInterface {

  /**
   * Constructs a UrlResolver object.
   *
   * @param \Drupal\media\OEmbed\ProviderRepositoryInterface $providers
   *   The oEmbed provider repository service.
   * @param \Drupal\media\OEmbed\ResourceFetcherInterface $resource_fetcher
   *   The OEmbed resource fetcher service.
   * @param \GuzzleHttp\ClientInterface $http_client
   *   The HTTP client.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   The cache backend.
   */
  public function __construct(ProviderRepositoryInterface $providers, ResourceFetcherInterface $resource_fetcher, ClientInterface $http_client, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend) {
  public function __construct(
    ProviderRepositoryInterface $providers,
    ResourceFetcherInterface $resource_fetcher,
    ClientInterface $http_client,
    ModuleHandlerInterface $module_handler,
    CacheBackendInterface $cache_backend,
    protected ?ConfigFactoryInterface $configFactory = NULL,
  ) {
    $this->providers = $providers;
    $this->resourceFetcher = $resource_fetcher;
    $this->httpClient = $http_client;
    $this->moduleHandler = $module_handler;
    $this->cacheBackend = $cache_backend;
    if ($configFactory === NULL) {
      $this->configFactory = \Drupal::configFactory();
      @trigger_error('Calling ' . __METHOD__ . ' without the $configFactory argument is deprecated in drupal:11.1.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3481570', E_USER_DEPRECATED);
    }
  }

  /**
@@ -136,9 +137,12 @@ public function getProviderByUrl($url) {
      }
    }

    if ($this->configFactory->get('media.settings')->get('oembed_discovery')) {
      $resource_url = $this->discoverResourceUrl($url);
      if ($resource_url) {
      return $this->resourceFetcher->fetchResource($resource_url)->getProvider();
        return $this->resourceFetcher->fetchResource($resource_url)
          ->getProvider();
      }
    }

    throw new ResourceException('No matching provider found.', $url);
Loading