diff -u b/core/modules/media/src/OEmbed/ProviderDiscoveryInterface.php b/core/modules/media/src/OEmbed/ProviderDiscoveryInterface.php --- b/core/modules/media/src/OEmbed/ProviderDiscoveryInterface.php +++ b/core/modules/media/src/OEmbed/ProviderDiscoveryInterface.php @@ -38,8 +38,8 @@ * - formats: List of supported formats. Can be "json", "xml" or both. * - discovery: Whether the provider supports oEmbed discovery. * - * @throws \InvalidArgumentException if there is no known oEmbed provider with - * the specified name. + * @throws \InvalidArgumentException + * If there is no known oEmbed provider with the specified name. */ public function get($provider_name); diff -u b/core/modules/media/src/OEmbed/ProviderException.php b/core/modules/media/src/OEmbed/ProviderException.php --- b/core/modules/media/src/OEmbed/ProviderException.php +++ b/core/modules/media/src/OEmbed/ProviderException.php @@ -12,9 +12,9 @@ /** * Information about the oEmbed provider which caused the exception. * - * @see \Drupal\media\OEmbed\ProviderDiscoveryInterface::get() - * * @var array + * + * @see \Drupal\media\OEmbed\ProviderDiscoveryInterface::get() */ protected $provider = []; diff -u b/core/modules/media/src/OEmbed/ResourceException.php b/core/modules/media/src/OEmbed/ResourceException.php --- b/core/modules/media/src/OEmbed/ResourceException.php +++ b/core/modules/media/src/OEmbed/ResourceException.php @@ -44,6 +44,7 @@ * Gets the URL of the oEmbed resource which caused the exception. * * @return string + * The URL of the oEmbed resource which caused the exception. */ public function getUrl() { return $this->url; @@ -53,6 +54,7 @@ * Gets the oEmbed resource which caused the exception, if available. * * @return array + * The oEmbed resource which caused the exception. */ public function getResource() { return $this->resource; diff -u b/core/modules/media/src/OEmbed/ResourceFetcher.php b/core/modules/media/src/OEmbed/ResourceFetcher.php --- b/core/modules/media/src/OEmbed/ResourceFetcher.php +++ b/core/modules/media/src/OEmbed/ResourceFetcher.php @@ -121,6 +121,20 @@ return $resource_url; } + /** + * Builds the endpoint URL with the oEmebed provider info and returns it. + * + * @param array $provider_info + * Provider info returned form + * Drupal\media\OEmbed\ProviderDiscoveryInterface::get(). + * @param string $url + * The canonical media URL. + * + * @return bool|string + * URL of the oEmbed endpoint, or FALSE if the building was not successful. + * + * @throws \Drupal\media\OEmbed\ProviderException + */ protected function buildResourceUrl(array $provider_info, $url) { if (empty($provider_info['endpoints'])) { throw new ProviderException('Provider @name does not define any endpoints.', $provider_info); @@ -173,6 +187,17 @@ return $this->findUrl($xpath, 'json') ?: $this->findUrl($xpath, 'xml'); } + /** + * Tries to find the oEmbed URL in a DOM. + * + * @param \DOMXPath $xpath + * Page HTML as DOMXPath. + * @param string $format + * Format of oEmbed resource. Possible values are 'json' and 'xml'. + * + * @return bool|string + * A URL to an oEmbed resource or FALSE if not found. + */ protected function findUrl(\DOMXPath $xpath, $format) { $result = $xpath->query("//link[@type='application/$format+oembed']"); return $result->length ? $result->item(0)->getAttribute('href') : FALSE; diff -u b/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php b/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php --- b/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php +++ b/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php @@ -7,8 +7,8 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\media\Exception\ResourceException; -use Drupal\media\ResourceFetcherInterface; +use Drupal\media\OEmbed\ResourceException; +use Drupal\media\OEmbed\ResourceFetcherInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -29,7 +29,7 @@ /** * The oEmbed service. * - * @var \Drupal\media\ResourceFetcherInterface + * @var \Drupal\media\OEmbed\ResourceFetcherInterface */ protected $oEmbed; @@ -50,7 +50,7 @@ * The view mode. * @param array $third_party_settings * Any third party settings settings. - * @param \Drupal\media\ResourceFetcherInterface $oembed + * @param \Drupal\media\OEmbed\ResourceFetcherInterface $oembed * The oEmbed service. */ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ResourceFetcherInterface $oembed) { @@ -70,7 +70,7 @@ $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], - $container->get('media.oembed') + $container->get('media.oembed.resource_fetcher') ); } @@ -96,9 +96,14 @@ try { $resource = $this->oEmbed->fetchResource($this->oEmbed->getResourceUrl($item->{$main_property}, $this->getSetting('maxwidth'), $this->getSetting('maxheight'))); $element[$delta] = [ - '#theme' => 'media_oembed', - '#post' => (string) $resource['html'], + '#markup' => $this->t('The oEmbed provider does not support a HTML output'), ]; + if (!empty($resource['html'])) { + $element[$delta] = [ + '#theme' => 'media_oembed', + '#post' => (string) $resource['html'], + ]; + } } catch (ResourceException $exception) { drupal_set_message($this->t('Could not retrieve the remote URL.'), 'error'); diff -u b/core/modules/media/src/Plugin/media/Source/OEmbed.php b/core/modules/media/src/Plugin/media/Source/OEmbed.php --- b/core/modules/media/src/Plugin/media/Source/OEmbed.php +++ b/core/modules/media/src/Plugin/media/Source/OEmbed.php @@ -233,7 +233,7 @@ */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { parent::submitConfigurationForm($form, $form_state); - $this->configuration['allowed_providers'] = array_values($this->configuration['allowed_providers']); + $this->configuration['allowed_providers'] = array_filter(array_values($this->configuration['allowed_providers'])); } /**