diff --git a/core/modules/media/media.api.php b/core/modules/media/media.api.php index 93244f58a8..4af2e66102 100644 --- a/core/modules/media/media.api.php +++ b/core/modules/media/media.api.php @@ -37,6 +37,20 @@ function hook_oembed_resource_url_alter(array &$parsed_url, \Drupal\media\OEmbed } } +/** + * Alters the context information that get passed to the oembed iframe. + * + * @param array $context + * Context information that get passed to media_oembed_iframe theme function. + * @param \Drupal\Core\Field\FormatterInterface $plugin + * The FieldFormatter interface. + * @param \Drupal\media\OEmbed\Resource $resource + * The oembed resource. + */ +function hook_oembed_formatter_context_alter(array &$context, \Drupal\Core\Field\FormatterInterface $plugin, \Drupal\media\OEmbed\Resource $resource) { + $context['plugin_id'] = $plugin->getPluginId(); +} + /** * @} End of "addtogroup hooks". */ diff --git a/core/modules/media/media.module b/core/modules/media/media.module index b4d3112e9c..f836e029cc 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -80,6 +80,7 @@ function media_theme() { 'resource' => NULL, 'media' => NULL, 'placeholder_token' => '', + 'context' => NULL, ], ], 'media_embed_error' => [ diff --git a/core/modules/media/src/Controller/OEmbedIframeController.php b/core/modules/media/src/Controller/OEmbedIframeController.php index 5e7b12ff37..62c1d45f1b 100644 --- a/core/modules/media/src/Controller/OEmbedIframeController.php +++ b/core/modules/media/src/Controller/OEmbedIframeController.php @@ -123,10 +123,12 @@ public function render(Request $request) { $url = $request->query->get('url'); $max_width = $request->query->getInt('max_width'); $max_height = $request->query->getInt('max_height'); + $context = $request->query->get('context', NULL); // Hash the URL and max dimensions, and ensure it is equal to the hash // parameter passed in the query string. $hash = $this->iFrameUrlHelper->getHash($url, $max_width, $max_height); + if (!hash_equals($hash, $request->query->get('hash', ''))) { throw new AccessDeniedHttpException('This resource is not available'); } @@ -137,6 +139,7 @@ public function render(Request $request) { $response = new HtmlResponse('', HtmlResponse::HTTP_OK, [ 'Content-Type' => 'text/html; charset=UTF-8', ]); + $response->addCacheableDependency(Url::createFromRequest($request)); try { @@ -169,11 +172,14 @@ public function render(Request $request) { ], ], '#placeholder_token' => $placeholder_token, + '#context' => $context, ]; + $context = new RenderContext(); $content = $this->renderer->executeInRenderContext($context, function () use ($element) { return $this->renderer->render($element); }); + $response ->setContent($content) ->setAttachments($element['#attached']) diff --git a/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php b/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php index c006231447..9020cebf32 100644 --- a/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php +++ b/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php @@ -4,6 +4,7 @@ use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; @@ -82,6 +83,13 @@ class OEmbedFormatter extends FormatterBase { */ protected $iFrameUrlHelper; + /** + * The module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + /** * Constructs an OEmbedFormatter instance. * @@ -111,8 +119,10 @@ class OEmbedFormatter extends FormatterBase { * The config factory service. * @param \Drupal\media\IFrameUrlHelper $iframe_url_helper * The iFrame URL helper service. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler service. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, MessengerInterface $messenger, ResourceFetcherInterface $resource_fetcher, UrlResolverInterface $url_resolver, LoggerChannelFactoryInterface $logger_factory, ConfigFactoryInterface $config_factory, IFrameUrlHelper $iframe_url_helper) { + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, MessengerInterface $messenger, ResourceFetcherInterface $resource_fetcher, UrlResolverInterface $url_resolver, LoggerChannelFactoryInterface $logger_factory, ConfigFactoryInterface $config_factory, IFrameUrlHelper $iframe_url_helper, ModuleHandlerInterface $module_handler) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); $this->messenger = $messenger; $this->resourceFetcher = $resource_fetcher; @@ -120,6 +130,7 @@ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInter $this->logger = $logger_factory->get('media'); $this->config = $config_factory->get('media.settings'); $this->iFrameUrlHelper = $iframe_url_helper; + $this->moduleHandler = $module_handler; } /** @@ -139,7 +150,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('media.oembed.url_resolver'), $container->get('logger.factory'), $container->get('config.factory'), - $container->get('media.oembed.iframe_url_helper') + $container->get('media.oembed.iframe_url_helper'), + $container->get('module_handler') ); } @@ -194,16 +206,22 @@ public function viewElements(FieldItemListInterface $items, $langcode) { ]; } else { + // We construct context information for the iframe. + $context = []; + $this->moduleHandler->alter('oembed_formatter_context', $context, $this, $resource); + $url = Url::fromRoute('media.oembed_iframe', [], [ 'query' => [ 'url' => $value, 'max_width' => $max_width, 'max_height' => $max_height, + 'context' => $context, 'hash' => $this->iFrameUrlHelper->getHash($value, $max_width, $max_height), ], ]); $domain = $this->config->get('iframe_domain'); + if ($domain) { $url->setOption('base_url', $domain); } @@ -232,6 +250,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { // An empty title attribute will disable title inheritance, so only // add it if the resource has a title. $title = $resource->getTitle(); + if ($title) { $element[$delta]['#attributes']['title'] = $title; } @@ -241,6 +260,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { ->applyTo($element[$delta]); } } + return $element; }