diff -u b/core/modules/media/config/schema/media.schema.yml b/core/modules/media/config/schema/media.schema.yml --- b/core/modules/media/config/schema/media.schema.yml +++ b/core/modules/media/config/schema/media.schema.yml @@ -55,6 +55,15 @@ media.source.oembed: type: media.source.field_aware label: '"oEmbed" media source configuration' + mapping: + thumbnails_location: + type: string + label: 'Thumbnails location' + allowed_providers: + type: sequence + label: 'Allowed providers' + sequence: + type: string media.source.field_aware: type: mapping 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 @@ -62,19 +62,17 @@ /** * Implements hook_oembed_providers_alter(). */ -function media_oembed_providers_alter(array $providers) { +function media_oembed_providers_alter(array &$providers) { foreach ($providers as $provider_name => $provider_info) { // @TODO Figure out a way of making this validation more robust. Youtube, // for instance, does not come with schemes... if ($provider_info['provider_name'] === 'YouTube') { - $provider_info['endpoints'][0]['schemes'] = [ + $providers[$provider_name]['endpoints'][0]['schemes'] = [ 'http*://*youtube.com/*', 'http*://*youtu.be/*', ]; } } - - return $providers; } /** diff -u b/core/modules/media/src/Exception/OEmbedProviderException.php b/core/modules/media/src/Exception/OEmbedProviderException.php --- b/core/modules/media/src/Exception/OEmbedProviderException.php +++ b/core/modules/media/src/Exception/OEmbedProviderException.php @@ -4,22 +4,37 @@ - - +/** + * Class OEmbedProviderException. + */ class OEmbedProviderException extends \Exception { + /** + * List of oEmbed providers. + * + * @var array + */ protected $providers; + /** + * OEmbedResourceException constructor. + * + * @param string $message + * Exception message. + * @param array $providers + * List of providers. + */ public function __construct($message = "", array $providers = []) { parent::__construct($message); $this->providers = $providers; } /** + * Get the list of providers. + * * @return array + * Returns oEmbed provider list. */ public function getProviders() { return $this->providers; } - - } diff -u b/core/modules/media/src/Exception/OEmbedResourceException.php b/core/modules/media/src/Exception/OEmbedResourceException.php --- b/core/modules/media/src/Exception/OEmbedResourceException.php +++ b/core/modules/media/src/Exception/OEmbedResourceException.php @@ -5,21 +5,4 @@ - - -class OEmbedResourceException extends \Exception { - - protected $providers; - - public function __construct($message = "", array $providers = []) { - parent::__construct($message); - $this->providers = $providers; - } - - /** - * @return array - */ - public function getProviders() { - return $this->providers; - } - - - -} +/** + * Class OEmbedResourceException. + */ +class OEmbedResourceException extends \Exception {} diff -u b/core/modules/media/src/OEmbed.php b/core/modules/media/src/OEmbed.php --- b/core/modules/media/src/OEmbed.php +++ b/core/modules/media/src/OEmbed.php @@ -45,8 +45,8 @@ /** * Static cache of fetched oEmbed resources. * - * A fetched oEmbed resource it the actual retrieved data for a specific - * media object. + * A fetched oEmbed resource is the actual retrieved data for a specific + * media object like name, thumbnail, etc. * * @var array */ @@ -56,7 +56,7 @@ * Static cache of discovered oEmbed resources. * * A discovered oEmbed resource is the oEmbed URL for a specific media object. - * This oEmbed URL is fetched from the cononical URL of the media object. + * This oEmbed URL is fetched from the canonical URL of the media object. * * @var array */ @@ -172,8 +172,6 @@ catch (OEmbedResourceException $exception) { return FALSE; } - - // TODO: handle providers that do not support discovery. return FALSE; } diff -u b/core/modules/media/src/OEmbedProviderDiscovery.php b/core/modules/media/src/OEmbedProviderDiscovery.php --- b/core/modules/media/src/OEmbedProviderDiscovery.php +++ b/core/modules/media/src/OEmbedProviderDiscovery.php @@ -103,8 +103,7 @@ throw new OEmbedProviderException('Remote oEmbed providers database returned empty list.', $providers); } - $providers = \Drupal::moduleHandler() - ->invokeAll('oembed_providers_alter', [$providers]); + \Drupal::moduleHandler()->alter('oembed_providers', $providers); // Some provider names may contain dot chars ("."), which are not // allowed in config keys. We store them in an array where keys are 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 @@ -76,6 +76,8 @@ * The logger channel for media. * @param \Drupal\media\OEmbedInterface $oembed * The oEmbed service. + * @param \Drupal\media\OEmbedProviderDiscoveryInterface $oembed_provider_discovery + * The oEmbed 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, OEmbedInterface $oembed, OEmbedProviderDiscoveryInterface $oembed_provider_discovery) { parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory); @@ -174,6 +176,9 @@ return parent::getMetadata($media, 'default_name'); case 'thumbnail_uri': + if ($uri = $this->getMetadata($media, 'thumbnail_local_uri')) { + return $uri; + } return parent::getMetadata($media, 'thumbnail_uri'); default: @@ -212,18 +217,6 @@ '@response' => json_encode($exception->getProviders()), ]); } - catch (ClientException $exception) { - drupal_set_message($this->t('Could not retrieve the providers list from the remote oEmbed database.'), 'error'); - $this->logger->error('Remote oEmbed providers database returned status code @code.', [ - '@code' => $exception->getResponse()->getStatusCode(), - ]); - return FALSE; - } - catch (RequestException $exception) { - drupal_set_message($this->t('Could not retrieve the providers list from the remote oEmbed database.'), 'error'); - $this->logger->error($exception->getMessage()); - return FALSE; - } $form['allowed_providers'] = [ '#type' => 'select', diff -u b/core/profiles/standard/config/optional/core.entity_form_display.media.video.default.yml b/core/profiles/standard/config/optional/core.entity_form_display.media.video.default.yml --- b/core/profiles/standard/config/optional/core.entity_form_display.media.video.default.yml +++ b/core/profiles/standard/config/optional/core.entity_form_display.media.video.default.yml @@ -25,6 +25,13 @@ type: link_default weight: 2 region: content + status: + type: boolean_checkbox + settings: + display_label: true + weight: 100 + region: content + third_party_settings: { } uid: type: entity_reference_autocomplete weight: 0 diff -u b/core/profiles/standard/config/optional/core.entity_view_display.media.video.default.yml b/core/profiles/standard/config/optional/core.entity_view_display.media.video.default.yml --- b/core/profiles/standard/config/optional/core.entity_view_display.media.video.default.yml +++ b/core/profiles/standard/config/optional/core.entity_view_display.media.video.default.yml @@ -25,4 +25,5 @@ created: true langcode: true + name: true thumbnail: true uid: true diff -u b/core/profiles/standard/config/optional/field.field.media.video.field_media_oembed.yml b/core/profiles/standard/config/optional/field.field.media.video.field_media_oembed.yml --- b/core/profiles/standard/config/optional/field.field.media.video.field_media_oembed.yml +++ b/core/profiles/standard/config/optional/field.field.media.video.field_media_oembed.yml @@ -12,7 +12,7 @@ bundle: video label: OEmbed description: '' -required: false +required: true translatable: true default_value: { } default_value_callback: '' diff -u b/core/profiles/standard/config/optional/field.storage.media.field_media_oembed.yml b/core/profiles/standard/config/optional/field.storage.media.field_media_oembed.yml --- b/core/profiles/standard/config/optional/field.storage.media.field_media_oembed.yml +++ b/core/profiles/standard/config/optional/field.storage.media.field_media_oembed.yml @@ -10,7 +10,7 @@ type: link settings: { } module: link -locked: true +locked: false cardinality: 1 translatable: true indexes: { } only in patch2: unchanged: --- /dev/null +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedTest.php @@ -0,0 +1,92 @@ +getSession(); + $page = $session->getPage(); + $assert_session = $this->assertSession(); + + $this->doTestCreateMediaType($media_type_id, 'oembed', $provided_fields); + + // Create custom fields for the media type to store metadata attributes. + $fields = [ + 'field_string_width' => 'string', + 'field_string_height' => 'string', + 'field_string_author_name' => 'string', + ]; + $this->createMediaTypeFields($fields, $media_type_id); + + // Hide the name field widget to test default name generation. + $this->hideMediaTypeFieldWidget('name', $media_type_id); + + $this->drupalGet("admin/structure/media/manage/{$media_type_id}"); + // Only accept vimeo videos. + $page->selectFieldOption("source_configuration[allowed_providers][]", '56696d656f'); + + $page->selectFieldOption("field_map[width]", 'field_string_width'); + $page->selectFieldOption("field_map[height]", 'field_string_height'); + $page->selectFieldOption("field_map[author_name]", 'field_string_author_name'); + $page->pressButton('Save'); + + // Create a media item. + $this->drupalGet("media/add/{$media_type_id}"); + $page->fillField("OEmbed", 'https://vimeo.com/7073899'); + $page->pressButton('Save'); + + $assert_session->addressEquals('media/1'); + + // Make sure the thumbnail is displayed from uploaded image. + $assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'oembed_thumbnails/Vimeo_1455b.jpg'); + + // Load the media and check that all fields are properly populated. + $media = Media::load(1); + $this->assertEquals('Drupal Rap Video - Schipulcon09', $media->getName()); + $this->assertEquals('480', $media->get('field_string_width')->value); + $this->assertEquals('360', $media->get('field_string_height')->value); + + // Try to create a youtube item, which should not be allowed. + $this->drupalGet("media/add/{$media_type_id}"); + $page->fillField("OEmbed", 'https://www.youtube.com/watch?v=lZ-s3DRZJKY'); + $page->pressButton('Save'); + + $assert_session->pageTextContains('The provider used is not allowed.'); + } + +}