diff --git a/core/modules/media/src/Plugin/media/Source/OEmbed.php b/core/modules/media/src/Plugin/media/Source/OEmbed.php index b95288aae8..ce3d64974d 100644 --- a/core/modules/media/src/Plugin/media/Source/OEmbed.php +++ b/core/modules/media/src/Plugin/media/Source/OEmbed.php @@ -14,6 +14,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Url; +use Drupal\Core\Utility\Token; use Drupal\media\IFrameUrlHelper; use Drupal\media\OEmbed\Resource; use Drupal\media\OEmbed\ResourceException; @@ -124,6 +125,13 @@ class OEmbed extends MediaSourceBase implements OEmbedInterface { */ protected $fileSystem; + /** + * The token replacement instance. + * + * @var \Drupal\Core\Utility\Token + */ + protected $token; + /** * Constructs a new OEmbed instance. * @@ -155,8 +163,10 @@ class OEmbed extends MediaSourceBase implements OEmbedInterface { * The iFrame URL helper service. * @param \Drupal\Core\File\FileSystemInterface $file_system * The file system. + * @param \Drupal\Core\Utility\Token $token + * The token replacement instance. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, FieldTypePluginManagerInterface $field_type_manager, LoggerInterface $logger, MessengerInterface $messenger, ClientInterface $http_client, ResourceFetcherInterface $resource_fetcher, UrlResolverInterface $url_resolver, IFrameUrlHelper $iframe_url_helper, FileSystemInterface $file_system) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, FieldTypePluginManagerInterface $field_type_manager, LoggerInterface $logger, MessengerInterface $messenger, ClientInterface $http_client, ResourceFetcherInterface $resource_fetcher, UrlResolverInterface $url_resolver, IFrameUrlHelper $iframe_url_helper, FileSystemInterface $file_system, Token $token) { parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory); $this->logger = $logger; $this->messenger = $messenger; @@ -165,6 +175,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition $this->urlResolver = $url_resolver; $this->iFrameUrlHelper = $iframe_url_helper; $this->fileSystem = $file_system; + $this->token = $token; } /** @@ -185,7 +196,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('media.oembed.resource_fetcher'), $container->get('media.oembed.url_resolver'), $container->get('media.oembed.iframe_url_helper'), - $container->get('file_system') + $container->get('file_system'), + $container->get('token') ); } @@ -243,7 +255,7 @@ public function getMetadata(MediaInterface $media, $name) { return parent::getMetadata($media, 'default_name'); case 'thumbnail_uri': - return $this->getLocalThumbnailUri($resource) ?: parent::getMetadata($media, 'thumbnail_uri'); + return $this->getLocalThumbnailUri($resource, $media) ?: parent::getMetadata($media, 'thumbnail_uri'); case 'type': return $resource->getType(); @@ -373,6 +385,8 @@ public function defaultConfiguration() { * * @param \Drupal\media\OEmbed\Resource $resource * The oEmbed resource. + * @param \Drupal\media\MediaInterface $media + * The Media interface. * * @return string|null * The local thumbnail URI, or NULL if it could not be downloaded, or if the @@ -383,7 +397,7 @@ public function defaultConfiguration() { * toggle-able. See https://www.drupal.org/project/drupal/issues/2962751 for * more information. */ - protected function getLocalThumbnailUri(Resource $resource) { + protected function getLocalThumbnailUri(Resource $resource, MediaInterface $media) { // If there is no remote thumbnail, there's nothing for us to fetch here. $remote_thumbnail_url = $resource->getThumbnailUrl(); if (!$remote_thumbnail_url) { @@ -393,7 +407,7 @@ protected function getLocalThumbnailUri(Resource $resource) { // Ensure that we can write to the local directory where thumbnails are // stored. $configuration = $this->getConfiguration(); - $directory = $configuration['thumbnails_directory']; + $directory = $this->token->replace($configuration['thumbnails_directory'], ['media' => $media]); // The local thumbnail doesn't exist yet, so try to download it. First, // ensure that the destination directory is writable, and if it's not, diff --git a/core/modules/media/tests/src/Kernel/OEmbedSourceTest.php b/core/modules/media/tests/src/Kernel/OEmbedSourceTest.php index 314b2796e8..faf8ca8521 100644 --- a/core/modules/media/tests/src/Kernel/OEmbedSourceTest.php +++ b/core/modules/media/tests/src/Kernel/OEmbedSourceTest.php @@ -26,6 +26,21 @@ class OEmbedSourceTest extends MediaKernelTestBase { */ protected static $modules = ['media']; + /** + * Token service. + * + * @var \Drupal\Core\Utility\Token + */ + protected $token; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + $this->token = \Drupal::token(); + } + /** * @covers ::getMetadata */ @@ -164,9 +179,14 @@ public function testThumbnailUri(string $remote_thumbnail_url, $thumbnail_header $source->getSourceFieldDefinition($media_type)->getName() => $this->randomString(), ]); $media->save(); - + $hash = Crypt::hashBase64($thumbnail_url); // The thumbnail should have a file extension, even if it wasn't in the URL. - $expected_uri = 'public://oembed_thumbnails/' . Crypt::hashBase64($thumbnail_url) . ".$expected_extension"; + $expected_uri = 'public://oembed_thumbnails/' . $hash . '.' . $expected_extension; + $this->assertSame($expected_uri, $source->getMetadata($media, 'thumbnail_uri')); + + // Get the local thumbnail URI using token. + $directory = $this->token->replace('public://oembed_thumbnails', ['media' => $media]); + $expected_uri = $directory . DIRECTORY_SEPARATOR . $hash . '.' . $expected_extension; $this->assertSame($expected_uri, $source->getMetadata($media, 'thumbnail_uri')); // Even if we get the thumbnail_uri more than once, it should only be // downloaded once (this is verified by the shouldBeCalledOnce() checks