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 83884fe45f..0dde4e206d 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -82,6 +82,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 3c5079f620..1e7ed129a4 100644 --- a/core/modules/media/src/Controller/OEmbedIframeController.php +++ b/core/modules/media/src/Controller/OEmbedIframeController.php @@ -136,6 +136,7 @@ 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->all('context', NULL); // Hash the URL and max dimensions, and ensure it is equal to the hash // parameter passed in the query string. @@ -182,6 +183,7 @@ public function render(Request $request) { ], ], '#placeholder_token' => $placeholder_token, + '#context' => $context, ]; $context = new RenderContext(); $content = $this->renderer->executeInRenderContext($context, function () use ($element) { diff --git a/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php b/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php index ab590ed41f..e63f7ebd12 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') ); } @@ -200,12 +212,17 @@ 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', [], [ 'absolute' => TRUE, 'query' => [ 'url' => $value, 'max_width' => $max_width, 'max_height' => $max_height, + 'context' => $context, 'hash' => $this->iFrameUrlHelper->getHash($value, $max_width, $max_height), ], ]);