diff -u b/core/modules/media/media.module b/core/modules/media/media.module --- b/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -9,9 +9,12 @@ use Drupal\Core\Session\AccountInterface; use Drupal\field\FieldConfigInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; +use Drupal\link\Plugin\Field\FieldWidget\LinkWidget; +use Drupal\media\MediaInterface; use Drupal\media\Plugin\media\Source\OEmbed; /** @@ -45,11 +48,6 @@ 'media' => [ 'render element' => 'elements', ], - 'media_oembed' => [ - 'variables' => [ - 'post' => NULL, - ], - ], ]; } @@ -91,4 +89,45 @@ /** + * Implements hook_field_widget_form_alter(). + */ +function media_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) { + if ($context['widget'] instanceof LinkWidget && !empty($element['uri'])) { + + /** @var \Drupal\media\MediaInterface $media */ + $media = $context['items']->getEntity(); + if (!($media instanceof MediaInterface)) { + return; + } + + $field_definition = $context['items']->getFieldDefinition(); + $source_config = $media->getSource()->getConfiguration(); + if ($source_config['source_field'] != $field_definition->getName()) { + return; + } + + $source_definition = $media->getSource()->getPluginDefinition(); + $allowed_providers = $source_definition['settings']['supported_providers']; + if (!empty($source_config['allowed_providers'])) { + $allowed_providers = $source_config['allowed_providers']; + } + + $message = t('These oEmbed providers are allowed: @providers', ['@providers' => implode(', ', $allowed_providers)]); + + if (is_array($element['uri']['#description']) && !empty($element['uri']['#description']['#items'])) { + $element['uri']['#description']['#items'][] = $message; + } + else { + $element['uri']['#description'] = [ + '#theme' => 'item_list', + '#items' => [ + $element['uri']['#description'], + $message, + ], + ]; + } + } +} + +/** * Prepares variables for media templates. * diff -u b/core/modules/media/src/OEmbed/ProviderDiscovery.php b/core/modules/media/src/OEmbed/ProviderDiscovery.php --- b/core/modules/media/src/OEmbed/ProviderDiscovery.php +++ b/core/modules/media/src/OEmbed/ProviderDiscovery.php @@ -59,7 +59,7 @@ } try { - $response = $this->httpClient->get($this->providersUrl); + $response = $this->httpClient->request('GET', $this->providersUrl); } catch (RequestException $e) { throw new ProviderException("Could not retrieve the oEmbed provider database from $this->providersUrl", [], $e); 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 @@ -33,6 +33,7 @@ $variables = [ '@name' => isset($provider['provider_name']) ? $provider['provider_name'] : '', ]; + $this->provider = $provider; $message = (string) new FormattableMarkup($message, $variables); parent::__construct($message, 0, $previous); } diff -u b/core/modules/media/src/OEmbed/ResourceFetcherInterface.php b/core/modules/media/src/OEmbed/ResourceFetcherInterface.php --- b/core/modules/media/src/OEmbed/ResourceFetcherInterface.php +++ b/core/modules/media/src/OEmbed/ResourceFetcherInterface.php @@ -21,6 +21,8 @@ * If a match was found, will return TRUE, otherwise FALSE. * * @see \Drupal\media\ResourceFetcherInterface::getProviders() + * + * @throws \Drupal\media\OEmbed\ResourceException */ public function getResourceUrl($url, $max_width = NULL, $max_height = NULL); @@ -33,6 +35,8 @@ * @return array|bool * Resource information as returned from the oEmbed endpoint, or FALSE if * the resource could not be fetched. + * + * @throws \Drupal\media\OEmbed\ResourceException */ public function fetchResource($endpoint_url); 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 @@ -6,7 +6,9 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Url; use Drupal\media\OEmbed\ResourceException; use Drupal\media\OEmbed\ResourceFetcherInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -34,6 +36,13 @@ protected $oEmbed; /** + * The messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface $messenger + */ + protected $messenger; + + /** * Constructs a EntityReferenceEntityFormatter instance. * * @param string $plugin_id @@ -52,10 +61,13 @@ * Any third party settings settings. * @param \Drupal\media\OEmbed\ResourceFetcherInterface $oembed * The oEmbed service. + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + * The messenger service. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ResourceFetcherInterface $oembed) { + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ResourceFetcherInterface $oembed, MessengerInterface $messenger) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); $this->oEmbed = $oembed; + $this->messenger = $messenger; } /** @@ -70,7 +82,8 @@ $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], - $container->get('media.oembed.resource_fetcher') + $container->get('media.oembed.resource_fetcher'), + $container->get('messenger') ); } @@ -95,19 +108,39 @@ if (!empty($item->{$main_property})) { try { $resource = $this->oEmbed->fetchResource($this->oEmbed->getResourceUrl($item->{$main_property}, $this->getSetting('maxwidth'), $this->getSetting('maxheight'))); - $element[$delta] = [ - '#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'], - ]; - + if (empty($resource['type'])) { + $this->messenger->addError($this->t("oEmbed type couldn't be identified.")); + return $element; + } + switch ($resource['type']) { + case 'link': + $element[$delta] = [ + '#title' => $resource['title'], + '#type' => 'link', + '#url' => Url::fromUri($item->{$main_property}), + ]; + break; + + case 'photo': + $element[$delta] = [ + '#theme' => 'image', + '#uri' => $resource['url'], + '#width' => $resource['width'], + '#height' => $resource['height'], + ]; + break; + + case 'video': + case 'rich': + $element[$delta] = [ + '#type' => 'inline_template', + '#template' => (string) $resource['html'], + ]; + break; } } catch (ResourceException $exception) { - drupal_set_message($this->t('Could not retrieve the remote URL.'), 'error'); + $this->messenger->addError($this->t("Could not retrieve the remote URL.")); } } } 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 @@ -10,6 +10,7 @@ use Drupal\Core\Field\FieldTypePluginManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Logger\LoggerChannelInterface; +use Drupal\Core\Messenger\MessengerInterface; use Drupal\link\LinkItemInterface; use Drupal\media\OEmbed\ProviderException; use Drupal\media\OEmbed\ResourceException; @@ -34,6 +35,13 @@ protected $logger; /** + * The messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messenger; + + /** * The oEmbed resource fetcher service. * * @var \Drupal\media\OEmbed\ResourceFetcherInterface @@ -66,14 +74,17 @@ * The field type plugin manager service. * @param \Drupal\Core\Logger\LoggerChannelInterface $logger * The logger channel for media. + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + * The messenger service. * @param \Drupal\media\OEmbed\ResourceFetcherInterface $resource_fetcher * The oEmbed resource fetcher service. * @param \Drupal\media\OEmbed\ProviderDiscoveryInterface $provider_discovery * The oEmbed provider discovery service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, FieldTypePluginManagerInterface $field_type_manager, LoggerChannelInterface $logger, ResourceFetcherInterface $resource_fetcher, ProviderDiscoveryInterface $provider_discovery) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, FieldTypePluginManagerInterface $field_type_manager, LoggerChannelInterface $logger, MessengerInterface $messenger, ResourceFetcherInterface $resource_fetcher, ProviderDiscoveryInterface $provider_discovery) { parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory); $this->logger = $logger; + $this->messenger = $messenger; $this->resourceFetcher = $resource_fetcher; $this->providerDiscovery = $provider_discovery; } @@ -91,6 +102,7 @@ $container->get('config.factory'), $container->get('plugin.manager.field.field_type'), $container->get('logger.factory')->get('media'), + $container->get('messenger'), $container->get('media.oembed.resource_fetcher'), $container->get('media.oembed.provider_discovery') ); @@ -136,7 +148,7 @@ $resource = $this->resourceFetcher->fetchResource($resource_url); } catch (ResourceException $e) { - drupal_set_message($e->getMessage(), 'error'); + $this->messenger->addError($e->getMessage()); return NULL; } diff -u b/core/modules/media/src/Plugin/media/Source/RemoteVideo.php b/core/modules/media/src/Plugin/media/Source/RemoteVideo.php --- b/core/modules/media/src/Plugin/media/Source/RemoteVideo.php +++ b/core/modules/media/src/Plugin/media/Source/RemoteVideo.php @@ -19,3 +19 @@ -class RemoteVideo extends OEmbed { - -} +class RemoteVideo extends OEmbed {} reverted: --- b/core/modules/media/templates/media-oembed.html.twig +++ /dev/null @@ -1,9 +0,0 @@ -{# -/** - * @file - * Default theme implementation to display a post. - * - * @ingroup themeable - */ -#} -{{ post|raw }} diff -u b/core/modules/media/tests/src/Functional/OEmbedProviderDiscoveryTest.php b/core/modules/media/tests/src/Functional/OEmbedProviderDiscoveryTest.php --- b/core/modules/media/tests/src/Functional/OEmbedProviderDiscoveryTest.php +++ b/core/modules/media/tests/src/Functional/OEmbedProviderDiscoveryTest.php @@ -2,7 +2,7 @@ namespace Drupal\Tests\media\Functional; -use Drupal\media\ProviderDiscovery; +use Drupal\media\OEmbed\ProviderDiscovery; /** * Tests the oEmbed provider discovery. @@ -21,7 +21,6 @@ public function testEmptyProviderList($content, $providerUrl, $exceptionClass, $exceptionMessage) { $cacheBackend = $this->container->get('cache.default'); - $logger = $this->container->get('logger.factory'); $body = $this->getMockBuilder('\GuzzleHttp\Psr7\Stream') ->disableOriginalConstructor() @@ -37,16 +36,10 @@ ->getMock(); $client->method('request')->withAnyParameters()->willReturn($response); - $clientFactory = $this->getMockBuilder('\Drupal\Core\Http\ClientFactory') - ->disableOriginalConstructor() - ->getMock(); - $clientFactory->method('fromOptions')->willReturn($client); - $this->setExpectedException($exceptionClass, $exceptionMessage); - /** @var \Drupal\media\ProviderDiscoveryInterface $providerDiscovery */ - $providerDiscovery = new ProviderDiscovery($clientFactory, $cacheBackend, $logger, $providerUrl); - $providerDiscovery->getProviders(); + $providerDiscovery = new ProviderDiscovery($client, $providerUrl, $cacheBackend); + $providerDiscovery->getAll(); } /** @@ -60,14 +53,14 @@ [ json_encode([]), 'http://oembed.com/providers.json', - 'Drupal\media\Exception\OEmbedProviderException', - 'Remote oEmbed providers database returned empty list.', + 'Drupal\media\OEmbed\ProviderException', + 'Remote oEmbed providers database returned invalid or empty list.', ], [ '', 'http://oembed.com/providers.json', - 'Drupal\media\Exception\OEmbedProviderException', - 'Remote oEmbed providers database returned empty list.', + 'Drupal\media\OEmbed\ProviderException', + 'Remote oEmbed providers database returned invalid or empty list.', ], ]; } @@ -80,14 +73,12 @@ public function testNonExistingProviderDatabase($providerUrl, $exceptionClass, $exceptionMessage) { $cacheBackend = $this->container->get('cache.default'); - $logger = $this->container->get('logger.factory'); - $clientFactory = $this->container->get('http_client_factory'); + $client = $this->container->get('http_client'); $this->setExpectedException($exceptionClass, $exceptionMessage); - /** @var \Drupal\media\ProviderDiscoveryInterface $providerDiscovery */ - $providerDiscovery = new ProviderDiscovery($clientFactory, $cacheBackend, $logger, $providerUrl); - $providerDiscovery->getProviders(); + $providerDiscovery = new ProviderDiscovery($client, $providerUrl, $cacheBackend); + $providerDiscovery->getAll(); } /** @@ -100,13 +91,13 @@ return [ [ 'http://oembed1.com/providers.json', - 'Drupal\media\Exception\OEmbedProviderException', - 'Could not retrieve the providers list from the remote oEmbed database.', + 'Drupal\media\OEmbed\ProviderException', + 'Could not retrieve the oEmbed provider database from http://oembed1.com/providers.json', ], [ 'http://oembed.com/providers1.json', - 'Drupal\media\Exception\OEmbedProviderException', - 'Remote oEmbed providers database returned status code 404.', + 'Drupal\media\OEmbed\ProviderException', + 'Could not retrieve the oEmbed provider database from http://oembed.com/providers1.json', ], ]; } only in patch2: unchanged: --- a/core/modules/media/media.info.yml +++ b/core/modules/media/media.info.yml @@ -9,3 +9,4 @@ dependencies: - file - image - user + - link