diff --git a/core/modules/media/src/MediaSourceBase.php b/core/modules/media/src/MediaSourceBase.php index d7827eed59..23e7a4f67b 100644 --- a/core/modules/media/src/MediaSourceBase.php +++ b/core/modules/media/src/MediaSourceBase.php @@ -321,6 +321,22 @@ protected function getSourceFieldName() { return $id; } + /** + * {@inheritdoc} + */ + public function getSourceFieldValue(MediaInterface $media) { + $source_field = $this->configuration['source_field']; + if (empty($source_field)) { + throw new \RuntimeException('Source field for media source is not defined.'); + } + // Source value is stored as the main property of the source field in 99% of + // the cases so return that. 1% can override this function and tweak the + // logic. + /** @var \Drupal\Core\Field\FieldItemInterface $field_item */ + $field_item = $media->get($source_field)->first(); + return $field_item->get($field_item->mainPropertyName()); + } + /** * {@inheritdoc} */ diff --git a/core/modules/media/src/MediaSourceInterface.php b/core/modules/media/src/MediaSourceInterface.php index 43dd8aa733..8c9d4df690 100644 --- a/core/modules/media/src/MediaSourceInterface.php +++ b/core/modules/media/src/MediaSourceInterface.php @@ -177,4 +177,20 @@ public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayIn */ public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display); + /** + * Gets source field value. + * + * Value that is stored in the source field. + * + * @param MediaInterface $media + * A media item. + * + * @return \Drupal\Core\TypedData\TypedDataInterface + * The source value. + * + * @throws \RuntimeException + * If the source field for the media source is not defined. + */ + public function getSourceFieldValue(MediaInterface $media); + } diff --git a/core/modules/media/src/Plugin/Validation/Constraint/OEmbedProviderConstraintValidator.php b/core/modules/media/src/Plugin/Validation/Constraint/OEmbedProviderConstraintValidator.php index c369a0a66a..2c7feb349a 100644 --- a/core/modules/media/src/Plugin/Validation/Constraint/OEmbedProviderConstraintValidator.php +++ b/core/modules/media/src/Plugin/Validation/Constraint/OEmbedProviderConstraintValidator.php @@ -4,6 +4,7 @@ use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\media\OEmbed\ProviderDiscoveryInterface; use Drupal\media\OEmbed\ProviderException; @@ -25,14 +26,24 @@ class OEmbedProviderConstraintValidator extends ConstraintValidator implements C */ protected $providerDiscovery; + /** + * The logger service. + * + * @var \Drupal\Core\Logger\LoggerChannelInterface + */ + protected $logger; + /** * Constructs a new OEmbedProviderConstraintValidator. * * @param \Drupal\media\OEmbed\ProviderDiscoveryInterface $provider_discovery * The oEmbed provider discovery service. + * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory + * The logger service. */ - public function __construct(ProviderDiscoveryInterface $provider_discovery) { + public function __construct(ProviderDiscoveryInterface $provider_discovery, LoggerChannelFactoryInterface $logger_factory) { $this->providerDiscovery = $provider_discovery; + $this->logger = $logger_factory->get('media'); } /** @@ -40,7 +51,8 @@ public function __construct(ProviderDiscoveryInterface $provider_discovery) { */ public static function create(ContainerInterface $container) { return new static( - $container->get('media.oembed.provider_discovery') + $container->get('media.oembed.provider_discovery'), + $container->get('logger.factory') ); } @@ -58,6 +70,7 @@ public function validate($value, Constraint $constraint) { } catch (ProviderException $e) { $this->context->addViolation($this->t('An error occurred while trying to retrieve the oEmbed provider database.')); + $this->logger->error($e->getMessage()); return; } diff --git a/core/modules/media/src/Plugin/media/Source/OEmbed.php b/core/modules/media/src/Plugin/media/Source/OEmbed.php index ca1303e8bf..fdc0861640 100644 --- a/core/modules/media/src/Plugin/media/Source/OEmbed.php +++ b/core/modules/media/src/Plugin/media/Source/OEmbed.php @@ -145,12 +145,7 @@ public function getMetadataAttributes() { * {@inheritdoc} */ public function getMetadata(MediaInterface $media, $name) { - $source_field = $this->getSourceFieldDefinition($media->bundle->entity)->getName(); - /** @var \Drupal\Core\Field\FieldItemInterface $source_field_item */ - $source_field_item = $media->get($source_field)->first(); - - $main_property = $source_field_item->mainPropertyName(); - $resource_url = $source_field_item->get($main_property)->getString(); + $resource_url = $this->getSourceFieldValue($media); try { $resource_url = $this->resourceFetcher->getResourceUrl($resource_url);